CloudFormation

When you use AWS CloudFormation, you work with templates and stacks. You create templates to describe your collection of AWS resources and their properties. CloudFormation manages those related resources as a single unit called a stack. CloudFormation creates a stack and then provisions the resources that are described in your template. So, CloudFormation creates, updates, and deletes a collection of resources defined in the template by creating, updating, and deleting the stack. If you need to make changes to the running resources in a stack, you update the stack. Before making changes to your resources, you can generate a change set, which is a summary of your proposed changes. Change sets allow you to see how your changes might impact your running resources, especially for critical resources, before implementing them.

Template - YAML

References:

Resources: 
  WebServer:
    Type: "AWS::Dummy::DemoOnly"
    Properties: {}
    # reference a parameter
    KeyName: !Ref Param1
    CidrBlock: !Select [0, !Ref Param1]
# Parameters enable: template reuse;
Parameters:
  Param1:
    Description: param1 description
    Type: [String|Number|...]
    AllowedValues:
      - "one"
      - "two"
      [...]
# Mapping
RegionMap:
  us-west-1:
    "32": "ami-6411e20d"
    "64": "ami-7a11e213"
  us-west-2:
    "32": "ami-6612e20d"
    "64": "ami-8b11e213"
  

Template components:

  • Resources

  • Parameters

  • Mappings

  • Outputs

  • Conditionals

  • Metadata

Template options:

  • Tags

  • Permissions

  • Notification options

  • Timeouts

  • Rollback on failure

  • Stack policy

Parameters

Reference: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.htmlarrow-up-right

Parameters will be displayed on the AWS management console after the template is uploaded. Parameter settings include:

  • Description

  • Type:

    • String

    • Number

    • CommaDelimitedList

    • List<Type>

    • AWS Parameter (guard against invalid values, by matching against existing values in the AWS account)

  • Costraints

  • ConstraintDescription (String)

  • Min/MaxLength

  • Min/MaxValue

  • Defaults

  • AllowedValues (array)

  • AllowedPattern (regexp)

  • NoEcho (boolean)

Resources

Reference: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.htmlarrow-up-right

Mapping

Reference: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.htmlarrow-up-right

Define:

Retrieve:

Pseudo parameters

Examples:

  • AWS::AccountId

  • AWS::Region

  • AWS::StackName

Retrieve:

Outputs

Reference: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.htmlarrow-up-right

Define:

Retrieve:

Conditions

Reference: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.htmlarrow-up-right

Define:

Intrinsic functions are: !And, !Equals, !If, !Not, and !Or

Additional Fn: !GetAtt

Example:

Intrinsic Function - Fn::GetAtt

Reference: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#aws-properties-ec2-instance-return-valuesarrow-up-right

Example:

Metadata

Reference: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/metadata-section-structure.htmlarrow-up-right

The optional Metadata section to include arbitrary JSON or YAML objects that provide details about the template. For example, you can include template implementation details about specific resources, as shown in the following snippet:

Run from AWS CLI

Prerequisite:

  • Install & setup AWS CLI

AWS Samples

Examples

chevron-rightCloudformation Templates - various EPC & VPC setuphashtag

Github: https://github.com/gabepublic/aws-cloudformation-templatesarrow-up-right

Summary:

  • EC2:

    • EC2 with ingress on port 80

    • EC2 with ingress on port 22

    • EC2 with ingress on ports 22 & 80

  • VPC:

    • VPC with 4 subnets VPC with 1 public subnet, igw and EC2 + website

    • VPC with 2 public subnets, igw, load balancer, and EC2 + website

    • VPC 2 public subnets, bastion host, alb, ec2 + website

    • VPC with 4 subnets (2 public & 2 private), igw, alb, and EC2 website & APIs

EC2

chevron-rightEC2 with ingress on port 80hashtag
chevron-rightEC2 with ingress on port 22hashtag

VPC

chevron-rightVPC with 1 public subnet, igw and EC2 + websitehashtag
chevron-rightVPC with 2 public subnets, igw, load balancer, and EC2 + websitehashtag
chevron-rightVPC 2 public subnets, bastion host, alb, ec2 + websitehashtag
chevron-rightVPC with 4 subnetshashtag

References

Last updated