如何使用 CodeCommit 和 Code Pipeline 部署到多个环境
How to deploy to multiple environments using CodeCommit and Code Pipeline
我使用 CodeCommit 作为我的代码存储库,因为代码提交使您能够将代码部署到另一个环境中的跨账户。我在 template.yaml 使用 AWS SAM 的 QA 环境中设置了一个 lambda 函数。
我应该在代码管道的什么地方定义环境变量,以便 lambda 函数可以部署在另一个帐户的 Prod 环境中?
我将如何定义变量,以便在 prod 环境中合并暂存 lambda 函数时它采用 prod 环境变量?
因为在合并代码时在 prod 环境中定义暂存环境变量没有任何意义。
是否会在代码构建中定义环境变量?
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: staging-bucket-name
CONFIG_KEY: source-data-key-path
您可以使用 cloudformation 中的参数和条件功能来执行此操作,例如,您将添加如下参数部分:
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
那么您的函数可以根据您所处的环境使用变量:
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 部署命令时,您需要定义要部署到哪个阶段。
例如:
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
我使用 CodeCommit 作为我的代码存储库,因为代码提交使您能够将代码部署到另一个环境中的跨账户。我在 template.yaml 使用 AWS SAM 的 QA 环境中设置了一个 lambda 函数。
我应该在代码管道的什么地方定义环境变量,以便 lambda 函数可以部署在另一个帐户的 Prod 环境中?
我将如何定义变量,以便在 prod 环境中合并暂存 lambda 函数时它采用 prod 环境变量?
因为在合并代码时在 prod 环境中定义暂存环境变量没有任何意义。
是否会在代码构建中定义环境变量?
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: staging-bucket-name
CONFIG_KEY: source-data-key-path
您可以使用 cloudformation 中的参数和条件功能来执行此操作,例如,您将添加如下参数部分:
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
那么您的函数可以根据您所处的环境使用变量:
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 部署命令时,您需要定义要部署到哪个阶段。 例如:
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