簡體   English   中英

AWS SAM & Parameter Store:如何將 select 參數部署到不同的環境中

[英]AWS SAM & Parameter Store: How to select parameter for the deployment into different environments

我有一個設置,我使用 CodeCommit 作為存儲庫來存儲 lambda 函數,並使用 AWS SAM 來部署和創建 lambda 函數的 CodePipeline。

我想將 lambda 函數部署到不同的環境中,例如 QA、staging 和 Prod。 我已使用 AWS 參數存儲來引用我的變量。

下面是我設置的 template.yaml 文件,它創建了一個 lambda function,它使用 AWS 參數存儲來引用變量

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)定義它。

當管道觸發時,它應該使用 QA 變量部署到 QA 環境,並使用將在 AWS 參數存儲中定義的 prod 變量部署到 prod

我應該在我的 template.yaml 文件中進行哪些更改才能部署到不同的環境?

正如 Meir 所提到的,您可以使用 cloudformation 中的參數和條件功能來執行此操作,例如,您將添加一個參數部分,如下所示:

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

然后是帶有 map 的映射部分,用於保存所有階段的環境變量

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

那么您的 function 可以根據您所處的環境使用變量:

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 to deploy 命令時,您需要定義要部署到的階段。 前任:

sam deploy --parameter-overrides Stage=prod

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

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

您正在尋找映射FindInMap功能。 查看示例並在 RegionMap 和 EnvMap 之間切換,它將非常適合您的用例。

這里的期望是根據給定的參數(參數)將采用一組變量,這正是映射所做的。

可以通過傳遞 parameterOverrides 從代碼管道覆蓋 CloudFormation 模板默認參數

從控制台:

  • 添加/編輯操作

  • 動作提供者 Cloudformation

  • 動作模式

  • 創建/更新堆棧

  • 高級 -> 參數覆蓋

    {"BucketName":"/BUCKETNAME/DEV","CSVPath":"/CSVPATH/DEV"}

如果代碼管道是從 Cloudformation 模板創建的:

                  - Name: DeployMyStack
                    InputArtifacts:
                        - Name: Artifacts
                    ActionTypeId:
                        Category: Deploy
                        Owner: AWS
                        Version: 1
                        Provider: CloudFormation
                    Configuration:
                        ActionMode: 'CREATE_UPDATE'
                        Capabilities: CAPABILITY_NAMED_IAM,CAPABILITY_AUTO_EXPAND
                        ParameterOverrides: !Sub
                            - '{"BucketName":"/BUCKETNAME/${Env}","CSVPath":"/CSVPATH/${Env}'
                            - {
                                  Env: !Ref Environment,
                              }
                        RoleArn: !Ref CloudFormationServiceRoleArn
                        StackName: !Sub
                            - Gl${ENV}-My-Stack
                            - { ENV: !Ref Environment }
                        TemplatePath: 'Artifacts::MyTemplate.yaml'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM