AWS Lambda is a service from Amazon Web Services (AWS) that lets you run your code in response to events without managing servers. It’s a simple and scalable way to build applications.
In this tutorial, I’ll show you how to use AWS Lambda with three other services:
-
Amazon S3 for storing files, images, and videos
-
Amazon Simple Notification Service (SNS) for sending notifications
-
Amazon EventBridge for scheduling messages
We’ll go through everything step by step.
By the end, with the integration of the other services, you will have built a Goal Manifestation Quote App that sends random inspirational messages to keep you motivated and focused on your goals.
Prerequisites
-
An AWS account: If you don’t have one, sign up here.
-
A GitHub repository: This is for storing your source code. If you don’t have a GitHub account, you can create one here.
-
An Integrated Development Environment (IDE) such as Visual Studio Code or Sublime Text.
-
A basic knowledge of web development and any programming language of your choice. I used Python for this tutorial.
What You’ll Learn
-
How to create an Amazon S3 bucket
-
How to use Amazon Simple Notification Service (SNS)
-
How to use Amazon Lambda
-
How to use Amazon EventBridge
Table of Contents
Step 1: Set Up Your Development Environment
In this step, you’ll get everything set up. Start by signing in to your AWS account, then install Python if you don’t have it on your IDE.
Step 2: Create an Amazon Simple Storage Service (S3)
Before we begin creating an S3 bucket, let’s first understand what Amazon S3 is:
Amazon S3 (Simple Storage Service) is a service from Amazon that allows you to store and access any amount or type of data, such as photos, videos, documents, and backups, whenever you need it.
Now that you know the basics of what Amazon S3 is, let’s return to the tutorial.
Create an S3 Bucket
There are several ways to create an S3 bucket, but for this tutorial, we’ll use the Ubuntu command line (CMD), your terminal, or Amazon CloudShell, depending on what you’re most comfortable with.
-
Type boto3 s3 in the web search bar to view a list of related documentation.
-
Click on the first result.
- Once the documentation is open, copy the first command you see.
- Paste it on your CMD OR terminal of your choice – but before then remember to “cd” into the right directory.
- In the documentation, scroll down and click on “create_bucket.
-
Once it’s open, scroll down to “Request Syntax.” Copy the bucket name and the bucket configuration.
-
Other variables listed in the request syntax are optional.
- Once this is done, make sure you save.
- Go back and call the script:
#python3 your file name
- Running the script automatically creates an S3 bucket in your Amazon S3.
- Now you can go to the console to check if it has been created:
Upload Files
With the bucket created, we can now upload files through the console. I believe there’s also a programmatic way to upload files and test, but I haven’t explored all the methods in the documentation yet.
Click on the bucket name to be redirected to the object page. This is where you will upload your files for storage.
Click on the Upload button to upload a file. Remember, we’re creating a Goal Manifestation Quote Application.
Now that we’ve set up a storage bucket:
-
Open a tool like Google Drive, MS Word, WPS, or any other document editor.
-
Write down the goals you want to achieve.
-
Save the file in either PDF or DOCX format.
-
Take the document and upload it to your Amazon S3.o
To verify if it’s the correct file:
-
Navigate to the Permissions tab.
-
Scroll down to Block public access.
-
Click on Edit and uncheck the box.
As shown above, it is currently set to “on.” Uncheck it to turn it “off.”
-
On the same bucket settings page, modify the policy.
-
Scroll down, and you’ll see that a bucket policy has been auto-generated.
-
Go ahead and copy the policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::your-bucket-name/*"
}
]
}
- Go back to the bucket policy editor and paste the policy.
Once you’ve completed these steps, your object will have public access.
Return to the Objects tab and click on the Object URL provided below:
With this URL, your upload is now visible.
Step 3: Create an Amazon Simple Notification Service (SNS)
SNS is a fully managed messaging service provided by AWS. It enables communication between applications or directly with users by sending notifications.
To create an SNS, follow these steps:
1. Log in to the AWS management console
Then go to Amazon SNS. Navigate to the SNS Dashboard and select Topics from the left-hand menu.
To create a topic:
-
Click Create topic.
-
Choose a Topic type: Standard (default) or FIFO (for ordered messages).
-
Enter a Name for your topic. (for example,
MyFirstSNSTopic
). -
Configure optional settings like encryption, delivery retry policies, or tags.
-
Click Create topic.
2. Add Subscriptions:
Once the topic is created, click on it to open the details page. Select the Subscriptions tab.
Click Create Subscription and choose:
-
Protocol can be Email, SMS, HTTP/S, Lambda, or SQS.
-
Endpoints such as email address, phone number, or URL.
Click Create Subscription.
3. Confirm the Subscription:
If you selected email or SMS, a confirmation link or code will be sent to the provided endpoint. Follow the instructions to confirm the subscription.
Now that we’ve done this, let’s create an Amazon Lambda function that will trigger the SNS so that the message will be sent to your mail.
Step 4: Create an IAM Policy
This policy is created to authorize Amazon Lambda to trigger the event and to ensure that CloudWatch is automatically triggered to monitor the application’s events.
To create a policy, follow these steps:
1. Log in to the AWS Management console.
In the left-hand menu, select Policies. Then:
-
Click Create policy.
-
Choose the Visual tab, then select the SNS service.
-
Next, click the Choose tab to create a custom policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": "arn:aws:sns:REGION:ACCOUNT_ID:goal_topic"
}
]
}
Then, replace the following placeholders with your info:
-
region
: Your AWS region (for example,us-east-1
). -
account-id
: Your AWS account ID. -
topic-name
: Your SNS topic name.
2. View and create the policy:
You can do this by following these steps:
-
Click on the Review button.
-
Give your policy a Name (for example,
LambdaSNSPolicy
), and optionally, a Description. -
Click Create policy.
3. Attach policy to the Lambda Execution Role
Now, you need to attach the policy to your Lambda Execution Role. To do that, follow these steps:
-
Go to the Roles section in the IAM Console.
-
Search for and select the execution role.
-
Next, search for the policy you just created and select it.
-
Click Attach policy.
Both policies will be automatically attached.
Step 5: Create an Amazon Lambda function
Amazon Lambda is a service from AWS that lets you run code without managing servers. You upload your code, and Lambda automatically runs and scales it when needed.
Follow these steps to create an Amazon Lambda function:
1. Log in to AWS Management Console:
Navigate to AWS Lambda.
2. Create a Function:
Click on the Create function and choose the option Author from scratch.
Fill in the details:
-
Function name: Enter a unique name (for example,
SNSLambdaFunction
). -
Runtime: Select the runtime (for example, Python, Node.js, Java, and so on).
- Role: Choose or create a role. If you already have a role, select Use an existing role. Otherwise, select Create a new role with basic Lambda permissions.
- Click the Create function button.
3. Paste in the code:
On the Lambda function’s page, go to the Configuration tab:
Remember, we are trying to fetch a quote. I’ll add the ARN of the topic we created here and include my API keys. But for this tutorial, I will use the API directly to fetch the data.
4. Write the Lambda Code:
Go to the Code tab in your Lambda function. Then write or paste the code from your IDE to process the incoming SNS messages.
Example:
Here’s the code:
import os
import json
import urllib.request
import boto3
def fetch_random_quote():
"""
Fetches a random quote from the ZenQuotes API.
"""
api_url = "https://zenquotes.io/api/random"
try:
with urllib.request.urlopen(api_url) as response:
data = json.loads(response.read().decode())
if data and isinstance(data, list):
# Format the quote and author
quote = data[0].get("q", "No quote available")
author = data[0].get("a", "Unknown author")
return f'"{quote}" - {author}'
else:
return "No quote available."
except Exception as e:
print(f"Error fetching random quote: {e}")
return "Failed to fetch quote."
def lambda_handler(event, context):
"""
AWS Lambda handler function to fetch a random quote and publish it to an SNS topic.
"""
# Get the SNS topic ARN from environment variables
sns_topic_arn = os.getenv("SNS_TOPIC_ARN")
sns_client = boto3.client("sns")
# Fetch a random quote
quote = fetch_random_quote()
print(f"Fetched Quote: {quote}")
# Publish the quote to SNS
try:
sns_client.publish(
TopicArn=sns_topic_arn,
Message=quote,
Subject="Daily Random Quote to help you stay motivated and inspired to achieve your goals",
)
print("Quote published to SNS successfully.")
except Exception as e:
print(f"Error publishing to SNS: {e}")
return {"statusCode": 500, "body": "Error publishing to SNS"}
return {"statusCode": 200, "body": "Quote sent to SNS"}
5. Save:
Click on the deploy button to save.
6. Test Your Lambda Function:
Go to the Test tab and create a new test event.
Then save and run the test. If it’s successful, a message will be sent:
This means the message has been created for you
Finally, check your email or SMS, depending on the endpoint you used for this tutorial. In my case, I used email.
Step 6: Create an EventBridge
Amazon EventBridge is a service that helps you connect applications and AWS services such as the Amazon SNS and Amazon Lambda.
To create an Amazon EventBridge rule, follow these steps:
1. Navigate to EventBridge:
In the search bar, type EventBridge and select it from the services list.
2. Create a Rule:
In the EventBridge console, click Rules on the left panel. Then click the Create rule button.
3. Set Up the Rule Details:
-
Name: Enter a unique name for your rule.
-
Description (optional): Add a description to explain what this rule does.
4. Choose the Event Bus:
Select Default event bus (or another event bus if you’ve created one).
5. Define the Event Pattern or Schedule:
For Event Pattern:
-
Choose an AWS Service as the event source.
-
Select the specific event type (for example, an S3 file upload or an EC2 instance state change).
For Schedule:
- Choose the Schedule option to run the rule on a fixed interval (for example, every 5 minutes).
- Click on continue. This takes you to the specific details page where:
-
Scroll down and click on the cron scheduler. The cron scheduler specifies what time the message will be sent.
-
Select “Off” for the flexible time window option.
-
Review the rule details to confirm everything is correct.
-
Click the “Next” button to proceed to the Target page.
The picture above shows when the time the messages will be sent.
- On the Target page, select AWS Lambda to invoke your function.
- Scroll down to invoke and choose the function you created.
- Click the “Next” button to proceed. This will take you to the settings page. Under the permissions section, select “Use existing rule.”
- Lastly, go to the review and create a schedule:
- The next page shows you all the details:
Using the EventBrige creates a scheduler for the users.
Step 7: Upload Your Code
Finally, upload your code to GitHub and include proper documentation to help explain how the code works.
Check this documentation out if you don’t know how: Uploading a project to GitHub.
Conclusion
If you’ve followed all these steps, you will have created a Goal Manifestation Quote App using AWS Lambda, Amazon S3, Amazon SNS, and Amazon EventBridge. This app fetches motivational quotes and sends them to subscribers on a schedule.
You can find the repository link here.
Feel free to share your progress or ask questions if you have any issues.
If you found this article helpful, share it with others.
Stay updated with my projects by following me on Twitter, LinkedIn and GitHub
Thank you for reading 💖.
Disclaimer:
The resources shown in this article, including the S3 bucket and its ARN, have been deleted and no longer exist. The details visible in the screenshots are used solely for demonstration purposes.
Source: freeCodeCamp Programming Tutorials: Python, JavaScript, Git & MoreÂ