是否可以将 Lambda response_data
或 return
或任何其他变量值引用到 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/