gpt4 book ai didi

python - 带有 Lambda 自定义资源的 Cloudformation 错误 - Python

转载 作者:行者123 更新时间:2023-12-03 07:27:04 26 4
gpt4 key购买 nike

我一直在考虑通过使用 CloudFormation (CF) 的 Lambda 来开发一些自定义资源,并且一直在考虑使用自定义资源助手,但一开始一切正常,然后 CF 堆栈需要很长时间才能创建或删除。当我检查云监视日志时,我注意到在 Lambda 中运行创建或云函数后出现错误。

[7cfecd7b-69df-4408-ab12-a764a8bf674e][2021-02-07 12:41:12,535][ERROR] send(..) failed executing requests.put(..):Formatting field not found in record: 'requestid'

我注意到其他一些人也遇到了这个问题,但没有解决。我已经使用了下面链接中的通用代码,我的自定义代码可以工作并完成,但看起来像是将更新传递给 CF。我查看了 crhelper.py,我能找到的 'requestid' 的唯一引用是:

logfmt = '[%(requestid)s][%(asctime)s][%(levelname)s] %(message)s \n'
mainlogger.handlers[0].setFormatter(logging.Formatter(logfmt))
return logging.LoggerAdapter(mainlogger, {'requestid': event['RequestId']})

Reference

最佳答案

要了解您遇到的错误,我们需要查看您正在执行的操作的可重现代码示例。请考虑到,每次自定义资源操作出现某种错误时,可能需要很长时间才能完成,正如您所注意到的。

但是,对于您正在使用的原始自定义资源帮助程序,有一个很好的替代方案,并且根据我的经验,这非常有效,同时使代码保持更简单(由于良好的抽象级别)并遵循最佳实践。这是自定义资源帮助器框架,如 this 中所述AWS 博客。

您可以在 github here 上找到有关实现的更多详细信息。 .

基本上,在下载所需的依赖项并将其加载到您的 lambda 上(这取决于您处理自定义 lambda 依赖项的方式)后,您可以像这样管理自定义资源操作:

from crhelper import CfnResource
import logging

logger = logging.getLogger(__name__)
# Initialise the helper
helper = CfnResource()

try:
## put here your initial code for every operation
pass
except Exception as e:
helper.init_failure(e)


@helper.create
def create(event, context):
logger.info("Got Create")
print('Here we are creating some cool stuff')

@helper.update
def update(event, context):
logger.info("Got Update")
print('Here you update the things you want')

@helper.delete
def delete(event, context):
logger.info("Got Delete")
print('Here you handle the delete operation')


# this will call the defined function according the
# cloudformation operation that is running (create, update or delete)
def handler(event, context):
helper(event, context)

关于python - 带有 Lambda 自定义资源的 Cloudformation 错误 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66089284/

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