AWS Event-Driven Architecture

Kevin Kiruri
6 min readDec 8, 2023

--

Event-Driven Architecture (EDA) is a great way to make things happen automatically when certain events occur. AWS offers tools to make events trigger actions across different services. In this blog, we’ll walk through a hands-on example using three AWS tools: Amazon S3, AWS Lambda, and Simple Notification Service (SNS). Imagine setting up a special storage space (S3 bucket) where, whenever you put a file in it, it automatically kicks off a little program (Lambda function). This program then sends a quick message to an email address using SNS. It’s like creating a chain reaction of events without manual intervention. Join us as we break down the steps and explore how this event-driven magic works on AWS!

Prerequisites

  1. Have an AWS account. If you don’t have one, sign up here and enjoy the benefits of the Free-Tier Account
  2. View project files

Creating an S3 Bucket

  1. On the S3 bucket console on AWS, click on Create bucket
v

2. Select the region you want to host the bucket and give the bucket a unique name for all buckets within that region

3. Keep the other settings as default and scroll to the bottom and click on Create bucket

Creating an SNS Topic

  1. On the Amazon SNS Dashboard, select Topic on the left-hand menu and click on Create topic

2. Select the Standard type, and give it a name. `

3. Scroll to the bottom and click on Create topic

4. Note the ARN of the Topic, we will be using it later

Subscribing to a Topic

We will create an email subscription to the topic

  1. On the Topic dashboard, click on Create subscription

2. Select Email as the Protocol and put the desired email as the Endpoint

3. Scroll to the bottom and click on Create subscription

4. In the email endpoint, you will receive an email from AWS Notifications for you to Confirm the subscription

5. Open the mail and click on the Confirm subscription link

Creating a Lambda Function

  1. On the Lambda Console dashboard, on the left navigation plane, select Functions, then click on Create function

2. Select Author from scratch, then give the function a name and select the most updated Python runtime (Currently as at the time I am writing this blog, it’s Python 3.11)

3. Scroll to the bottom and click on Create function

4. On the code tab, input the following code.

import os
import json
from datetime import datetime
import boto3

def lambda_handler(event, context):
# Get information about the uploaded object from the S3 event
s3_event = event['Records'][0]['s3']
bucket_name = s3_event['bucket']['name']
object_name = s3_event['object']['key']
object_size = s3_event['object']['size']

# Get the current timestamp
upload_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

# Prepare message to be sent to SNS
message = {
'bucket_name': bucket_name,
'object_name': object_name,
'object_size': object_size,
'upload_time': upload_time
}

# Convert the message to JSON
message_json = json.dumps(message)

# Set up the SNS client
sns_client = boto3.client('sns')

# Replace 'YOUR_SNS_TOPIC_ARN' with your actual SNS topic ARN
sns_topic_arn = 'YOUR_SNS_TOPIC_ARN'

# Publish the message to the SNS topic
sns_client.publish(
TopicArn=sns_topic_arn,
Message=message_json,
Subject=f"Object Uploaded: {object_key}"
)

print(f"Message published to SNS: {message_json}")

return {
'statusCode': 200,
'body': json.dumps('Message published to SNS successfully!')
}

3. Replace YOUR_SNS_TOPIC_ARN with the ARN of the Topic we created earlier

3. Click on Deploy

Adding the Trigger to the Lambda Function

  1. On the Function overview page, click on Add trigger

2. Select S3 as the source and select the bucket we created earlier. Select PUT as the Event type so that the function only runs when an object is uploaded. Scroll to the bottom and click on Add

Edit the Lambda Role

  1. Click on the Configuration tab and select the Role name link

2. It opens on a new tab. Scroll down to Permission policies and select the Policy name link

3. Click on Edit

4. Select the visual tab and click on Add more permissions

5. Select the Service as SNS, for the actions allowed, select Publish and you can tick the box for Any in this account . Click on Next

6. Click on Save changes

Let’s test it

  1. Open the bucket we created on S3 and click on Upload

2. Click on Add files , select the file you want to upload, then click on Upload

3. You should get an email with the object details:

CleanUp

  1. Delete the uploaded object
  2. Delete the S3 bucket
  3. Delete the SNS topic
  4. Delete the Lambda function

Conclusion

Using Event-Driven Architecture on AWS shows us a smart way to make things happen automatically when certain events occur. Our journey with Amazon S3, AWS Lambda, and Simple Notification Service (SNS) demonstrated how a small event, like uploading a file, can set off a chain of actions. This isn’t just about making things easier — it’s about making systems work faster and smarter. Understanding this approach helps us build efficient, flexible solutions in cloud. Event-Driven Architecture is like giving our systems a superpower, making them respond and adapt to changes in a way that’s both clever and simple.

--

--