Table of Contents

AWS Cloud Formation

Code Examples

Deploy CloudFormation from cli

Using !Ref

Parameters, Mappings, and !Refs

Cloud Formation Mappings and Output Section

Example from course for best practice example

AWS Lambda Function with Tags

Set numeric value to string

Force an string value instead of integer (join two strings with nothing, “”):-

!Join ["", ["00", "1002899"]]

Tags

    Properties:
      Tags:
        - Key: "Tag1"
          Value: "TagValue"
        - Key: "CreatedBy"
          Value: "CloudFormation"

Outputs and Exports

From:- https://aws.amazon.com/blogs/aws/aws-cloudformation-update-yaml-cross-stack-references-simplified-substitution/

You can now create and export values from one stack and make use of them in other stacks without going to the trouble of creating custom CloudFormation resources. The first stack exports values like this:

Outputs: 
  TSSG: 
    Value: !Ref TroubleShootingSG
    Export:
      Name: AccountSG

The other stacks then reference them using the new ImportValue function:

EC2Instance:
  Type: AWS::EC2::Instance
  Properties:
    SecurityGroups:
      - !ImportValue AccountSG

StackSet - CLI

Create StackSet

aws cloudformation create-stack-set --stack-set-name <name> --description "Description Text" --template-body file://template.yaml --profile profile1234 --capabilities CAPABILITY-WHATEVER --parameters file://parameters.json --auto-deployment file://auto-deployment.json --managed-execution Active=false --auto-deployment Enabled=true, RetainStacksOnAccountRemoval=false

autodeployment.json

{
    "Enabled": true,
    "RetainStacksOnAccountRemoval": false
}

Deploy Stackset

aws cloudformation create-stack-instances --stack-set-name "Name" --accounts 123412341234  --regions file://regions.json --output json --profile profile1234

regions.json:-

["eu-west-1", "us-east-1"]