2017-03-03 99 views
2

我正在创建一个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

AWS Documentation

When you associate a Lambda function with a custom resource, the function is invoked whenever the custom resource is created, updated, or deleted

回答

0

你创建在同一CF模板lambda函数?

我没有仔细看过这个,但我最初的印象是,lambda函数没有完成让cloudformation知道它已经完成创建的要求。

关键在于CF“response.SUCCESS”响应没有被发送回CF. CF将创建lambda函数,但它需要知道它是成功的。

这是你如何做node.js,我不知道Python的语法。

response.send(event, context, response.SUCCESS, responseData); 

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html

相关问题