【问题标题】:Can't invoke lambda function by a custom cloudformation resource无法通过自定义 cloudformation 资源调用 lambda 函数
【发布时间】: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 中?

来自AWS Documentation

当您将 Lambda 函数与自定义资源相关联时, 每当创建、更新自定义资源时调用函数, 或删除

【问题讨论】:

    标签: python json amazon-web-services amazon-cloudformation


    【解决方案1】:

    您是否在同一个 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

    【讨论】:

      猜你喜欢
      • 2021-05-11
      • 2016-06-09
      • 1970-01-01
      • 2019-08-02
      • 2021-04-01
      • 2020-02-13
      • 2018-03-10
      • 2016-09-03
      • 2018-03-02
      相关资源
      最近更新 更多