AWS Lambda 字符串插值不起作用

AWS Lambda String interpolation not working

所以我试图超越 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

!Sub HelloLambda-${Environment}

获取环境变量并替换为指定的值,因此您可以根据环境变量获得不同的功能。