我正在创建一个cloudformation
模板,该模板会创建一个DynamoDB table
。我想将模板参数的值放入DynamoDB table
。要做到这一点,我创建了一个lambda函数,它的栈参数,并把它们在表中的项目如下:无法通过自定义的cloudformation资源调用lambda函数
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