Architecting Solutions: Buildinga Proof of Concept for a Serverless Solution
Serverless Solution
The exercises in this course will incur charges on your AWS account. In this exercise, you will create the following resources:
AWS Identity and Access Management (IAM) policy and user (these are AWS account features and are offered at no additional charge)
Amazon DynamoDB table
AWS Lambda functions
Amazon Simple Queue Service (Amazon SQS) queue
Amazon Simple Notification Service (Amazon SNS) topic
Amazon API Gateway
Amazon CloudWatch Logs
The final task includes instructions to delete all the resources you create.
Familiarize yourself with the following:
DynamoDB pricing
Amazon SQS pricing
Amazon SNS pricing
Lambda pricing
API Gateway pricing
CloudWatch Logs pricing
AWS Free Tier
Exercise 1: Architecting Solutions - Building a Proof of Concept for a Serverless Solution
In this exercise, you will build a proof of concept for a serverless solution in the AWS Cloud.
Suppose you have a customer who needs a serverless web backend hosted on AWS. The customer sells cleaning supplies and experiences spikes in website demand, requiring an architecture that can easily scale. Additionally, the application should have decoupled components.
The architectural diagram below shows the flow for the serverless solution you will build.
Architecture Diagram for Exercise 1
In this architecture, a REST API places a database entry in the Amazon SQS queue. Amazon SQS invokes the first Lambda function, which inserts the entry into a DynamoDB table. DynamoDB Streams captures a record of the new entry and invokes a second Lambda function, which then passes the database entry to Amazon SNS. After Amazon SNS processes the new record, it sends a notification to a specified email address.
In this exercise, you will learn to:
Create IAM policies and roles to follow best practices in the AWS Cloud.
Create a DynamoDB table to store data.
Create an Amazon SQS queue to handle messages between software components.
Create Lambda functions and set up triggers for different AWS services.
Enable DynamoDB Streams to capture database table modifications.
Configure Amazon SNS for email or text notifications.
Create a REST API to insert data into a database.
Notes:
To complete this exercise, select the US East (N. Virginia) us-east-1 Region in the AWS Management Console.
The instructions might prompt you to enter your 12-digit AWS account ID, found under your account alias in the AWS Management Console. Ensure to remove hyphens (-) when entering your account ID.
Task 1: Setup - Creating IAM Policies and Roles
When you create an AWS account, you become the root user, which we don't recommend for daily tasks. Instead, use IAM users or roles to access services and features. IAM policies, users, and roles are offered at no additional charge.
Step 1.1: Creating Custom IAM Policies
Sign in to the AWS Management Console.
In the search box, enter IAM.
From the results list, choose IAM.
In the navigation pane, choose Policies.
Choose Create policy.
In the JSON tab, paste the following code:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"dynamodb:PutItem",
"dynamodb:DescribeTable"
],
"Resource": "*"
}
]
}
Choose Next: Tags and then Next: Review.
For the policy name, enter Lambda-Write-DynamoDB.
Choose Create policy.
Repeat the previous steps to create the following policies:
Lambda-SNS-Publish
Lambda-DynamoDBStreams-Read
Lambda-Read-SQS
Step 1.2: Creating IAM Roles and Attaching Policies to the Roles
In the navigation pane of the IAM dashboard, choose Roles.
Choose Create role and configure the following settings:
Trusted entity type: AWS service
Common use cases: Lambda
Choose Next.
On the Add permissions page, select Lambda-Write-DynamoDB and Lambda-Read-SQS.
Choose Next.
For Role name, enter Lambda-SQS-DynamoDB.
Choose Create role.
Follow the previous steps to create two more IAM roles:
Lambda-DynamoDBStreams-SNS
APIGateway-SQS
Task 2: Creating a DynamoDB Table
In the AWS Management Console search box, enter DynamoDB.
From the list, choose DynamoDB.
On the Get started card, choose Create table and configure the following settings:
Table: orders
Partition key: orderID (Data type: String)
Keep the remaining settings at their default values, and choose Create table.
Task 3: Creating an SQS Queue
In the AWS Management Console search box, enter SQS and choose Simple Queue Service.
On the Get started card, choose Create queue.
Configure the following settings:
Name: POC-Queue
Access Policy: Basic
Define who can send messages to the queue: Only the specified AWS accounts, IAM users and roles (paste the ARN for the APIGateway-SQS IAM role)
Define who can receive messages from the queue: Only the specified AWS accounts, IAM users and roles (paste the ARN for the Lambda-SQS-DynamoDB IAM role)
Choose Create queue.
Task 4: Creating a Lambda Function and Setting Up Triggers
Step 4.1: Creating a Lambda Function for the Lambda-SQS-DynamoDB Role
In the AWS Management Console search box, enter Lambda.
Choose Create function and configure the following settings:
Function option: Author from scratch
Function name: POC-Lambda-1
Runtime: Python 3.9
Change default execution role: Use an existing role
Existing role: Lambda-SQS-DynamoDB
Choose Create function.
Step 4.2: Setting Up Amazon SQS as a Trigger to Invoke the Function
Expand the Function overview section if needed.
Choose Add trigger.
For Trigger configuration, enter SQS and choose the service from the list.
For SQS queue, choose POC-Queue.
Add the trigger by choosing Add.
Step 4.3: Adding and Deploying the Function Code
- On the POC-Lambda-1 page, in the Code tab, replace the default Lambda function code with the following code:
import boto3, uuid
client = boto3.resource('dynamodb')
table = client.Table("orders")
def lambda_handler(event, context):
for record in event['Records']:
print("test")
payload = record["body"]
print(str(payload))
table.put_item(Item={
'orderID': str(uuid.uuid4()),
'order': payload
})
- Choose Deploy.
Step 4.4: Testing the POC-Lambda-1 Lambda Function
In the Test tab, create a new event with the following settings:
Event name: POC-Lambda-Test-1
Template-Optional: SQS
Save your changes and choose Test.
Step 4.5: Verifying the Lambda Function Adds the Test Message to the Database
In the AWS Management Console search box, enter DynamoDB and choose DynamoDB.
In the navigation pane, choose Explore items.
Select the orders database. Under Items returned, the orders table should return "Hello from SQS!" from the Lambda function test.
Task 5: Enabling DynamoDB Streams
In the DynamoDB console, in the Tables section of the navigation pane, choose Update settings.
In the Tables card, ensure the orders table is selected.
Choose the Exports and streams tab.
In the DynamoDB stream details section, choose Enable.
For View type, choose New image.
Choose Enable stream.
Task 6: Creating an SNS Topic and Setting Up Subscriptions
Step 6.1: Creating a Topic in the Notification Service
In the AWS Management Console, search for SNS and choose Simple Notification Service.
On the Create topic card, enter POC-Topic and choose Next step.
In the Details section, keep the Standard topic type selected and choose Create topic.
On the POC-Topic page, copy the ARN of the topic for reference.
Step 6.2: Subscribing to Email Notifications
On the Subscriptions tab, choose Create subscription.
For Topic ARN, ensure the box contains the ARN for POC-Topic.
3.For Protocol, choose Email.
Provide Endpoint Email Address:
- Enter your email address for the
Endpoint
.
- Enter your email address for the
Create Subscription:
- Choose
Create subscription
. A confirmation message will be sent to the specified email address.
- Choose
Confirm Subscription:
- After receiving the confirmation email, confirm the subscription. If you don’t receive an email within a few minutes, check your spam folder.
Task 7: Creating an AWS Lambda Function to Publish a Message to the SNS Topic
In this task, you will create a Lambda function to publish messages to an SNS topic triggered by DynamoDB Streams.
Step 7.1: Creating the POC-Lambda-2 Function
Navigate to AWS Lambda:
- Search for and open AWS Lambda in the AWS Management Console.
Create Lambda Function:
Choose
Create function
and configure:Function option:
Author from scratch
Function name:
POC-Lambda-2
Runtime:
Python 3.9
Change default execution role:
Use an existing role
Existing role:
Lambda-DynamoDBStreams-SNS
Save and Create Function:
- Choose
Create function
.
- Choose
Step 7.2: Setting Up DynamoDB as a Trigger
Add Trigger:
In the Function overview section, choose
Add trigger
and configure:Trigger configuration:
DynamoDB
DynamoDB table:
orders
Enable Trigger:
- Ensure the DynamoDB state is
Enabled
in the Configuration tab.
- Ensure the DynamoDB state is
Step 7.3: Configure Second Lambda Function
Update Function Code:
- Choose the
Code
tab and replace the Lambda function code.
- Choose the
Deploy Changes:
- Choose
Deploy
.
- Choose
Step 7.4: Test the POC-Lambda-2 Function
Create Test Event:
- On the
Test
tab, create a new event withPOC-Lambda-Test-2
as the Event name.
- On the
Run Test:
Save changes and choose
Test
.Check for successful execution in the notification banner.
Task 8: Creating an API with Amazon API Gateway
In this task, you will create a REST API in Amazon API Gateway to serve as a communication gateway between your application and AWS services.
Open API Gateway:
- Search for and open API Gateway in the AWS Management Console.
Create REST API:
Choose
Build
on the REST API card with public authentication and configure settings:Choose the protocol:
REST
Create new API:
New API
API name:
POC-API
Endpoint Type:
Regional
Create API:
- Choose
Create API
.
- Choose
Configure Method:
Choose
Create Method
from the Actions menu.Choose
POST
from the method menu and save changes.
Setup POST - Integration:
- Configure integration type, AWS Region, AWS Service, Path override, Execution role, and Content Handling.
Save Changes:
- Save your changes.
Configure Integration Request:
- Configure HTTP Headers and Mapping Templates.
Save Changes:
- Save your changes.
Task 9: Testing the Architecture with API Gateway
In this task, you will use API Gateway to send mock data to Amazon SQS as a proof of concept for the serverless solution.
Test API Gateway:
- Return to the POST - Method Execution page and choose
Test
.
- Return to the POST - Method Execution page and choose
Enter Request Body:
- Enter the request body in the box.
Run Test:
- Choose
Test
to run the test.
- Choose
Task 10: Cleaning Up
In this task, you will delete the AWS resources created for this exercise to avoid unnecessary costs.
Delete DynamoDB Table:
- Open the DynamoDB console, select the
orders
table, and chooseDelete
.
- Open the DynamoDB console, select the
Delete Lambda Functions:
- Open the Lambda console, select
POC-Lambda-1
andPOC-Lambda-2
, and chooseDelete
.
- Open the Lambda console, select
Delete SQS Queue:
- Open the SQS console, select the queue, and choose
Delete
.
- Open the SQS console, select the queue, and choose
Delete SNS Topic and Subscriptions:
- Open the SNS console, delete the topic, and delete any subscriptions associated with it.
Delete API Gateway:
- Open the API Gateway console, select
POC-API
, and chooseDelete
.
- Open the API Gateway console, select
Delete IAM Roles and Policies:
- Open the IAM console, delete the roles and policies created for this exercise.
3.5
more
ChatGPT
Sure, here's the continuation:
Delete DynamoDB Table:
- Open the DynamoDB console, navigate to Tables, select the
orders
table, and choose Delete.
- Open the DynamoDB console, navigate to Tables, select the
Delete Lambda Functions:
- Open the Lambda console, select
POC-Lambda-1
andPOC-Lambda-2
, then click Delete.
- Open the Lambda console, select
Delete SQS Queue:
- Go to the SQS console, select the queue created for this exercise, and choose Delete Queue.
Delete SNS Topic and Subscriptions:
- In the SNS console, choose Topics, select the
POC-Topic
, then choose Delete Topic. Also, delete any subscriptions associated with it.
- In the SNS console, choose Topics, select the
Delete API Gateway:
- Open the API Gateway console, choose APIs, select
POC-API
, then choose Delete API.
- Open the API Gateway console, choose APIs, select
Delete IAM Roles and Policies:
- In the IAM console, navigate to Roles and Policies, delete the roles (
APIGateway-SQS
,Lambda-SQS-DynamoDB
,Lambda-DynamoDBStreams-SNS
) and associated policies.
- In the IAM console, navigate to Roles and Policies, delete the roles (
Ensure to follow these steps to clean up and avoid incurring unnecessary costs.