How to create an EventBridge application in python

aws
Reading Time: 3 minutes

Hello Readers!!! In this article, we will see how we can create an eventbridge application in python. First we will see create a python application that will send custom events to eventBridge. Then if rule matches, target will be invoked which would be lambda function in our case. And lambda will prints the event details in the cloud watch.

Create an eventbridge project :

1.First we will open any code editor and create an eventbridge application.

import boto3

client = boto3.client('events',
                      region_name='us-east-1',
                      aws_access_key_id= '',
                      aws_secret_access_key='')
                      


                    


detailJsonString = '{"username":"neha","city":"Delhi",age:"24"}'

response = client.put_events(
    Entries=[
        {
            'Source':'user-event',
            'DetailType':'user-preferences',
            'Detail':'detailJsonString',
            'EventBusName':'my-event-bus'

        }
    ]
)

print(response)

Create Lambda Function:

1.We will create one lambda function named as event-bridge-lambda and in runtime we will choose python. Then click on create function.

2. Next we will go to the python code.

So this is our lambda code and we will deploy this lambda function.

Create Event Bus in EventBridge:

1. We can go to eventbridge and click on event bus.

2. We will create one event bus and named as my-event-bus and click on create.

Create Rule:

  1. We will select event bus from the drop down menu i.e. my-event-bus.

2. We will see that we don’t have any rule inside our custom event bus so click on create rule.

3. Now we will create a rule named as my-event-rule.

4. Then we will select rule with an event pattern in define pattern.

5. In event pattern, we will write one custom patterns in json editor.

6. Next in target, we will select a target from the drop down menu. Here we will select lambda function and in function we will select event-bridge-lambda from the drop down menu.

7. Next we will click on review and create and click on create rule.

8. Now we will see our rule is successfully created.

Running the application:

We will execute the below command to run the application.

python3 eventBridge.py

Conclusion:

In this article, we saw how we can create an EventBridge application in python.

Written by 

I am reliable, hard-working with strong attention to detail and eager to learn about new technologies and business issues. I am able to work well both on my own initiative and as part of a team.