gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:22:38 25 4
gpt4 key购买 nike

下面的 lambda 函数是将 SNS 主题关联到现有目录,然后是自定义资源来调用 lambda 函数本身。我看到 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

最佳答案

唉,编写自定义资源并不像您最初想象的那么简单。相反,必须添加特殊代码来将响应发布回 URL

您可以在以下位置提供的示例 Zip 文件中看到这一点:Walkthrough: Looking Up Amazon Machine Image IDs - AWS CloudFormation

来自Custom Resources - AWS CloudFormation文档:

The custom resource provider processes the AWS CloudFormation request and returns a response of SUCCESS or FAILED to the pre-signed URL. The custom resource provider provides the response in a JSON-formatted file and uploads it to the pre-signed S3 URL.

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

关于aws-lambda - Lambda 支持的自定义资源 cf 模板返回 'CREATE_FAILED',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50577887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com