Manage stage and prod environments in AWS SAM/Cloudformation template along with CI/CD support

Viewed 1402

I'm having an AWS SAM template file with some resources hosted on github, a codepipeline has been setted up to detect changes in the repo then create/update and execute changes on cloudformation stack. Everything is working fine. But now I need to configure stage and prod environments in the same template. I'm finding it difficult how to do it properly.

Different approaches are welcomed as well.

2 Answers

Are PROD and STAGE in the same account, or different accounts? I will assume same

Transform: AWS::Serverless-2016-10-31

Parameters:
  Environment:
    Type: String
    AllowedValues:
      - STAGE
      - PROD

Resources:
  MyLambda:
    Type: AWS::Serverless::Function
    Properties:
      Handler: lambda_function.lambda_handler
      FunctionName: !Sub ${Environment}_my_lambda
      CodeUri: my_lambda

This would give a unique name to your lambda, by environment

Then when you deploy your template, use --parameter-overrides=Environment=STAGE or --parameter-overrides=Environment=PROD

You can setup CloudWatch to listen to CodeCommit. If STAGE branch changes, call CodeBuild to use the STAGE branch, and call CloudFormation w the STAGE param. Same for PROD

Parameters would be best

You could also use Mappings or Conditions. But either of those could get messy

Related