Skip to content
reidca and Chuck Meyer Added support for updating strings containing decimal placeholders wi…
…thin resources and changed naming format of resources to add iteration count to name (#155)

* Added support for decimal place holder replacements

* Updated the readme to make usage clearer

* Typo fix
Latest commit e17fdca Feb 18, 2019
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
src
README.md
deploy.sh
event.json Add "Count" macro example featured in DEV327 at re:Invent 2018 (#141) Jan 2, 2019
event_bad.json Add "Count" macro example featured in DEV327 at re:Invent 2018 (#141) Jan 2, 2019
requirements.txt Raised version of requirements to resolve security issue (#142) Jan 2, 2019
template.yaml
test.yaml
test_2.yaml Added support for updating strings containing decimal placeholders wi… Feb 18, 2019

README.md

Count CloudFormation Macro

The Count macro provides a template-wide Count property for CloudFormation resources. It allows you to specify multiple resources of the same type without having to cut and paste.

How to install and use the Count macro in your AWS account

Deploying

  1. You will need an S3 bucket to store the CloudFormation artifacts:

    • If you don't have one already, create one with aws s3 mb s3://<bucket name>
  2. Package the Macro CloudFormation template. The provided template uses the AWS Serverless Application Model so must be transformed before you can deploy it.

    aws cloudformation package \
        --template-file template.yaml \
        --s3-bucket <your bucket name here> \
        --output-template-file packaged.yaml
  3. Deploy the packaged CloudFormation template to a CloudFormation stack:

    aws cloudformation deploy \
        --stack-name Count-macro \
        --template-file packaged.yaml \
        --capabilities CAPABILITY_IAM
  4. To test out the macro's capabilities, try launching the provided example template:

    aws cloudformation deploy \
        --stack-name Count-test \
        --template-file test.yaml \
        --capabilities CAPABILITY_IAM

Usage

To make use of the macro, add Transform: Count to the top level of your CloudFormation template.

To create multiple copies of a resource, add a Count propert with an integer value.

AWSTemplateFormatVersion: "2010-09-09"
Transform: Count
Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Count: 3
  SQS:
    Type: AWS:::SQS::Queue
    Count: 2

Note

This will cause the resource "Bucket" to be multiplied 3 times. The new template will contain Bucket1, Bucket2 and Bucket3 but will not contain Bucket as this will be removed.

Using decimal placeholders

When resources are multiplied, you can put a decimal placeholder %d into any string value that you wish to be replaced with the iterator index number.

e.g.

AWSTemplateFormatVersion: "2010-09-09"
Transform: Count
Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      Tags:
        - Key: TestKey
          Value: my bucket %d
    Count: 3

Using this example, the processed template will result become:

AWSTemplateFormatVersion: "2010-09-09"
Resources:
  Bucket1:
    Type: AWS::S3::Bucket
    Properties:
      Tags:
        - Key: TestKey
          Value: my bucket 1
  Bucket2:
    Type: AWS::S3::Bucket
    Properties:
      Tags:
        - Key: TestKey
          Value: my bucket 2
  Bucket3:
    Type: AWS::S3::Bucket
    Properties:
      Tags:
        - Key: TestKey
          Value: my bucket 3

Important - Naming resources

You cannot use Count on resources that use a hardcoded name (Name: property). Duplicate names will cause a CloudFormation runtime failure. If you wish to specify a name then you can use the decimal place holder %d in the name which will cause the name to incorporate the iterator value.

e.g.

AWSTemplateFormatVersion: "2010-09-09"
Resources:
  Bucket1:
    Type: AWS::S3::Bucket
    Properties:
        BucketName: MyBucket%d

Author

Jose Ferraris AWS ProServ DevOps Consultant Amazon Web Services

You can’t perform that action at this time.