AWS Lambda 字符串插值不起作用



所以我试图覆盖lambda环境,我使用了字符串插值,但是有一件小事情我无法理解,所以基本上以下是我的Lambda,如果你看到函数名称,它有一个环境占位符。但是当我像这样部署它时

aws cloudformation deploy --template-file build/output.yaml --stack-name test-stack --capabilities CAPABILITY_IAM --parameter-overrides Environment=de
v

占位符不会更新以下代码

Parameters:
Environment:
Type: String
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: src
Handler: index.lambda_handler
Runtime: python3.6
FunctionName: HelloLambda-${Environment}
MemorySize: 128
Timeout: 30
Policies:
- AWSLambdaBasicExecutionRole 

但是如果我像这样做同样的事情 参数: 环境: 类型:字符串

资源:

HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: src
Handler: index.lambda_handler
Runtime: python3.6
FunctionName: !Sub HelloLambda-${Environment}
MemorySize: 128
Timeout: 30
Policies:
- AWSLambdaBasicExecutionRole

上面的执行有效,那么FunctionName: !Sub HelloLambda-${Environment}FunctionName: HelloLambda-${Environment}有什么区别

通过在前面放置!Sub,您正在调用具有云形成的子函数。它采用模板参数并在需要时应用替换。

更多文档在,

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html

!子 HelloLambda-${Environment}

采用环境变量并替换为指定的值,因此您可以根据环境变量获得不同的函数。

最新更新