如何将Lambda响应数据引用到CloudFormation中的Output



是否可以将Lambdaresponse_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函数并引用一些返回值,你将不得不使用一个由lambda函数支持的自定义资源。

编辑# 1

您使用cfnresponse库定义和传递的响应数据,可以使用文档中所述的cfn!GetAtt函数进行检索。

数据:可选的。随响应一起发送的自定义资源提供程序定义的名称-值对。您可以使用Fn::GetAtt在模板中通过名称访问这里提供的值。

最新更新