AWS CloudFormation

Table of Contents

Overview

Template

---
AWSTemplateFormatVersion: "version date"

Description:
  String

Metadata:
  template metadata

Parameters:
  set of parameters

Mappings:
  set of mappings

Conditions:
  set of conditions

Transform:
  set of transforms

Resources:
  set of resources

Outputs:
  set of outputs

AWSTemplateFormatVersion

The latest template format version is 2010-09-09 and is currently the only valid value.

AWSTemplateFormatVersion: "2010-09-09"

Parameters

Parameters: 
  InstanceTypeParameter: 
    Type: String
    Default: t2.micro
    AllowedValues: 
      - t2.micro
      - m1.small
      - m1.large
    Description: Enter t2.micro, m1.small, or m1.large. Default is t2.micro.

Use the Ref intrinsic function to reference a parameter:

Ec2Instance:
  Type: AWS::EC2::Instance
  Properties:
    InstanceType:
      Ref: InstanceTypeParameter
    ImageId: ami-2f726546

Types

Mappings

Mappings: 
  Mapping01: 
    Key01: 
      Name: Value01
    Key02: 
      Name: Value02
    Key03: 
      Name: Value03
RegionMap: 
  us-east-1: 
    "32": "ami-6411e20d"
    "64": "ami-7a11e213"
  us-west-1: 
    "32": "ami-c9c7978c"
    "64": "ami-cfc7978a"
  eu-west-1: 
    "32": "ami-37c2f643"
    "64": "ami-31c2f645"
  ap-southeast-1: 
    "32": "ami-66f28c34"
    "64": "ami-60f28c32"
  ap-northeast-1: 
    "32": "ami-9c03a89d"
    "64": "ami-a003a8a1"
Resources: 
  myEC2Instance: 
    Type: "AWS::EC2::Instance"
    Properties: 
      ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", 32]
      InstanceType: m1.small

Conditions

You might use conditions when you want to reuse a template that can create resources in different contexts, such as a test environment versus a production environment.

Conditions: 
  CreateProdResources: !Equals [ !Ref EnvType, prod ]
Resources: 
  EC2Instance: 
    Type: "AWS::EC2::Instance"
    Properties: 
      ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", AMI]
  MountPoint: 
    Type: "AWS::EC2::VolumeAttachment"
    Condition: CreateProdResources
    Properties: 
      InstanceId: 
        !Ref EC2Instance
      VolumeId: 
        !Ref NewVolume
      Device: /dev/sdh
  NewVolume: 
    Type: "AWS::EC2::Volume"
    Condition: CreateProdResources
    Properties: 
      Size: 100
      AvailabilityZone: 
        !GetAtt EC2Instance.AvailabilityZone
Outputs: 
  VolumeId: 
    Condition: CreateProdResources
    Value: 
      !Ref NewVolume

Transform

A kind of macro feature. For instance, AWS:Include includes templete definitions fromother files:

Resources:
  MyBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
        'Fn::Transform':
            - Name: 'AWS::Include'
              Parameters:
                Location: s3://bucket/myBucketName.yaml
            - Name: 'AWS::Include'
              Parameters:
                Location: s3://bucket/myBucketAcl.yaml

Resources

Resources:
  Logical ID:
    Type: Resource type
    Properties:
      Set of properties
Logical ID
The logical ID must be alphanumeric (A-Za-z0-9) and unique within the template.

Outputs

Outputs:
  Logical ID:
    Description: Information about the value
    Value: Value to return
    Export:
      Name: Value to export

Functions

GetAtt

!GetAtt logicalNameOfResource.attributeName

Sub

Name: !Sub
  - www.${Domain}
  - { Domain: !Ref RootDomainName }

Resource Types

AWS::DynamoDB::Table

Type: "AWS::DynamoDB::Table"
Properties:
  AttributeDefinitions:
    - AttributeDefinition
  GlobalSecondaryIndexes:
    - GlobalSecondaryIndexes
  KeySchema:
    - KeySchema
  LocalSecondaryIndexes:
    - LocalSecondaryIndexes
  ProvisionedThroughput:
    ProvisionedThroughput
  SSESpecification:
    SSESpecification
  StreamSpecification:
    StreamSpecification
  TableName: String
  Tags: 
    - Resource Tag
  TimeToLiveSpecification: 
    TimeToLiveSpecification

AttributeDefinition

The AttributeDefinition property type represents an attribute for describing the key schema for a DynamoDB table and indexes. They don't represent the full schema of the table.

KeySchema

KeySchema: 
  - AttributeName: Sales
    KeyType: HASH
KeySchema: 
  - AttributeName: Sales
    KeyType: HASH
  - AttributeName: Artist
    KeyType: RANGE