如何在云形成模板中根据阶段设置环境变量



lambda处理程序中的环境变量必须根据阶段通过lambda处理函数进行设置。架构、端点的值对于不同阶段是不同的。如何通过yml模板做到这一点。我是新手,所以不知道该怎么做。

Parameters:
Stage: {Type: String, Default: ''}
Resources:
LambdaHandler:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
......
......

如何继续?

template.yaml:

Parameters:
Environment:
AllowedValues:
- dev
- prod
Type: String
Resources:
myLambda:
Type: AWS::Lambda::Function
Properties:
Environment:
Variables:
stage: !Ref Environment

你的外壳:

$ aws cloudformation deploy --parameter-overrides Environment=dev

假设你想要一个以环境为条件的变量:

Parameters:
Environment:
AllowedValues:
- dev
- prod
Type: String
Mappings:
Environments:
dev:
LogLevel: "DEBUG"
prod:
LogLevel: "ERROR"
Resources:
myLambda:
Type: AWS::Lambda::Function
Properties:
Environment:
Variables:
LOG_LEVEL: !FindInMap [Environments, !Ref Environment, LogLevel]

我想应该是:

Environment:
Variables:
your-key: !Ref Stage

您可以使用以下条件

MyNotCondition:
!Not [!Equals [!Ref EnvironmentType, prod]]

看看这篇关于如何使用阶段参数和使用映射来设置环境变量的文章https://www.singlestoneconsulting.com/blog/cloudformation-mapping-and-conditionals-making-your-templates-more-universal/

最新更新