How to use AWS EventBridge with Python Lambda and Boto?

0

I played around with AWS a little bit and ended up at AWS EventBridge. I tried to write a Lambda for testing and understanding, but got an error just at the beginning.

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)
    }

I get the following error message:

Response: { "errorMessage": "'CloudWatchEvents' object has no attribute 'create_event_bus'", "errorType": "AttributeError", "stackTrace": [ " File \"/var/task/lambda_function.py\", line 5, in lambda_handler\n response = client.create_event_bus(\n", " File \"/var/runtime/botocore/client.py\", line 563, in getattr\n self.class.name, item)\n" ] }

The version of Boto running in the Lambda is 1.9.42

已提問 5 年前檢視次數 2534 次
1 個回答
1

First of all, with your code

client = boto3.client('events')
response = client.create_event_bus(

you are accessing the CloudWatchEvents, not AWS EventBridge (as the error message states - reason why is stated below).

"CloudWatchEvents' object has no attribute 'create_event_bus'"

Moreover, EventBridge isn't available in the Python and JS lambda environment yet (via default packages). I assume AWS has not the latest version of their SDK running in lambda, you could upload your own code as a bundle bundle or work with lambda layers and install your custom NPM dependencies (assuming you will run it as JS). That way you could install the latest SDK version of AWS (NPM) and use EventBridge (EventBridge was announced one month ago).

Furthermore if you take a look a the boto3 documentation https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/events.html#eventbridge, it states that it is (as of now) version 1.9.205 (see also the /api/latest/ in the URL).

If you open up the documentation for 1.9.42, the version you added to your report (I assume you got this out of the lambda), via this URL, https://boto3.amazonaws.com/v1/documentation/api/1.9.42/reference/services/events.html#eventbridge , you will see that the version 1.9.42 does not have EventBridge implemented.

It only features CloudWatchEvents and that's why you can only access EventBridge with boto3 (as of now). You have to wait until AWS updates the boto3 version on lambda's or you have to manually deploy your bundle and run it.

Edited by: MartinJK on Aug 12, 2019 10:06 AM

已回答 5 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南