无法通过自定义 cloudformation 资源调用 lambda 函数
Can't invoke lambda function by a custom cloudformation resource
我正在创建一个 cloudformation
模板,它会创建一个 DynamoDB table
。我想将模板参数的值放在 DynamoDB table
内。为此,我创建了一个 lambda 函数,它接受堆栈参数并将它们放在 table 项中,如下所示:
import boto3
def lambda_handler(event, context):
parameters = {}
outputs = {}
cf_client = boto3.client('cloudformation')
dynamodb = boto3.resource('dynamodb')
# Get the name of the stack cloudformation
stack_name = context.invoked_function_arn.split(':')[6].rsplit('-', 2)[0]
response = cf_client.describe_stacks(StackName=stack_name)
# Get the outputs of the stack
for r in response['Stacks'][0]['Outputs']:
outputs[r['OutputKey']] = r['OutputValue']
policy_table_name = outputs['PolicyDDBTableName']
# Get the parametres of the stack
for e in response['Stacks'][0]['Parameters']:
parameters[e['ParameterKey']] = e['ParameterValue']
DefaultRetentionDays = parameters['DefaultRetentionDays']
CustomTagName = parameters['CustomTagName']
AutoSnapshotDeletion = parameters['AutoSnapshotDeletion']
response = dynamodb.put_item(
TableName=policy_table_name,
Item={'SolutionName': {'S': 'EbsSnapshotScheduler'},
'DefaultRetentionDays': {'S': DefaultRetentionDays},
'CustomTagName': {'S': CustomTagName},
'AutoSnapshotDeletion': {'S': AutoSnapshotDeletion}
})
然后在模板cloudformation
中,我创建了一个custom resource
来调用函数:
"PutInDB" : {
"Type" : "Custom::customhelper",
"Properties" : {
"ServiceToken": { "Fn::GetAtt" : ["FunctionHelper" , "Arn"] },
"StackName": {"Ref": "AWS::StackName" }
}
},
函数及其角色也在同一堆栈中创建。
当我创建堆栈时,custom resource
挂起并且创建失败并出现错误:
Custom Resource failed to stabilize in expected time
。我在这里错过了什么吗?
我怎样才能成功创建 custom resource
并调用该函数,以便将堆栈的参数插入到 DynamoDB table
中?
When you associate a Lambda function with a custom resource, the
function is invoked whenever the custom resource is created, updated,
or deleted
您是在同一个 CF 模板中创建 lambda 函数吗?
我没有仔细看,但我的初步印象是 lambda 函数没有完成让 cloudformation 知道它已完成创建的要求。
关键是CF的"response.SUCCESS"响应还没有发回给CF。 CF 将创建 lambda 函数,但它需要知道它是否成功。
这就是你在 node.js 中的做法,我不知道 python 的语法。
response.send(event, context, response.SUCCESS, responseData);
我正在创建一个 cloudformation
模板,它会创建一个 DynamoDB table
。我想将模板参数的值放在 DynamoDB table
内。为此,我创建了一个 lambda 函数,它接受堆栈参数并将它们放在 table 项中,如下所示:
import boto3
def lambda_handler(event, context):
parameters = {}
outputs = {}
cf_client = boto3.client('cloudformation')
dynamodb = boto3.resource('dynamodb')
# Get the name of the stack cloudformation
stack_name = context.invoked_function_arn.split(':')[6].rsplit('-', 2)[0]
response = cf_client.describe_stacks(StackName=stack_name)
# Get the outputs of the stack
for r in response['Stacks'][0]['Outputs']:
outputs[r['OutputKey']] = r['OutputValue']
policy_table_name = outputs['PolicyDDBTableName']
# Get the parametres of the stack
for e in response['Stacks'][0]['Parameters']:
parameters[e['ParameterKey']] = e['ParameterValue']
DefaultRetentionDays = parameters['DefaultRetentionDays']
CustomTagName = parameters['CustomTagName']
AutoSnapshotDeletion = parameters['AutoSnapshotDeletion']
response = dynamodb.put_item(
TableName=policy_table_name,
Item={'SolutionName': {'S': 'EbsSnapshotScheduler'},
'DefaultRetentionDays': {'S': DefaultRetentionDays},
'CustomTagName': {'S': CustomTagName},
'AutoSnapshotDeletion': {'S': AutoSnapshotDeletion}
})
然后在模板cloudformation
中,我创建了一个custom resource
来调用函数:
"PutInDB" : {
"Type" : "Custom::customhelper",
"Properties" : {
"ServiceToken": { "Fn::GetAtt" : ["FunctionHelper" , "Arn"] },
"StackName": {"Ref": "AWS::StackName" }
}
},
函数及其角色也在同一堆栈中创建。
当我创建堆栈时,custom resource
挂起并且创建失败并出现错误:
Custom Resource failed to stabilize in expected time
。我在这里错过了什么吗?
我怎样才能成功创建 custom resource
并调用该函数,以便将堆栈的参数插入到 DynamoDB table
中?
When you associate a Lambda function with a custom resource, the function is invoked whenever the custom resource is created, updated, or deleted
您是在同一个 CF 模板中创建 lambda 函数吗?
我没有仔细看,但我的初步印象是 lambda 函数没有完成让 cloudformation 知道它已完成创建的要求。
关键是CF的"response.SUCCESS"响应还没有发回给CF。 CF 将创建 lambda 函数,但它需要知道它是否成功。
这就是你在 node.js 中的做法,我不知道 python 的语法。
response.send(event, context, response.SUCCESS, responseData);