Guided Lab: Creating an Amazon SNS Standard Topic
Description
Amazon Simple Notification Service (SNS) is a fully managed pub/sub-messaging service that allows the decoupling of microservices, distributed systems, or applications by sending notifications or messages between systems. SNS supports various protocols, including email, SMS, HTTP/S, and AWS Lambda. This lab demonstrates how to create a Standard SNS topic, subscribe a Lambda function to the topic, and process SNS messages using a Lambda function.
The goal of this lab is to create a Standard SNS topic and integrate it with a Lambda function that processes messages from the topic. This setup is useful for real-time applications, like sending notifications or alerts in response to events.
Prerequisites
This lab assumes you have the following:
- Basic knowledge and understanding of AWS services, Amazon SNS, and AWS Lambda
- Familiarity with Python programming language.
If you find any gaps in your knowledge, consider taking the following lab:
Objectives
By the end of this lab, you will:
- Create a Standard SNS topic.
- Create an AWS Lambda function to process SNS messages.
- Subscribe to the Lambda function on the SNS topic.
- Publish a message to the SNS topic and verify the Lambda function’s processing through CloudWatch Logs.
Lab Steps
Create an SNS Standard Topic
1. Go to the Amazon SNS console. Search “sns” in the search bar.
2. You will be directed to a SNS dashboard.
3. To create Topics, look for the Create topic section, type your topic name (for example, OrdersQueue), and click Next step.
4. You will be directed to a page to configure your topic further. Follow the configurations below:
- Choose Standard as the topic type.
- In the Name field, enter a name for your topic (e.g., MyStandardTopic), or leave it as is if it is already filled.
- Leave other fields as default, scroll down, and click Create topic.
Create a Lambda Function to Process SNS Messages
1. Navigate to the AWS Lambda Console.
2. Create a new Lambda function using the following configurations:
- Choose Author from scratch.
- Function name: myLambdaFunction
- Select Python 3.12 as the runtime.
- Execution role:
- Select Use an Existing Role: PlayCloud-Sanbox
- Click Create function
3. Update the Lambda Function Code:
- In the function code editor, replace the default code with the following updated code:
import json
def lambda_handler(event, context):
# Print the event received from SNS
print("Received event from SNS:")
print(json.dumps(event, separators=(',', ':')))
# Process each record in the event
for record in event['Records']:
# Access the SNS message inside the Sns object
sns_message = record['Sns']['Message']
print(f"Processing SNS message: {sns_message}")
return {
'statusCode': 200,
'body': json.dumps('Messages processed successfully!')
}
This function will print out the message it receives from the SNS to the logs.
4. Deploy the Lambda function by clicking the Deploy button.
Create a Subscription for the Topic
1. Navigate back to the Amazon SNS Topic we create earlier. In the subscription tab, click on Create Subscription.
2. In the Protocol drop-down, choose the type of subscription you want to create.
Common protocols include:
- Email – Sends notifications via email.
- SMS – Sends notifications via text message.
- Amazon SQS – Sends messages to an SQS queue.
- AWS Lambda – Invokes a Lambda function when a message is published.
- HTTP/HTTPS – Sends HTTP/HTTPS POST requests.
a. For this lab, choose AWS Lambda and provide a valid email address.
b. In the Endpoint field, choose the Lambda Function we created earlier.
3. Click Create subscription.
Publish a Message to the SNS Topic and Verify
1. Publish Message to Topic:
-
Navigate to the SNS Console, and select MyStandardTopic from the Topics section.
Ignore any error message that will pop up.
-
Notice that the Lambda function is now registered as one of the subscriptions.
- Click Publish message.
2. Enter Message Details:
- In the Subject field, enter: Test SNS.
- In the Message body, enter a simple message, e.g., Tutorials Dojo Number One.
- Scroll down and click Publish message.
Ignore the red error pop-up.
3. Verify Lambda Execution:
- Go back to your Lambda function.
- Click on the Monitoring tab.
- Select View logs in CloudWatch.
- In CloudWatch Logs, find the latest log stream for your function.
- Review the logs to see the messages printed by your function.
- Open the latest log stream to verify the SNS message. You should see output similar to the following:
Congratulations! In this lab, you successfully created a Standard SNS topic and linked it with a Lambda function to process messages. The Lambda function was configured to handle incoming SNS messages, and you published a test message to verify this. You also explored how CloudWatch Logs helps you monitor message processing.
This hands-on lab showcases how SNS and Lambda can be used together for real-time messaging, event-driven notifications, and decoupling application components.
As a best practice, remember to delete any resources no longer in use, such as SNS topic and Lambda functions, to maintain a clutter-free AWS environment.
Thank you for joining this lab, and happy learning!