Build a Scalable Backend Service with AWS: The Power of Serverless Architecture
Asim Hafeez
Senior Software Engineer | Lead | AI | LLMs | System Design | Blockchain | AWS
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.
We will go with a reverse flow,
Create DynamoDB Table
First of all, we will create a DynamoDB table named “Users”. It is very simple to create a table in DynamoDB.
2. Click on the Create Table button.
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,
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.
Congratulations!!!?You have created a Lambda function, which was quite simple. Right?
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.
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.
It would ask you to select the Type of an API you want to build. There are four types of API available,
We are going to use the most?HTTP API?for our demo because it is straightforward.
2. You have to select three things.
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.
3. Next we have to configure routes, first, select a?Method,?resource path, and?integration target.
More routes could be added!
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.
Everything is done to create an HTTP API. Just Review it and click on the?Create button?to finally create an HTTP API.
Congratulations!!! You have created an HTTP API.
After its creation, you would get an endpoint of the API, which would look like this,
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 ??.
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.
Engineering Lead | Solution Architect | Cloud Engineer | FinTech | SaaS | PaaS | AWS | Azure | GCP
2 年Very detail and helpful article ??