I am back to blogging after long time.. This time I have switched my technology from PCF (VMware Tanzu) to AWS. Although a different technology I am back to event driven architecture 🙂
Usecase
I need to publish an event message called OrderPlaced. Order Fulfillment Lambda and Email Notificaton Lambda will act on the published message.
Lambda Setup
Lets start with Order Fulfillment and Email Notification Lambda.
Login to your AWS console and search for Lambda. Create two lambda functions called
order-fulfillment
email-notification
Following is the source code for order-fulfillment written in python
import json
print('Loading function')
def lambda_handler(event, context):
print(f'Order Id recieved at order fulfillment lambda as {event["detail"]["detail"]["orderId"]}')
return event
Following is the source code for email-notification lambda.
import json
print('Loading function')
def lambda_handler(event, context):
print(f'Order Id recieved at email notification service as {event["detail"]["detail"]["orderId"]}')
return event
Once you deploy the lambda lets now go setup event bridge.
Event Bridge Setup
After logging into AWS console, search for event bridge you will see the below screen.
We need following components of Event Bridge to make the pub sub work.
- Event Bus
- Rules
Lets create Event Bus
Event Bus Setup
Click on the Event buses item from left navigation menu. Click on “Create event bus”. Then type in the name of bus. I will go with “order-event-bus”. Click create to get the bus ready.
Rules setup
Now lets set the rules on the created event bus. Click on “Rules” item from left hand navigation.
Click on “Create rule” button. Then enter the details as shown below and click next.
In the next screen leave the defaults on “Event source” section, “Sample event”.
On “Creation method” section, select “Custom pattern”. In the “Event Pattern” section, enter the following pattern and click “Next”
{
"source": ["com.nitin.technically"]
}
Now choose Target 1 Lambda as shown below
Click “Add another target”. Select email-notification lambda as shown below. Click “Next”
Again in the Tags screen click “Next”. You will see the summary screen.
Click “Create rule”. You will see the rule created.
Testing the Setup
Now we are done with setup, lets go and test it. Click on the “Event buses” item from left navigation, and click on “Send events” button.
Enter the details as shown below
Make sure Event source and Detail type are correct. You will see the confirmation as below.
Validating the Setup
Now go to Cloudwatch and under log groups you should see two log groups created.
If you drill down into Log events you will see the below messages. At this point message is published to both the subscribers as shown below.
Thanks for reading.