Lambda支持的自定义资源cf模板返回'CREATE_FAILED'

时间:2018-05-29 06:43:42

标签: aws-lambda amazon-cloudformation

下面的lambda函数是将SNS主题与现有目录相关联,然后是一个自定义资源来调用lambda func本身。我看到lambda创建成功,'Register_event_topic'也完成了。然而,堆栈在一段时间后失败主要是因为“自定义资源未能在预期时间内稳定”;如何确保堆栈不会出错?

AWSTemplateFormatVersion: '2010-09-09'
    #creating lambda function to register_event_topic
    Description: Lambda function to register event topic with existing directory ID
    Parameters:
      RoleName:
        Type: String
        Description: "IAM Role used for Lambda execution"
        Default: "arn:aws:iam::<<Accountnumber>>:role/LambdaExecutionRole"
      EnvVariable:
        Type: String
        Description: "The Environment variable set for the lambda func"
        Default: "ESdirsvcSNS"
    Resources:
      REGISTEREVENTTOPIC:
        Type: 'AWS::Lambda::Function'
        Properties:
          FunctionName: dirsvc_snstopic_lambda
          Handler: index.lambda_handler
          Runtime: python3.6
          Description: Lambda func code to assoc dirID with created SNS topic
          Code:
            ZipFile: |
              import boto3
              import os
              import logging
              dsclient = boto3.client('ds')
              def lambda_handler(event, context):
                response = dsclient.describe_directories()
                directoryList = []
                print(response)
                for directoryList in response['DirectoryDescriptions']:
                    listTopics = dsclient.describe_event_topics(
                      DirectoryId=directoryList['DirectoryId']
                    )
                    eventTopics = listTopics['EventTopics']
                    topiclength = len(eventTopics)
                    if topiclength == 0:
                      response = dsclient.register_event_topic(
                          DirectoryId=directoryList['DirectoryId'],
                          TopicName= (os.environ['MONITORING_TOPIC_NAME'])
                      )  
                    print(listTopics)
          Timeout: 60
          Environment:
            Variables:
              MONITORING_TOPIC_NAME: !Ref EnvVariable
          Role: !Ref RoleName

      InvokeLambda:
        Type: Custom::InvokeLambda
        Properties:
          ServiceToken: !GetAtt REGISTEREVENTTOPIC.Arn
          ReservedConcurrentExecutions: 1

2 个答案:

答案 0 :(得分:2)

唉,编写自定义资源并不像你最初想的那么简单。相反,必须将特殊代码添加到将回复发布回网址

您可以在Walkthrough: Looking Up Amazon Machine Image IDs - AWS CloudFormation

上提供的示例Zip文件中看到这一点

来自Custom Resources - AWS CloudFormation文档:

  

自定义资源提供程序处理AWS CloudFormation请求,并向预先签名的URL返回SUCCESSFAILED的响应。自定义资源提供程序以JSON格式的文件提供响应,并将其上载到预先签名的S3 URL。

这是由于CloudFormation的异步行为。它并不是简单地调用Lambda函数然后等待响应。相反,它会触发Lambda函数,函数必须回调并触发CloudFormation中的下一步。

答案 1 :(得分:2)

您的lambda不支持自定义资源生命周期

  

在Lambda支持的自定义资源中,您可以实现逻辑   支持创建,更新和删除资源。这些   指示通过事件从CloudFormation发送并提供给您   有关堆栈过程的信息。

此外,您还应将状态恢复为CloudFormation

  

CloudFormation希望在您完成逻辑后获得Lambda函数的响应。如果没有得到响应,或者至少在达到1小时(!)超时之前,它将不会继续部署过程。它可能会花费你很多时间和挫折。

您可以阅读更多here