[英]How to use AWS EventBridge with Python Lambda and Boto?
我在 AWS 上玩了一会儿,最后来到了 AWS EventBridge。 我试图写一个 Lambda 来测试和理解,但刚开始时出错。
import json, boto3
def lambda_handler(event, context):
client = boto3.client('events')
response = client.create_event_bus(
Name='TestEventBus',
EventSourceName='SomeSoucreEvent'
)
return {
'statusCode': 200,
'body': json.dumps(response)
}
我收到以下错误消息:
响应:{ "errorMessage": "'CloudWatchEvents' 对象没有属性 'create_event_bus'", "errorType": "AttributeError", "stackTrace": [ " File \\"/var/task/lambda_function.py\\", line 5在lambda_handler \\ N响应= client.create_event_bus(\\ n “ ”文件\\“的/ var /运行/ botocore / client.py \\”,线路563,在GETATTR \\ n自。类。名,项目)\\ n”个] }
在 Lambda 中运行的 Boto 版本是 1.9.42
首先,用你的代码
client = boto3.client('events')
response = client.create_event_bus(
您正在访问 CloudWatchEvents,而不是 AWS EventBridge(如错误消息所述 - 原因如下所述)。
"CloudWatchEvents' object has no attribute 'create_event_bus'"
此外,EventBridge 在 Python 和 JS lambda 环境中尚不可用(通过默认包)。 我假设 AWS 没有在 lambda 中运行的最新版本的 SDK,您可以将自己的代码作为捆绑包上传或使用 lambda 层并安装您的自定义 NPM 依赖项(假设您将其作为 JS 运行)。 这样您就可以安装最新的 AWS 开发工具包版本 (NPM) 并使用 EventBridge(EventBridge 于一个月前发布)。
此外,您的文档链接https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/events.html#eventbridge指出它是(截至目前)版本 1.9.205(另请参阅 URL 中的 /api/latest/)。
如果您打开 1.9.42 的文档,即您添加到报告中的版本(我假设您是从 lambda 中得到的),通过此 URL, https: //boto3.amazonaws.com/v1/documentation/api/ 1.9.42/reference/services/events.html#eventbridge ,您会看到1.9.42版本没有实现EventBridge 。
它仅具有 CloudWatchEvents 功能,这就是为什么您只能使用 boto3 访问 EventBridge(截至目前)。 您必须等到 AWS 更新 lambda 上的 boto3 版本,否则您必须手动部署您的包并运行它。
刚刚检查,boto3 现在有一个 EventBridge 客户端。
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/events.html
import boto3
client = boto3.client('events')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.