AWS Lambda字符串插值不起作用

时间:2018-08-21 12:16:06

标签: aws-lambda string-interpolation aws-sam

因此,我试图超越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}之间有什么区别

1 个答案:

答案 0 :(得分:2)

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

更多文档,

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

  

!子   HelloLambda-$ {环境}

采用环境变量并替换为指定的值,因此您将基于环境变量获得不同的功能。