Search code examples
aws-cloudformationserverlessaws-saminfrastructure-as-code

How to create multiple cloudformation stacks where a single parameter changes?


I want to enter a list of cities as parameters to a SAM template. For Example: London, New York, Dubai. The list can have a large number of cities, say 50.

Using this list of cities, I want to create a log group pertaining to each city. For example:

  • /aws/ecs/Dubai
  • /aws/ecs/London
  • /aws/ecs/New York

How can I achieve this?


Solution

  • You can use Count Macro in Cloudformation.

    AWSTemplateFormatVersion: "2010-09-09"
    Transform: Count
    Resources:
       Type: AWS::Logs::LogGroup
       Properties: 
           LogGroupName: /aws/ecs/%s
       Count: ['Dubai','London','NewYork']
    

    For more information check this question: How to loop through values in a CloudFormation template