amazon-web-services - 如何将 Lambda 响应数据引用到 CloudFormation 中的输出

标签 amazon-web-services aws-lambda aws-cloudformation

是否可以将 Lambda response_datareturn 或任何其他变量值引用到 CloudFormation 输出中?这样我就可以导出输出以用作跨堆栈引用。

Resources:
  EC2CustomResource:
    Type: Custom::EC2CustomResource
    Properties:
      ServiceToken: !GetAtt AWSLambdaFunction.Arn
        
  AWSLambdaFunction:
     Type: AWS::Lambda::Function
     Properties:
       Description: "bucket!"
       FunctionName: !Sub '${AWS::StackName}-${AWS::Region}-lambda'
       Handler: index.handler
       Timeout: 360
       Runtime: python3.9
       Code:
         ZipFile: |
           import boto3
           import cfnresponse   
           
           def handler(event, context):
           
               response_data = {}
               s3 = boto3.client('s3')
               
               # dummy test
               testList = [1, 2, 3]
               
               try:
                   print("Execution succesfull!")
                   response_data['testList'] = testList
                   cfnresponse.send(event, context, cfnresponse.SUCCESS, response_data)
               except Exception as e:
                   print("Execution failed...")
                   response_data['Data'] = str(e)
                   cfnresponse.send(event, context, cfnresponse.FAILED, response_data)

# This is not working
Outputs:
  LambdaFunctionOutput: 
    Value: !GetAtt AWSLambdaFunction.response_data['testList']
    Description: Return Value of Lambda Function

最佳答案

您指定的模板仅创建 Lambda 函数。它不会执行这个 lambda 函数。如果您想执行 lambda 函数并引用某些返回值,则必须使用 custom resource由 lambda 函数支持。

编辑#1

可以使用 cfn !GetAtt 函数检索您使用 cfnresponse 库定义和传递的响应数据,如 the documentation 中所述。 .

Data: Optional. The custom resource provider-defined name-value pairs to send with the response. You can access the values provided here by name in the template with Fn::GetAtt.

关于amazon-web-services - 如何将 Lambda 响应数据引用到 CloudFormation 中的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69630137/

相关文章:

java - 我想用 Java 代码在浏览器上打开链接或 URL

aws-lambda - AWS Lambda Authorizer `body` 对象中不存在 `event` 参数

amazon-web-services - 部署后带有计划事件的 Lambda 函数行为

amazon-web-services - 从 Cloudformation 设置 QueuePolicy 在堆栈创建时不会生效 - 但在创建后设置相同的策略会生效

amazon-web-services - 将未知大小的实例类型和权重容量列表添加到 AWS CF 模板中的 Auto Scaling 组 (YAML)

c++ - AWS 上的 GLIBCXX 版本错误

amazon-web-services - AWS GLUE 数据导入问题

amazon-web-services - 无法删除 AWS WAF 服务

javascript - 从字符串中删除货币符号并使用 Javascript 中的单行转换为数字

amazon-web-services - 如何将认知用户信息传递给 lambda?