aws-lambda - AWS Lambda 字符串插值不起作用

标签 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}有什么区别

最佳答案

通过在前面添加!Sub,您可以调用带有云形成的子函数。它采用模板参数并在需要时应用替换。

更多文档位于,

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

!Sub HelloLambda-${Environment}

获取环境变量并替换为指定的值,从而根据环境变量获得不同的功能。

关于aws-lambda - AWS Lambda 字符串插值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51948484/

相关文章:

node.js - 无服务器 502 网关错误

c# - Visual Studio 2015 : Invalid "Cast is redundant" warning in interpolated string expression

python - 如何将字符串转换为 f 字符串?

amazon-web-services - sam 打包并部署,无需打包参数文件

amazon-web-services - AWS网关响应未传回401未经授权的授权者上下文

javascript - AWS Lambda 到 AWS Lambda 权限

amazon-web-services - 指定哪个帐户/用户可以使用 SAM 和 API 网关调用 API

amazon-web-services - AWS Kinesis,具有保证排序的并发 Lambda 处理

Swift 中的字符串插值

amazon-web-services - 如何从 AWS SAM 启动 lambda 函数并将自定义 AWS IAM 角色分配给 lambda