为各环境配置不同的CloudFormation参数,实现SNS主题多环境部署
Great question! Let's walk through how to configure your CloudFormation template to deploy the SNS Topic across staging (stg) and production (prod) environments with proper differentiation. We'll use CloudFormation Mappings and Parameters to keep things clean and maintainable.
Your original code has duplicate Parameters and Mappings sections—let's fix that first to avoid errors. We'll keep a single set of mappings and parameters to define environment-specific values.
Mappings are perfect for this scenario because they let you define key-value pairs per environment, which CloudFormation can look up during deployment. We'll define the protocol and endpoint for each environment here:
Mappings: SNSEnvironmentConfig: stg: Protocol: Email Endpoint: "staging-alerts@yourdomain.com" prod: Protocol: https Endpoint: "https://prod-webhook.yourdomain.com/sns-endpoint" qa: # Keeping this as an example too Protocol: Email Endpoint: "qa-test@yourdomain.com"
To make deployment flexible, add a parameter that lets you specify which environment you're deploying to. This way, you can reuse the same template for all environments:
Parameters: TargetEnvironment: Type: String Description: The target environment to deploy to (stg, prod, qa) AllowedValues: - stg - prod - qa Default: stg # Set staging as default for convenience
Now, use !FindInMap to pull the correct protocol and endpoint from the mappings based on the TargetEnvironment parameter. We'll also make the TopicName environment-specific to avoid conflicts:
Resources: MySNSTopic: Type: AWS::SNS::Topic Properties: TopicName: !Sub "mytopic-${TargetEnvironment}" # Unique name per environment Subscription: - Protocol: !FindInMap [SNSEnvironmentConfig, !Ref TargetEnvironment, Protocol] Endpoint: !FindInMap [SNSEnvironmentConfig, !Ref TargetEnvironment, Endpoint] # Add a second subscription if needed, repeating the same pattern # - Protocol: !FindInMap [SNSEnvironmentConfig, !Ref TargetEnvironment, Protocol] # Endpoint: !FindInMap [SNSEnvironmentConfig, !Ref TargetEnvironment, AnotherEndpoint]
Once your template is ready, you can deploy it to each environment easily:
Using AWS CLI
For staging:
aws cloudformation deploy \ --template-file your-template.yaml \ --stack-name my-sns-topic-stg \ --parameter-overrides TargetEnvironment=stg \ --region your-region
For production:
aws cloudformation deploy \ --template-file your-template.yaml \ --stack-name my-sns-topic-prod \ --parameter-overrides TargetEnvironment=prod \ --region your-region
Using AWS Console
When launching the stack via the AWS CloudFormation console, you'll be prompted to select the TargetEnvironment from the allowed values—just pick the environment you want, and CloudFormation will handle the rest.
- Single Source of Truth: You only maintain one template for all environments, reducing duplication and errors.
- Clear Differentiation: Environment-specific values are centralized in the mappings, making it easy to update or add new environments later.
- Flexible Deployment: The parameter lets you switch environments with minimal changes during deployment.
内容的提问来源于stack exchange,提问作者Brian Huynh

