Home > Net >  Different Cloud Formation parameter values for every environment
Different Cloud Formation parameter values for every environment

Time:10-19

I did create a sns topic. However I wanna deploy it in multiple environments staging and prod. Like the code below for each environment.

MySNSTopic:

MySNSTopic:
      Type: AWS::SNS::Topic
      Properties:
        TopicName: mytopic
        Subscription:
          - Protocol: 
            Endpoint: 
          - Protocal:  
            Endpoint: 

Parameters:
  Mappings:
    snsProtocol:
      qa: Email
      stg: Email
      prd: https
    snsEndpoint:
      qa: Email
        stg: Email
        prd: https
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Parameters: Mappings: snsProtocol: qa: Email stg: Email prd: https snsEndpoint: qa: Email stg: Email prd: https

Thank you enter image description here

CodePudding user response:

You need to provide Conditions in CloudFormation template. That will get used while creating resource. Something like below.

Parameters:
    EnvType:
      Description: Environment type.
      Default: test
      Type: String
      AllowedValues:
        - prod
        - test
      ConstraintDescription: must specify prod or test.
Conditions:
  CreateProdResources: !Equals 
    - !Ref EnvType
    - prod

Reference: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html#conditions-section-structure-examples

CodePudding user response:

Consider using Fn::FindInMap:

Parameters:
  EnvType:
    Default: qa
    Type: String
    AllowedValues:
      - qa
      - stg
      - prd
  Mappings:
    SettingsMap:
      qa:
        Protocol: Email
        Endpoint: Email
      stg:
        Protocol: Email
        Endpoint: Email 
      prd:
        Protocol: https
        Endpoint: https

and then:

Subscription:
  Protocol: !FindInMap
    - SettingsMap
    - !Ref EnvType
    - Protocol
  • Related