您可以使用以下代码示例来在AWS Lambda函数中读取CloudFormation输出:
import boto3
def lambda_handler(event, context):
# 创建 CloudFormation 客户端
cf_client = boto3.client('cloudformation')
# 获取 CloudFormation 输出的值
response = cf_client.describe_stacks(StackName='your-stack-name')
outputs = response['Stacks'][0]['Outputs']
# 遍历输出并打印值
for output in outputs:
key = output['OutputKey']
value = output['OutputValue']
print(f'{key}: {value}')
# 在这里添加您的逻辑代码
return {
'statusCode': 200,
'body': 'Success'
}
在上面的示例中,我们首先创建了一个 CloudFormation 客户端对象(cf_client
)。然后,我们使用 describe_stacks
方法来获取指定堆栈(StackName
参数)的详细信息,包括其输出(outputs
)。我们遍历输出列表并打印输出键和值。
在上面的示例中,您需要将 your-stack-name
替换为您的 CloudFormation 堆栈的名称。
您可以根据您的需求在 lambda_handler
函数中添加任何其他逻辑代码。最后,我们返回一个包含状态码和响应体的字典。