AWS Serverless : API Gateway using Lambda

Topics Covered in this BLOG

1.What is an API ?

2.What is AWS API Gateway?

3.Who uses API Gateway?

4.Building an API Gateway using Lambda

API

API stands for “Application Programming Interface.” It is a set of rules and protocols that define how two or more software applications can interact with each other. APIs allow different software systems to communicate with each other and share data and functionality.

APIs are often used in web development, where they enable web applications to communicate with other systems or services, such as retrieving data from a database or sending a message to a messaging service. APIs are also commonly used in mobile development, where they allow mobile apps to access data and functionality from other systems or services.

In general, APIs are an important tool for enabling software systems to interoperate and work together, and are widely used in modern software development.

AWS API Gateway

Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. With API Gateway, you can create RESTful APIs that enable real-time two-way communication applications.

With Lambda and the API Gateway, you can create a RESTful API that can be accessed by clients over the internet. The API Gateway will route incoming requests to the appropriate Lambda function, which will execute the code and return a response to the client. This allows you to build scalable, serverless applications that can handle a high volume of traffic.

Who Uses API Gateway ?

API gateway is basically used by two types of developers that is the API developers and APP developers .

An API developer is someone who creates and deploys APIs to enable the required functionality in an API gateway.

An app developer builds a functioning applications to call AWS services by invoking a WebSocket or REST API created by an API developer in the API Gateway ,

API Gateway using Lambda

To create an API using Lambda and the API Gateway, you will first need to define the functions that will be executed in response to incoming requests. You can do this in a programming language of your choice, such as Node.js, Python, or Java.

Create Lambda Function

Step 1: Go to the AWS Console and move on to Lambda function

Step 2: Click Create function ,select Author from scratch and give a function name .Choose Runtime as Python and leave rest as default and click create function .

By Default there will be small code added at the code pane of your lambda function . For more details about lambda function refer to this link .

import json

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Once you have defined your functions, you can create an API Gateway and connect it to your Lambda functions. This will allow you to specify the HTTP methods (e.g. GET, POST, PUT, DELETE) and endpoints that your API will support.

Create API gateway

Step-1 : Go to API gateway console by searching it in Management Console

Step-2 :Click to create a Rest API

Step-3 : Click New API and under settings give a API name and Endpoint as Regional and then click Create API

Step-4 : After creating API you will see Under Resources there is nothing but just a slash this basically depicts the root level of the resource ,which corresponds to the base path URL for your API

Step-5 : Click on the Actions menu and under actions click on create method ,when you do this you will see that a new drop down menu pop’s up below the root directory this list consist of basic API methods such as GET ,POST ,PUT ,Delete and etc . Select the get method and click on the tick mark on beside in order to save the choice that you made ,

Step-6 :After that there will be set-up pane opened beside and by default the integration type would be Lambda Function ,Click the Lambda proxy integration and choose the region as same as the region where lambda function is created (In my case it’s us-east-2(ohio) )

Step-7 :In Lambda function type your lambda function name , if you forget your lambda function name just type a random letter and press backspace and you will get a dropdown of your functions and click your Lambda function.

Step-8 : Once you Click save you will be popped notifying to add API gateway permission to invoke the lambda function which is the main thing we require , click ok .

Step-9 :In the next Pane you would see GET-Method Execution consists of number of items

  • The client box over here represents the client which is the browser or the application that calls your API’s get method . If you click on test link and then click on test this simulates a get request from client .
  • The method request box represents the clients get request as it’s received by API gateway if you choose method request you’ll see settings for things like authorization and modifying method request before it’s passed in back end as an integration request . Leave it default
  • The Integration request box represents the get request as it’s passed to the back end . Here the settings depends on the integration types that is selected . Integration type is set to Lambda functions and others are default .
  • The next box represents the backend lambda function that we just created , if we click on this it will open the lambda function in lambda console .
  • Integration response box represents the response from the back end before it is passed to the client as a method response . For Lambda proxy Integration this entire box is greyed out ,because a proxy integration returns the lambda function’s output as it is for a lambda non proxy integration you can configure the integration response to transform the lambda functions output into a forms that’s appropriate tot he client’s application for now let’s set to the default values.

  • The method response box is response that’s returned to the client as an https status code and an optional response header and an optional response body by default the response body that’s returned to your lambda function is passed through as it is as a json document so the response body default setting is application/json with an empty model indicating the body is passed through as it is for this section as well leave everything as default .

Until now we created a API but we cannot actually use it ,this is because it needs to be deployed

Deploy your rest API in the API gateway console .

To deploy the above API we created , Click on the Actions drop down menu and choose deploy API

In Deployment stage click [New Stage] , give a stage name (In my case it’s “Production”) and then click deploy .

In the production stage editor at the top you could see the Invoke URL .

If you choose to invoke the URL it will open a new browser tab with that URL

We can also create API gateway directly through the lambda service .

Move on to the lambda console and select the lambda function you have created .

we could already see a a trigger that API Gateway is already created by following the above steps .

Click on add trigger and choose and API Gateway in drop down menu

Then Select create an API option and API type as HTTP API and security can be give open as of now since this is just for demonstration purposes , for real time application it’s not recommended to keep security option as open .

Then Click on ADD and when you come back to the Lambda function you could see there’s 2 API gate-ways for this function created .

Click on the Configuration and click on the API endpoint link and we can see the function returns “Hello from lambda!”.

In similar way you can define your own function in Code pane of the Lambda function and you can test it and deploy it. When ever there’s changes made in the code make sure you deploy so that the actual changes will reflects in API endpoint link .

You can then monitor and manage your API using the API Gateway’s management console, where you can view metrics, set up logging and caching, and configure other settings.

Click on the Monitoring page to see the metrics and we can see the corresponding metrics for the lambda function through the API that we have just created .

Overall, using Lambda and the API Gateway allows you to create powerful, scalable, and secure APIs that can be accessed by clients over the internet. It is a popular choice for many developers and organizations, and can be a great way to build serverless applications on the AWS platform.

I hope you found this blog helpful and insightful. Thanks for reading.