Connect with us

Get more updates and further details about your project right in your mailbox.

Thank you!
Oops! Something went wrong while submitting the form.
August 11, 2023

Creating Serverless APIs with DynamoDB and Lambda

The best time to establish protocols with your clients is when you onboard them.

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

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.

Create user by clicking on the “Add User” button.

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.

Select the list of permissions for the services your lambda function needs access to using the Attach existing policies directly function.

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.

Link to install AWS CLI

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

Add your Access Key ID and Secret Access Key from the file we downloaded on Create User.

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 initnpm 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.

Overall folder structure
Folder structure with files

The code for each of these files are listed below.

serverless.yaml

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.

app.js
routes.js
db.js
getAllProducts.js

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.

Output of serverless deploy

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.

CodeStax.Ai
Profile
August 16, 2023
-
6
min read
Subscribe to our newsletter
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Share this article:

More articles