Creating Serverless APIs with DynamoDB and Lambda 🚀
This article will teach you how to build a server-less backend API using DynamoDB as the database. We will be using AWS to host the backend NodeJS application using Serverless Framework. Let’s start with a small introduction on Server-less.
Server-less
Server-less technology is a pretty popular item these days, and it should be as it really is a superpower for smaller teams. It is a subcategory of managed services and basically it means that you don’t manage servers any longer. Servers are abstracted away and you pay-per-use of the service (defined in various ways, depending on the specific service). This is especially useful if you have high elastic workloads, but also to enable very cost-efficient environments.
“It’s not “No” server. It’s No server for “You” to manage. “
By the end of this article you’ll know how to:
- Set up Serverless Framework with AWS
- Create a server-less database with AWS DynamoDB and read data from the table
- Create a NodeJS-Express web application with Serverless Framework
- Deploy the application to AWS using Serverless Framework.
Being able to do all these things gives you the ability to create all the functionality needed for most application back ends in a completely server-less way.
Setting up Serverless framework with AWS
The Serverless Framework is a tool that we can use as developers to configure and deploy server-less services from our computers. There’s a bit of setup to allow all of this to work together and this section will show you how to do that.
To allow Serverless to do work on your account, you need to set up a user for it. To do this, navigate into AWS and search for “IAM” (Identity and Access Management).
Once on the IAM Page, click on Users in the list on the left hand side. This will open the list of users on your account. From here we’ll be clicking Add user.
We need to create a user which has Programmatic access and I’ve called my user ServerlessApp, but the name doesn’t matter too much.
On Create user you will be prompted to download the keys. Please download the keys to your local system. We will be using this it in the next couple of steps.
We have now successfully created the user and downloaded the access keys to create a profile for AWS in our local system.
Now let us start setting up AWS CLI in our local system. This tutorial shows the set up process for Mac OS. Process for Windows and Linux systems will shared in a separate article.
To start with we need AWS CLI installed in the local system. Please follow the steps listed out in the following link to install latest AWS CLI in your local system.
To check if AWS CLI is already installed in the local system type the following command.
aws --version
If AWS CLI is successfully installed in the system, we now have to add a named profile to deploy our lambda function to AWS.
aws configure --profile serverlessapp
To check if the named profile is added to AWS config in your local system, type the following in the terminal.
aws configure list-profiles
We have now successfully added a named profile in the local system. Let us now start creating DynamoDB in AWS.
Setting up DynamoDB
Amazon DynamoDB is a fully managed proprietary NoSQL database service that supports key–value and document data structures. DynamoDB is a connection less database. Since there is no upper limit for database connections the database is horizontally scalable and can auto-scale based on incoming traffic. To have a complete server-less back-end API, having a connection-less database is crucial. If the database is limited by connections, the incoming requests will be throttled based on the connection limit and that impacts customer experience due to wait times. DynamoDB is also super fast with sub milliseconds latency.
To create a DynamoDB table on your account, navigate into AWS and search for “DynamoDB” in the console. Click on Create table.
Let us assume a simple API that gets a list of products in the database. Let us create a table with name serverlessDB and partition key as productName and sort key as CreatedAt. Defining the partition key and sort key for the table is a separate topic of discussion and will be addressed in subsequent articles.
Select Default settings to start with.
We have now successfully created a DynamoDB table. Now, Let us start setting up the NodeJS code in our local system.
Setting up NodeJS-Express Application
Before setting up the code base for NodeJS-ExpressJS application, we should install Serverless Framework in the local system.
npm install -g serverless
After installing Serverless globally, we can now start with the NodeJS-ExpressJS application with
npm init
npm install express cors dotenv aws-sdk serverless-http
For simplicity we will be having one POST method to get all “ACTIVE” products. We will have the following folder structure.
The code for each of these files are listed below.
The serverless.yaml should contain the lambda function name in service. We will call this application serverlessAPI. We should also mention the stage of development in stage. For demo purposes we can call this dev. We should also mention the profile name from the AWS named profiles we created by downloading the key and secret from AWS. The codes for all other files are shown below.
If we have all these set up, we are ready to go. Now we can go ahead and host the application using Serverless Framework.
Hosting the application using Serverless Framework
To host the application to AWS using Serverless Framework, we have to just type
serverless deploy
Serverless Framework will do all the heavy lifting of creating CloudFormation Stack, S3 Bucket to store the code, Create API Gateway, spawn lambda Function and attach it to API Gateway.
On successful hosting Serverless Framework will give us the CloudFront URL. Let’s run a quick POSTMAN call to check if the back-end is running.
And Voila!
We now have a completely server-less API that can auto-scale with no intervention.
About CodeStax.Ai
At CodeStax.Ai, we stand at the nexus of innovation and enterprise solutions, offering technology partnerships that empower businesses to drive efficiency, innovation, and growth, harnessing the transformative power of no-code platforms and advanced AI integrations.
But the real magic? It’s our tech tribe behind the scenes. If you’ve got a knack for innovation and a passion for redefining the norm, we’ve got the perfect tech playground for you. CodeStax.Ai offers more than a job — it’s a journey into the very heart of what’s next. Join us, and be part of the revolution that’s redefining the enterprise tech landscape.