Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

amazon web services - How to deploy to multiple environments using CodeCommit and Code Pipeline


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use parameters and condition functionality in cloudformation to do that, for example, you will add a parameter section as follow:

Parameters:
  Stage:
    Type: String
    Default: staging
    Description: Parameter for getting the deployment stage

then a mapping section with a map to hold the environment variables for all your stages

Mappings:
  StagesMap:
    staging:
      CONFIG_BUCKET: staging-bucket-name
      CONFIG_KEY: source-data-key-path
    prod:
      CONFIG_BUCKET: prod-bucket-name
      CONFIG_KEY: source-data-key-path

then your function can use the variables based on which environment you are in:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: CD Demo Lambda
Resources:
  CDDemoLambda:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: lambda_function.lambda_handler
      Runtime: python3.6
      CodeUri: ./LambdaCode
      FunctionName: ApigatewayLambda
      AutoPublishAlias: ApiLambda
      Description: 'Lambda function validation'
      MemorySize: 128
      Timeout: 30
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /getazs
            Method: get
      Environment:
        Variables:
          CONFIG_BUCKET: !FindInMap
            - StagesMap
            - Ref: Stage
            - CONFIG_BUCKET
          CONFIG_KEY: !FindInMap
            - StagesMap
            - Ref: Stage
            - CONFIG_KEY

Now when you are calling your sam to deploy command, you need to define which stage you are deploying to. ex:

sam deploy --parameter-overrides Stage=prod

Your complete cloudformation template should look like this:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: CD Demo Lambda

Parameters:
  Stage:
    Type: String
    Default: staging
    Description: Parameter for getting the deployment stage

Mappings:
  StagesMap:
    staging:
      CONFIG_BUCKET: staging-bucket-name
      CONFIG_KEY: source-data-key-path
    prod:
      CONFIG_BUCKET: prod-bucket-name
      CONFIG_KEY: source-data-key-path


Resources:
  CDDemoLambda:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: lambda_function.lambda_handler
      Runtime: python3.6
      CodeUri: ./LambdaCode
      FunctionName: ApigatewayLambda
      AutoPublishAlias: ApiLambda
      Description: 'Lambda function validation'
      MemorySize: 128
      Timeout: 30
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /getazs
            Method: get
      Environment:
        Variables:
          CONFIG_BUCKET: !FindInMap
            - StagesMap
            - Ref: Stage
            - CONFIG_BUCKET
          CONFIG_KEY: !FindInMap
            - StagesMap
            - Ref: Stage
            - CONFIG_KEY

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...