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년 전2485회 조회
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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인