Build a Scalable Backend Service with AWS: The Power of Serverless Architecture
Build a Scalable Backend Service with AWS

Build a Scalable Backend Service with AWS: The Power of Serverless Architecture

Gone are the days when creating a backend server for your application meant setting up and maintaining costly infrastructure. With the advent of cloud computing and serverless architecture, developers can now build scalable and cost-efficient backend services using a variety of cloud-based tools and services.

One such cloud provider is Amazon Web Services (AWS), which offers a suite of powerful tools for building serverless backends, including AWS API Gateway, AWS Lambda, and AWS DynamoDB. These services allow developers to build highly scalable and flexible APIs, handle requests and responses efficiently, and store data securely, all without the need for a traditional server. So, if you’re looking to build a powerful, cost-effective backend for your next application, it’s time to consider going serverless with AWS.

No alt text provided for this image

We will go with a reverse flow,

  1. Create?DynamoDB?Table and insert data in it
  2. Create a?Lambda?function to get data from the DynamoDB
  3. Create an HTTP API using?API Gateway?to invoke the created lambda function.


Create DynamoDB Table

First of all, we will create a DynamoDB table named “Users”. It is very simple to create a table in DynamoDB.

  1. Write a?Table name?and select a?Partition key?for the table. In our case, we have a Table name of?Users?and partition key of?id.

No alt text provided for this image

2. Click on the Create Table button.

No alt text provided for this image

That’s it!!! You have created a DynamoDB table.

Now, we are going to populate Dummy Data to the?Users?table. Which would look like this,

No alt text provided for this image

Create a Lambda Function

Now, we are going to create a lambda function which is also very simple to do.

First of all, go to the?AWS Lambda?service in the?AWS Console?and click on?Create Function?button.

Where you have to set the?Function Name, and Runtime Environment,?and?click on the?Button?Create Function.

In our case, we have?getUsers?as a function name and?Node.js 18.x?as a Runtime.

No alt text provided for this image

Congratulations!!!?You have created a Lambda function, which was quite simple. Right?

No alt text provided for this image

Now, we have to write a code that would get all the users from the?DynamoDB Users Table?and return it.

Code

import AWS from 'aws-sdk';

//document client
const ddb = new AWS.DynamoDB.DocumentClient({ region: 'us-west-1' });

export const handler = async (event) => {
  //params
  const params = {
    TableName: 'Users',
  };

  try {
    const result = await ddb.scan(params).promise();
    const users = result.Items;

    // success response
    const response = {
      success: true,
      message: 'Successfully got all the users!',
      users,
    };

    return response;
  } catch (err) {
    // error response
    const errResp = {
      success: false,
      message: err.message,
    };
    return errResp;
  }
};
        

We would write that code into the?Code?section of the Lambda Function and then Click on the?Deploy?button to Deploy it.

No alt text provided for this image
Deploy Lambda Function

It Done!?We are now at the last step, which is to create an HTTP API.

Create an HTTP API using API Gateway

This is the final step to complete our serverless backend service using AWS services. First of all, Go to?Amazon API Gateway.

No alt text provided for this image

It would ask you to select the Type of an API you want to build. There are four types of API available,

  1. HTTP API
  2. WebSocket API
  3. REST API
  4. REST API?(Private) (This is only accessible from within a VPC)

We are going to use the most?HTTP API?for our demo because it is straightforward.

  1. After clicking on the?build button?of?HTTP API.?We have to select an?Integration?and In our case, we are using?Lambda.

No alt text provided for this image

2. You have to select three things.

  • AWS Region
  • Lambda Function ( which we created earlier)
  • API Name

In our case, we have?us-west-1?as a region where we have created a lambda function named as?getUsers?and selected that one to integrate with the API. At last, just use an API Name like we have used the name of the?Get User List.

After filling out the form, just click on the?Next Button.

By clicking on the?Review and Create?Button, the default values for the APIs would be set.
No alt text provided for this image
HTTP API Integrations

3. Next we have to configure routes, first, select a?Method,?resource path, and?integration target.

More routes could be added!

No alt text provided for this image
Configure Routes

4. Final Step is to?Define Stages,?by default the stage name is set to?$default, and?multiple stages could be added to represent different environments like production or development. So, we are good to go. Just click on the?Next button.

No alt text provided for this image
Define Stages

Everything is done to create an HTTP API. Just Review it and click on the?Create button?to finally create an HTTP API.

No alt text provided for this image
Review and Create API

Congratulations!!! You have created an HTTP API.

No alt text provided for this image

After its creation, you would get an endpoint of the API, which would look like this,

No alt text provided for this image

To obtain the desired output from the API, you may append the created route to the provided endpoint and make a request. Like,

https://fx5x12.execute.api.amazonaws.com/users

Summary

So far, we have created a serverless backend using Amazon Web Services which are API Gateway, AWS Lambda, and DynamoDB. First, we created a DynamoDB table and inserted the dummy data into it. After that, we created a Lambda function that gets the list of users from the DynamoDB table and returns it. At last, we created an HTTP API and connect it with the lambda function we have created before. It was just a demo of creating a serverless backend service with AWS, a lot more can be achieved by using multiple services of AWS. It all depends on the need of your application.


If you found this article helpful, please show your appreciation by giving it a clap ??.

Umer Farooq

CTO at MRS Technologies | Building High-Performing Software Teams that Deliver Results | Driving Business Growth and Efficiency for Clients Worldwide ??????

2 年

Great blog. Blogs that are easier to read with clear images are the best.

Huzaifa Asif

Engineering Lead | Solution Architect | Cloud Engineer | FinTech | SaaS | PaaS | AWS | Azure | GCP

2 年

Very detail and helpful article ??

要查看或添加评论,请登录

Asim Hafeez的更多文章

社区洞察

其他会员也浏览了