【发布时间】:2022-01-23 07:28:02
【问题描述】:
我正在尝试在 CloudFormation 中创建自定义资源来标记事件规则。 这是 lambda:
from json import dumps
import sys
import traceback
import urllib.request
import boto3
def send_response(event, context, response):
"""Send a response to CloudFormation to handle the custom resource lifecycle"""
response_body = {
'Status': response,
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId'],
}
print('RESPONSE BODY: \n' + dumps(response_body))
data = dumps(response_body).encode('utf-8')
req = urllib.request.Request(
event['ResponseURL'],
data,
headers={'Content-Length': len(data), 'Content-Type': ''})
req.get_method = lambda: 'PUT'
try:
with urllib.request.urlopen(req) as resp:
print(f'response.status: {resp.status}, ' +
f'response.reason: {resp.reason}')
print('response from cfn: ' + resp.read().decode('utf-8'))
except Exception as e:
print(e)
raise Exception('Received non-200 response while sending response to AWS CloudFormation')
return True
def custom_resource_handler(event, context):
print("Event JSON: \n" + dumps(event))
ResourceARN = event['ResourceProperties']['ResourceARN']
tags = event['ResourceProperties']['Tags']
response = 'FAILED'
client = boto3.client('events')
if event['RequestType'] == 'Create':
try:
response = client.tag_resource(
ResourceARN=ResourceARN,
Tags=tags)
response = 'SUCCESS'
except Exception as e:
print(e)
send_response(event, context, response)
return
if event['RequestType'] == 'Update':
# Do nothing and send a success immediately
send_response(event, context, response)
return
if event['RequestType'] == 'Delete':
try:
response = client.untag_resource(
ResourceARN = ResourceARN,
TagKeys = tags['Key']
)
response = 'SUCCESS'
except Exception as e:
print(e)
send_response(event, context, response)
def lambda_handler(event, context):
"""Lambda handler for the custom resource"""
try:
return custom_resource_handler(event, context)
except Exception as e:
print(e)
raise
这是 CFN 块:
CustomTagEvent:
Type: Custom::TagEventRule
Version: "1.0"
DependsOn: EventRule
Properties:
ServiceToken: "LAMBDA_ARN"
ResourceARN:
Fn::GetAtt:
- "EventRule"
- "Arn"
Tags:
-
Key: Name
创建 CLoudFormation 时出现错误“CREATE FAILED”。
“无效的 PhysicalResourceId”
但是,不知何故设法创建了标签。 需要帮助了解为什么它在创建标签时会出现 CloudFormation 错误?
【问题讨论】:
-
您检查日志是否有任何来自 lambda 的错误?
标签: python-3.x aws-lambda amazon-cloudformation aws-event-bridge aws-cloudformation-custom-resource