Using AWS CDK to develop Serverless Reminder App

Aqil Zeeshan
5 min readMar 6, 2021
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

  1. User specify the time after which they need to be reminded, the message they want to recieve, their email and phone number, preference that they want to be reminded by email only or sms or both.
  2. Application sends email or sms or both based on user preference after the time user specified.

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:

  1. Once form is filled in browser, it makes fetch Api POST call to AWS API Gateway.
  2. AWS API Gateway has direct service integration with AWS Step functions.
  3. In this application Step Functions execute the following state machine which either sends email or sms or both.
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.

--

--

Aqil Zeeshan

Release Manager and Scrum Master proficient in Agile, Scrum, Kanban, AWS, Containers and fully automated release processes.