【发布时间】:2017-07-23 08:30:52
【问题描述】:
我正在创建一个cloudformation 模板,该模板创建一个DynamoDB table。我想将模板的参数值放在DynamoDB table 中。为此,我创建了一个 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 中?
当您将 Lambda 函数与自定义资源相关联时, 每当创建、更新自定义资源时调用函数, 或删除
【问题讨论】:
标签: python json amazon-web-services amazon-cloudformation