AWS Cloud Formation

Code Examples

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"]
 
aws/aws-cloudformation.txt · Last modified: 04/04/2024 09:29 by andrew