Using AWS API Gateway to Build an End-to-end HTTP API

About this article

This article goes in detail on how to get started with AWS API Gateway. We will be focusing on building an end-to-end HTTP API for users to request information from the database as well as allowing users to create new data into the database. By the way, this is my first blog. Hope it can help you!

If you are just getting started with AWS API Gateway, some of the concepts you might want to refresh/understand before going ahead —

In IoT, there are 2 communication APIs –

  • REST Based Communication APIs

It depends upon the HTTP methods such as GET, POST, PUT, DELETE to retrieve the data. A new TCP connection will be set up for each HTTP request.

  • Web Socket Based Communication APIs

It is Bi-directional. Messages can be received or sent by both server or client. It depends upon the IP address and port number to retrieve the data. Once the connection is set up the messages can be sent and received continuously without any interruption.

HTTP & REST APIs

API stands for Application Programming Interface. The first version of API Gateway was introduced in 2015. The first version of the API Gateway is referred to as REST APIs which is heavy with all the bells and whistles. During reinvent in 2020, Amazon introduced a new flavor of the API Gateway, called HTTP APIs. HTTP APIs were designed from the lightweight and thus, are supposed to be faster and cheaper than REST APIs.

Pricing

The tables below show the pricing for both HTTP & REST APIs for the us-east-1 region. As shown below, HTTP APIs are significantly cheaper than REST APIs.

HTTP APIs price
REST APIs price

Which one should I choose?

If HTTP supports everything that you’re trying to use and there’s no key functionality that’s missing definitely go with HTTP otherwise you can only switch to REST. Check more feature details between these two APIs from the Amazon API Gateway page.

Now the most exciting part is coming! Let’s create an HTTP API using AWS API Gateway right away.

Hands on Lab Tutorial

1. Create a Lambda function

Here we’ll implement the requests in Lambda function. Since we haven’t built one. I’ll create a lambda function for further configuration.

Click Lambda service>Functions>Create function

Defining your function name, select the latest python version that is listed on Runtime, others leave as default. Then click Create function.

2. Create an HTTP API

Click API Gateway service> APIs>select HTTP API> Build

Select Lambda, Region and the lambda function name we just created. Define your own API name, then click Next

Select GET method and edit the Recource path to /getPerson;

Add one more POST method and edit the Recource path to /createPerson, then click Next, leave the setting on the page default, click Next and then Create.

3. Edit the Lambda function code

Before we edit the code, let’s go to see how the API we just created looks like.

In my Person_API is https://h4klx2l6yj.execute-api.us-east-1.amazonaws.com/getPerson. Here I use the Postman(download it here) to send a request to the API. The Invoke URL can be copied as below in the API. Copy and paste yours into the postman. Here I also added the key personid and value 123

3.1 Build a GET method response

It returns the same message from Lambda function. Next, we’ll print the event in our Lambda function, deploy it, send the same request from postman and go to see the event details in CloudWatch.

Click CloudWatch>Log groups>/aws/lambda/myAppFunction>select the latest log as below

We can see there’re a lot of details in our event. To process it look prettier. I copied and pasted the JSON file to the JSON FORMATTER.

We can see the “rawPath” is /getPerson and the “personid” is 123.

Next, we’ll edit the lambda to send the response back to the request including the person 123’s info detail.

Now let’s use the postman to send the request again with personid=123 and get the response from API.

Nice! We successfully build a API with GET method and send the request back to the user. Next, I’ll teach you how to add a POST method to allow users to add their info into your dataset via API.

3.2 Build a POST method response

Similarly, before starting, we have to look at the detail of event in Cloud Watch.

And then use the postman to send a POST request with person’s detail to createPerson as below

Copy and paste the event log from CloudWatch to JSON FORMATTER

Here we can see the details are sent to the “body”, thus we can edit the Lambda function to return the info(new person id) back to user. Here I import uuid package to randomly create a person id

Finally, we apply the postman to send a createPerson request to the API and see the response.

Awesome!!! Now we have successfully built the HTTP API with GET and POST functions for users based on Lambda function.

Closing Notes

Hope you have learned something from this article. If you have any questions, please leave your comments below. Thanks for reading!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store