Santa’s AWS Workshop! (A serverless event driven approach)
Santa is a very busy man and even he needs help organizing and coordinating his operation. This article will help illustrate how a serverless event driven approach can help him deliver toys more effectively given the amount of letters he receives from all over the world.
Reading on will provide an overview of a working application with actual code to implement an actual AWS Solution which you can deploy. It is written in Java with CDK for deployments. The concepts are the same regardless of the language.
This solution will integrate different architectural concepts to achieve this within the AWS environment:
- Serverless architecture (Lambda, DynamoDB, SQS, SNS)
- Polyglot databases (RDS Postgres and DynamoDB)
- Single Table design with DynamoDB (Inverted Indexes)
- Event-Driven Architecture with the Saga Pattern Approach in AWS
- CQRS (Command Query Responsibility Segregation)
Introduction
Santa receives many letters requesting one or many toys for each child. Each of these Letters must be tracked along with the different toys for each letter. This is similar to an Ordering system by all the children in the world to Santa’s workshop.
Architecture
This application will manage letters(Parent) which contain a set of toys(Child) that each child requests. This data will be stored in a DynamoDB table Letters following a single table design to ensure that the parent child relationship between letters and toys is maintained.
At a high level overview, the architecture of Santa’s workshop consists of a RESTful set of microservices(with API Gateway & Lambda), an SNS Topic which deliver messages to 2 subscriber SQS queues.
We will now walk through the actual process of building the AWS Santa’s workshop to start
Prerequisites:
- Set up your Java environment as documented at Getting started with AWS, Java 11 (Amazon Corretto), Eclipse and AWS Toolkit
This article will follow a similar approach for developing a Java CDK Approach as documented at Creating an AWS Serverless Java 11 Application with CDK (Lambda, S3, DynamoDb, API Gateway)
2. Create the required PostgreSQL RDS instance as noted in Creating an AWS RDS PostgreSQL Database with pgAdmin
Project Setup:
Open up Eclipse and check out the code from the https://github.com/collin-smith/santasworkshop repository.
File/Import Git/Projects from Git (with smart import)
Press Next and select Clone URI
Press Next then enter in the github repo
Then press Next again
Set the destination directory to be C:\projects\santasworkshop
Press Next Then press Finish
You should now have a project which looks like the following:
Ensure your Java Build Path is set up correctly with JavaSE-11 and Maven Dependencies selected (Right click the Project Explorer in Eclipse to open the build path):
Enter the correct RDS Configuration in the SantasworkshopStack.java class by getting the db endpoint for your RDS instance in your AWS console.
Right click in the project explorer, Run As, Maven build.. . Then type clean install in the Goals text box. Then press Run
This should result in a successful build
Deploying the Code to your AWS Environment:
Once built, you should be able to go to the cmd console and complete the following steps to deploy it to your aws environment:
cd c:\projects\santasworkshop
cdk bootstrap
cdk synth
cdk deploy
It should take around 5–10 minutes to deploy
You have now successfully deployed the REST endpoints, the DynamoDb, SNS Topic and 2 SQS queues. In addition, this system will work with the RDS PostgreSQL instance as well.
As part of this article we will do a simple test of the first endpoint which should insert some data into the DynamoDB database.
Take the POST endpoint from the console which ends in /prod/insertletters and we can test using https://www.postman.com/ . It does not require a body when making the call.
You should now be able to look in your console and see that there are now items populated in the dev-Letter DynamoDb table
We can now confirm that the application has successfully deployed and that the DynamoDB table can be populated by calling one of the endpoints.
We will continue to explore this application to discuss further concepts. This will greatly help Santa become more efficient at delivering toys to the kids this year!
The additional concepts that can be discussed further include the following:
- Serverless architecture (Lambda, DynamoDB, SQS, SNS)
- Polyglot databases (RDS Postgres and DynamoDB)
- Single Table design with DynamoDB (Inverted Indexes)
- Event-Driven Architecture with the Saga Pattern Approach in AWS
- CQRS (Command Query Responsibility Segregation)
Resource Cleanup:
If you have finished with the PostgreSQL instance, go to the RDS section in the AWS console and delete the instance.
Also, to remove the Lambda code, you should run cdk destroy to remove the code deployed by the cdk project.
cdk destroy