In this tutorial, we will implement a Serverless API using AWS Lambda and we will deploy it using AWS CDK. We will use Typescript as the CDK language. It will be a page view counter where we will keep the state in Redis.
Check the github repo for the source code.
- AWS CloudFormation
- AWS SAM
- Serverless Framework
The above projects allows you to set up the infrastructure with configuration files (yaml, json) while with AWS CDK, you set up the resources with code. For more information about CDK see the related AWS Docs.
Install cdk with:
npm install -g aws-cdk
Create a directory for your project. Inside the directory init cdk:
Create a directory for your API function
Inside the API folder, init and npm project and install ioredis.
In the api folder, create a file: counter.js
Go back to the top directory, install lambda and api-gateway libraries:
Inside the lib directory, create counter_service.ts:
In the top folder:
Now you should see the command outputs the endpoint url. Click to the url, you should see the page counter.