Introduction to Amazon API Gateway using AWS Lambda
What is an API?
In simple terms, API is a messenger; let’s understand this with some examples. Let’s say you are hungry and you need to cook something at home. If you want to make noodles, you just take the ingredients out of the cupboard, fire up the stove, and make it yourself. This is a program doing something on its own with its own resources. But say you want pizza – you don’t have the ingredients, and your home oven isn’t really that suited for making a nice crispy crust. So you go to a pizza place instead. But unlike at home, you can’t just go into the kitchen and start using their ingredients to make a pizza. They don’t want your grubby hands all over their stuff! So what will you do? You have to go to the counter and make an order – there will be a menu listing what pizzas you can order and what toppings or other options you can pick. This is an API.
An application Programming Interface (API) is a set of rules specifying how two software programs should interact.
An API allows one software program to access the functionality of another software program. For example, if you have a software program that needs to retrieve data from the internet, it can use an API to request that data from a server. The server will then respond with the requested data, and the software program can use it as needed.
APIs are used to allow different software programs to communicate with each other and share data and functionality. They are an important part of modern software development, as they allow different systems to work together and enable the creation of more complex and powerful software applications.
HTTP API is a type of API that uses the HTTP protocol for sending and receiving data. It allows software programs to send and receive data using HTTP requests and responses. These requests and responses can be in various formats, such as plain text, JSON, or XML. They are commonly used in a wide range of applications, including web applications, mobile apps, and microservices. These APIs are relatively cheaper than REST APIs and have less functionalities than the later.
REST API is a type of API that follows a set of architectural principles called REST (Representational State Transfer). REST is a style of software architecture that defines a set of constraints for creating web APIs.
A stateful API is an API that maintains information about each client request and uses this information to process subsequent requests. This means that the API stores data about each request, such as the request parameters, in a server-side session. This data is then used to process subsequent requests from the same client.
A stateless API, on the other hand, does not maintain information about client requests. It processes each request independently, without storing any data about previous requests. This means that the API does not maintain a server-side session and does not use data from previous requests to process subsequent requests.
Key Differences between Stateful and Stateless APIs
Below are some key differences between stateful and stateless APIs:
|1. Require a server-side session to store data about client requests||1. Do not require a server-side session to store data about client requests|
|2. These may sometimes be slower than Stateless APIs because they require and store data which takes time.||2. Stateless APIs are faster because these do not require and store data about requests.|
|3. Stateful APIs are not easy to scale||3. Easier to scale as they do not need to maintain data about previous requests.|
|4. Generally considered less secure than Stateless APIs||4. These are generally considered more secure.|
What is Amazon API Gateway?
Amazon API Gateway is an AWS Service that is used to create, maintain and monitor both stateful (websocket) and stateless (HTTP and REST) APIs. We can use these APIs to access:
- Any AWS service
- The data stored in the AWS cloud (such as an S3 bucket)
- Any other web services.
If you are an API developer, you can easily make your APIs to third-party developers too.
There are mainly two types of users of AWS API Gateway.
- i) API Developers who create and deploy an API to enable the required functionality in API Gateway
- ii) App Developers who are customers of the API developer.
Amazon API Gateway Architecture
This architecture illustrates how serverless applications can be built with consistent and integrated developer experience. From end users to the data centers, API Gateway handles all the tasks involved, such as accepting and processing thousands of concurrent calls, traffic management, authorization, monitoring, access control, etc.
Amazon API Gateway Features
- It supports both stateful and stateless APIs. (Examples: Websocket, HTTP, and REST).
- Powerful authentication mechanisms include AWS Identity, Access Management Policies, and Lambda authorizer.
- Developer portal where APIs developers can publish their APIs.
- Execution and access logging for CloudWatch, including the option to set alarms.
- Integration with other AWS Services such as AWS Lambda and AWS Kinesis.
- Integration with AWS WAF is used for protection against web exploits, and AWS X-Ray is used to understand and emphasize performance latencies.
Getting Started with Amazon API Gateway
Now we are going to create a serverless API. In a serverless API, we can focus on our applications instead of spending time managing servers. It works like this:
- API is invoked by the client
- API sends request to lambda
- Lambda executes the lambda function and sends the result back to the API
- After receiving the result from lambda, API responds to the client
Step 1 – Create a lambda function
The Lambda function is used for the backend of our API. Lambda runs the code only when it is needed. It also scales automatically from a few requests per day to thousand requests every second.
Creating a lambda function:
1) Go to the lambda console at https://console.aws.amazon.com/lambda
2) Click on Create function.
3) Enter “my-function” as the function name.
4) Choose Python 3.9 as runtime.
5) For this function we won’t need to change anything in the Permissions and Advanced Settings tab as the default permissions are sufficient for this demonstration.
4) Click on Create function.
STEP 2 – Creating an HTTP API
The HTTP API provides an HTTP endpoint for your Lambda function. It works like this:
- Client submits query to the HTTP API
- The API triggers lambda which then executes the lambda function
- Lambda sends the result to the API
- API responds to the client with the received response
Steps to create a HTTP API
1) Visit API Gateway console at https://console.aws.amazon.com/apigateway
2) Click on Build to create your first HTTP API
3) Click on Add integration for integration
4) Select Lambda.
5) Enter your lambda function my-function.
6) For API name, enter my-http-api.
7) Click on Next.
8) Review the route created for you, and then choose Next.
9) Review the stage created for you, and then choose Next.
10) Click on Create.
STEP 3 Test your API –
Next, we need to test our API to make sure that it’s working. For this we will use a web browser to invoke our API.
To test our API
- Go to the API Gateway console at https://console.aws.amazon.com/apigateway
- Select your API.
- Note down your API’s invoke URL. (see the image above)
- Copy your API’s invoke URL, and paste it in a web browser. Join the invoke URL and the name of your lambda function to call your Lambda function. The API Gateway console creates a route using the name of your Lambda function “my-function” by default.
The full URL should look like https://abcdef123.execute-api.us-east-2.amazonaws.com/my-function.When you load this URL, a GET request is sent by your browser to the API
- You should see the text “Hello from Lambda!” in your browser. Hence your API’s response is verified.
We have created our first HTTP API using Amazon API Gateway and AWS Lambda. Further, we can explore various use cases of the same, and similarly, we can create REST API also, which provides more functionalities.
Did you like this article? Tell us your thoughts in the comment below. Also, don’t forget to mention which article you’d like to read next.
Leave a Reply Your email address will not be published. Required fields are marked *