AWS SAM&参数存储:如何选择参数以部署到不同的环境中 [英] AWS SAM & Parameter Store: How to select parameter for the deployment into different environments

查看:88
本文介绍了AWS SAM&参数存储:如何选择参数以部署到不同的环境中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置,其中我将CodeCommit用作存储lambda函数的存储库,并使用AWS SAM部署和创建lambda函数来存储CodePipeline.

I have a setup where I am using CodeCommit as my repository to store lambda functions and CodePipeline using AWS SAM to deploy and create lambda functions.

我想将lambda函数部署到不同的环境中,例如QA,staging和Prod.我已经使用AWS参数存储来引用我的变量.

I would like to deploy the lambda functions into different environments such as QA, staging, and Prod. I have used the AWS Parameters store to reference my variables.

下面是我设置的template.yaml文件,该文件创建了一个lambda函数,它使用AWS参数存储引用了vairables

Below is my template.yaml file that I have set up that creates a lambda function and it uses AWS parameter store to reference the vairables

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Test    

Parameters:
  BucketName:
    Description: 'Required. Bucket Name'
    Type: 'AWS::SSM::Parameter::Value<String>'
    Default: 'MyBucketname' 

  CSVPath:
    Description: 'Required. Configkey Name'
    Type: 'AWS::SSM::Parameter::Value<String>'
    Default: 'MyCSVPath' 

Resources:
    GetOrdersFunction:
        Type: AWS::Serverless::Function 
        Properties:
            CodeUri: ./LambdaCode
            Handler: app.lambda_handler
            FunctionName: app
            Runtime: python3.6
            Description: 'staging'
            Environment: 
                Variables:
                    BucketName: !Ref BucketName
                    CSVPath: !Ref CSVPath
            Events:
              HelloWorld:
                    Type: Api
                    Properties:
                        Path: /orders
                        Method: get  

我能够在template.yaml中定义用于部署的变量,但是我不确定如何为不同的环境(prod或qa)定义它.

I am able to define variables in my template.yaml for deployment but I am not sure how I can define it for different environments (prod or qa).

管道触发时,应使用QA变量部署到QA环境,并使用将在AWS参数存储区中定义的prod变量部署到prod

When the pipeline triggers it should deploy to QA environment using QA variables and deploy to prod using prod variables which will be defined in AWS Parameter store

我应该在template.yaml文件中进行哪些更改以允许部署到不同的环境?

What changes should I make in my template.yaml file to enable deploying to different environments?

推荐答案

正如Meir所提到的,您可以在cloudformation中使用参数和条件功能来做到这一点,例如,您将添加一个参数部分,如下所示:

As Meir has mentioned, 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

现在,当您调用sam进行部署命令时,您需要定义要部署到的阶段.例如:

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

您完整的cloudformation模板应如下所示:

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

这篇关于AWS SAM&amp;参数存储:如何选择参数以部署到不同的环境中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆