logo
logo
Sign in

How To Build A Serverless API Gateway From Scratch?

avatar
Jessica Wilson
How To Build A Serverless API Gateway From Scratch?

API is a crucial part of any web application tied to customer satisfaction and business growth. Even though there are many approaches to creating APIs, the serverless option has become popular due to its cost-effectiveness, simplicity, and scalability. AWS is the leading serverless provider and has played a big role in deploying serverless API Gateways. These gateways are built with AWS Lambda and other AWS services.

Why AWS Lambda?

AWS Lambda is mainly used for serverless API gateways because it executes commands triggered by specific applications. These triggers could be HTTP calls or events from AWS services like Kinesis, S3, or SNS. Execution of functions happens in some ephemeral containers, fully offered and scaled by AWS. The team can hence focus primarily on code functionality instead of the infrastructure. The pay-as-you-go payment model lets you only pay for the execution time of your functions without idle time.

Serverless API Concepts

  • AWS DynamoDB


AWS DynamoDB stores messages for serverless projects. This database is responsive to serverless principles and is easy to use with pay-per-request cost-effective models. Data is stored in multiple AWS servers under the full management of Amazon.

  • AWS Serverless application model


To further implement your API gateway, you need an AWS account and AWS Serverless Application Model (SAM) already installed and configured. SAM allows developers to create, update and manage serverless applications and all resources required to operate the application. With the AWS Serverless application model, you do not have to develop each service manually with a web console. All you have to do is describe everything required in the special template file.

After running the given commands, select “Quick Start from Scratch” to create a “whiteboard” directory with setup files.

Define the Required resources needed

Start by opening the template.yml file and remove everything under the “resources” function. Before you move to the API, start by creating secondary resources. Define a Dynamo DB table for storing the messages.

The code you informs the AWS to create a DynamoDB table where "partkey" will be a similar partition key for all records. "CreatedAt" will be a range key to allow further sorting by timestamp. Other keys may also be added to the records, but you should not define these.

Define API handlers functions

After defining the API, declare two functions connected to its specific endpoints. One of the functions will be triggered upon a POST request to the "messages" path. The other one is triggered upon a GET request to the same path. The two functions have a 128 MB RAM capacity with a five-second timeout.

Coding the functions

Move to the /src/handlers directory and create files with the functions. The functions will run to respond to POST requests and can parse the author and text from the request body and save data into the database. It will also fill the "partkey" attribute with similar values across all records. Even though it might not be a good practice, it works better for some functions because it lets you sort items by range with the same partition key.

Deployment

Deploying using AWS SAM is easy, and you can do it with one command and minimal inputs. Navigate to the project's root directory and run the deployment command.

collect
0
avatar
Jessica Wilson
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more