Creating a simple Ordering service with AWS Serverless Architecture using AWS SAM as Infrastructure-as-Code (IaC).
This project was a part from the ByteWise fellowship program. The goal of this project is to get acquainted with AWS Serverless Architecture, its various services, AWS SAM as IaC and Github Actions for CI/CD pipelines.
This project is a simple ordering service that allows users to order a product and get the order details. The service is built using AWS Lambda
, API Gateway
, SQS
, and SNS
as shown in figure below.
The Project flow is as follows:
- The user sends a POST request to the API Gateway endpoint with the order details.
- The API Gateway triggers the
CreateOrder
Lambda function. - The Lampbda function sends the information to a queue (SQS).
- The SQS queue, in turn, triggers the next Lambda function (
ProcessOrder
). - This Lambda function publishes the order details to an SNS Topic.
- This SNS Topic sends the order details to the user via email.
- The Order details of user is sent by POST request to the "InvokingAPI" endpoint in the following JSON format:
{
"name": "John Doe",
"email": "[email protected]",
"product": "Product Name",
"quantity": 1
}
- The user will receive an email with the order details.
As this project is built using AWS SAM, we will be using a template file to define the AWS resources used in this project.
The template file defines the following resources:
- Two Lambda functions:
CreateOrder
andProcessOrder
. - An SQS queue:
OrderQueue
. - An SNS Topic:
OrderTopic
. - An SNS Topic Subscription:
OrderNotificationSubscription
. - An API Gateway endpoint:
InvokingApi
.
The template file also defines the following environment variables:
- The SQS queue URL:
ORDER_TOPIC_ARN
. - The SNS Topic ARN:
QUEUE_URL
.
The template file also defines the following outputs:
- The API Gateway endpoint URL.
- The SQS queue URL:
OrderQueueUrl
. - The SNS Topic ARN:
OrderTopicArn
.
The template file also defines the following policies:
- A policy for the CreateOrder Lambda function to send messages to the SQS queue.
- A policy for the ProcessOrder Lambda function to receive messages from the SQS queue and publish messages to the SNS Topic.
- A policy for the SNS Topic to send emails to the user.
The template file also defines the following permissions:
- A permission for the SQS queue to trigger the ProcessOrder Lambda function.
- A permission for the API Gateway endpoint to trigger the CreateOrder Lambda function.
The template file can be found here in the project repository.
This service is deployed using CI/CD pipelines of Github Actions. The CI/CD pipeline is defined in the workflow file.
The DeployProd
job is defined in this workflow, which is responsible for deploying the application to the production environment. The job runs on an Ubuntu latest virtual machine and has an environment named "production". The job is triggered when a push event occurs on the main branch of the repository.
The workflow file defines the following main jobs:
- Build: This job builds the project using the
sam build --use-container
command - Deploy: This job deploys the SAM application to the production environment using the
sam deploy
command. The--no-confirm-changeset
flag is used to automatically confirm the changeset,--no-fail-on-empty-changeset
flag is used to not fail if there are no changes to be deployed,--stack-name
flag is used to specify the name of the CloudFormation stack,--resolve-s3
flag is used to resolve S3 bucket names in the CloudFormation template,--capabilities
flag is used to specify the IAM and auto-expand capabilities, and--region
flag is used to specify the AWS region.
The workflow file also defines the following environment variables:
- AWS_ACCESS_KEY_ID: The AWS access key ID.
- AWS_SECRET_ACCESS_KEY: The AWS secret access key.
- AWS_DEFAULT_REGION: The AWS region.
These environment variables are acquired from AWS Console, and defined as secrets in the repository settings.