amazon-web-servicesaws-cloudformation

Use a map in CloudFormation to define static dev and prod configs?


I want to have a single template for all my deployments.

I want my dev and prod configuration defined in the same template.

When I call the template I pass a param for dev or prod and CloudFormation uses the variables for the dev or prod map- is this possible?

I can see it supports maps but I'm unsure of how to achieve what i want with it: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.html

Is there a way I could make all my params maps and then have one param for dev or prod and when I set that one param when deploying CloudFormation uses the dev or prod property of the params?

Example:

Parameters:
  env:
    Description: environment
    Type: String
    Default: dev

Mappings: 
  RegionMap: 
    dev: 
      HVM64: "ami-0ff8a91507f77f867"
      HVMG2: "ami-0a584ac55a7631c0c"
    prod: 
      HVM64: "ami-0bdb828fd58c52235"
      HVMG2: "ami-066ee5fd4a9ef77f1"
  DynamoTableName:
    dev: 'devtable'
    prod: 'prodtable'
  SomeOtherVar: ...

Resources: 
  myEC2Instance: 
    Type: "AWS::EC2::Instance"
    Properties: 
      ImageId: !FindInMap
        - RegionMap
        - !Ref env
        - HVM64
      InstanceType: m1.small

Solution

  • Yes. You can use the Params https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html

    Here is passing param with CLI. https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack.html

    Here is how to find value in Map. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html

    Is this what you want?