Using AWS CDK to develop Serverless Reminder App

Architecture Diagram

Infrastructure as code (IaC)

The inspiration for this prototype and article came from https://acloudguru.com lab which shows step by step instructions to develop the whole application using AWS Console. Using UI to create infrastructure only works in classroom but in real world it causes all the problems which gave rise to the concept of Infrastructure as code (IaC).

  • IaC helps to automate the infrastructure deployment process in a repeatable, consistent manner.
  • IaC allows you to spin up an entire infrastructure by running a script.
  • IaC completely standardizes the setup of instrastructure so there is reduced possibility of any errors or deviations.
  • IaC serves as a form of documentation to minimize the risk in the case where employees leave your company with institutional knowledge.

AWS CDK

Based on above benefits my initial intention was to use Cloudformation to create the whole application but then while attending AWS Dev Hour I heard Ben Newton saying that “Cloudformation is the Assembly language of the cloud”. It completely changed my prespective and I decided to use AWS CDK to develop this whole application using Typescript.

  • AWS CDK reduces 10x the number of lines code compared to CloudFormation.
  • Since AWS CDK is a wrapper around CloudFormation so you get all the benefits of CloudFormation (repeatable deployment, easy rollback, and drift detection).
  • Using AWS CDK code can be written in any of the fimiliar programming languages (Typescript, C#, Java etc.)

I can tell from my personal experience that my speed of developing AWS solutions has increased many folds since I started using AWS CDK. There is no turning back to CloudFormation once you start using AWS CDK.

Application functionality

Step Functions

As mentioned here, AWS Step Functions are a “function orchestrator”, allowing you to connect multiple Lambda functions and other AWS services into an application.By moving the logic associated with the application including decisions, retries, parallel tasks, and error handling out of the Lambda functions, we can reduce the amount of code to construct the application, simplifying updates and reducing code complexity.

Application Architecture

Refer to diagram at the begining of the article. Explanation is as follows:

State Machine

Antipattern — Lambda function between Api Gateway and Step Functions In the actual lab a lambda function was used to pass data between API Gateway and Step Functions. Using lambda simply to pass data between services is waste of money and resources. Imagine huge number of visitors using the app, which means this lambda function will be called every time API gateway makes call to Step Functions. API Gatway offers direct service integration with many services including Step Functions. In this app direct integreation has been used between API Gateway and Step functions. AWS CDK. Execute Stepfunction via API Gateway by Oleksii Ivanchenko helped to create a class which simply takes machine as props and expose it as REST Api.

Code Walkthrough

Public S3 bucket is created to host front-end application. aws-s3-deployment module uploads front-end from local folder to the correct S3 bucket.

Using aws-s3-deployment to upload front-end to public S3 bucket

After that IAM role is created which can be used by lambda functions to do their job. Policy Statements are attached to managed policy which is added to the IAM role. For production code, best pratice of adding separate role to each lambda function should be followed where only those policy statements should be attached to the each role which are required by function to do its job.

Execution Role for Lambda functions

After that lambda functions are created providing runtime, path to handler function, attaching execution role and passing already verified email in SES as enviornment variable.

Lambda functions

email_remind.py uses AWS SES to send email

sms_reminder.py uses AWS SNS to send email as soon as a message is published to it.

Lambda function to send sms

Next tasks are created for AWS Step functions which is serverless microservice orchestraction service using aws-stepfunctions-task module. Lambda functions created earlier are provided to tasks. State machine is created for Step functions using condition and parallel methods of aws-stepfunctions module.

State Machine

Finally state machine is passed to API gateway so it can be exposed to frond-end as REST api

Machine passed to API Gateway

ApitoStateMachine.ts is providing abstraction to all the functionality of enabling CORS so that frond-end does not have to be on same domain as REST api endpoint. Reusable nature of this class to expose any state machine as REST api shows how AWS CDK helps to create reusable constructs.

Source Code of Application Complete source code of the application can be found here. The only pre-requisite for running the application is to have a verified email address added in AWS SES and provided to application here

Next Steps Microservice orchestration using Lambda functions and Step functions can be used to create more sophisticated workflows. I would like to create a more complex workflow using these services in future. I would also like to add unit tests for lambda functions and integration tests for REST api.

Release Manager, Educator & Father https://bytesizecoding.com/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store