How To Create An HTTP API In AWS API Gateway
Uriel Bitton
AWS Cloud Engineer | The DynamoDB guy | AWS Certified | I help you supercharge your DynamoDB database ????
Build an HTTP API in a matter of minutes not hours — with AWS API Gateway.
Back in the day, creating REST or HTTP APIs used to take time and deep expertise.
You needed to spin up and host your server code on-premise or even on a cloud-provider. You also needed experience writing the code in a backend language like Node JS or Java.
Now with AWS’s API Gateway, you can create and deploy APIs within minutes with no coding experience.
In this article, I’ll guide you through setting up an HTTP API with AWS API Gateway and deploying it for free, easily and within minutes.
Let’s get started !
HTTP vs REST API
Before we start with HTTP, you might be wondering why are we choosing HTTP over REST and what are their differences?
To be clear, HTTP isn’t better than REST and choosing one depends on your use case (like everything in software development).
HTTP in general has lower latencies and faster to start with quickly.
However for more complex requirements such as throttling, rate-limiting and route-level CORS configuration, REST APIs offer all of these while HTTP APIs do not.
For a deeper dive into their differences, pros and cons, check out this article.
Getting Started With an AWS HTTP API
Let’s start building our first HTTP API.
Navigate to your AWS console and search and click on the API Gatway service.
Click on Create API to get started.
Here you can choose the first option HTTP API, and click the build button.
In the next page, you can name your API and then proceed to create it.
I’ll name mine university-api, to model a university API use case.
Keep clicking on to the next pages. You can leave the default in the Define stages page — leave it on auto-deploy.
Hit Create when you’re done.
Routes Structuring
Once created, you’ll see the routes page.
Let’s create our first route.
Click on the Create button (in the blue border).
On this page, we’ll create a simple route to GET a student by their studentID:
Here we use the GET method and specify a user path with a dynamic studentID variable. This variable will be any value we pass as the path parameter after “/students/…” (we’ll understand this when we use it in our server code below).
Click on the Create button to create this route.
We now have a new route:
Let’s create one more endpoint to be able to create a new student.
Click on the Create button again.
This time use the POST method:
Now we have our two endpoints to get and create users, let’s connect them to Lambda functions.
AWS Lambda Function
In a new tab, open up the AWS Lambda service console.
Let’s create a new function to integrate with our students endpoints.
Use the following configuration for this function:
领英推荐
Create the function.
In the code editor below, copy the following code:
export const handler = async (event) => {
const { studentID } = event.pathParameters;
if (!studentID) {
return {
statusCode: 400,
body: JSON.stringify({
message: "Bad request - studentID is required",
})
}
}
return {
statusCode: 200,
body: JSON.stringify({
message: "Students endpoint request succeeded",
studentID,
})
}
};
We’ll provide a simple function that will return the studentID from the path parameters if it is defined.
If it is not defined, our server function will return a 400 “bad request” error.
When we are done we need to click on the Deploy button on the left-hand side.
We can now move on to the last part of integrating this function with our API endpoint.
Creating the endpoint integrations
Return to the API Gateway service in AWS.
Back in our university-api, with the /student/{studentID}/ GET endpoint selected, click on the Attach Integration button, in the route details.
Click on Create and attach integration again on the next page.
Next you can create the integration — choose Lambda as the integration type.
Search and select the Lambda function “students-test” that we just created and hit the Create button at the bottom.
Once you create the integration, you will see a green badge under that endpoint:
Testing Our API
All that remains now is to test our endpoint.
On the left-hand sidebar, under Deploy click on stages.
Make sure to select the $default stage and on the right you’ll see Stage Details.
Here you can find the Invoke URL.
Copy that and visit the URL in your browser.
You’ll get a page with a JSON object:
{“message”: “not found”}.
Now add the following path to the end of that URL:
/students/123
(here “123" is the studentID).
Now we get a successful response from our server:
And if we omit the userID and pass in only /students path, we’ll get the error we wrote in the Lambda function:
With this, you can continue experimenting with the POST route we created for students.
Congratulations, you’ve created your first HTTP API!
Best Practices
Some beginner best practices to keep in mind.
Conclusion
Creating an HTTP API with AWS API Gateway is a quick and simplified process, reducing the time and expertise required compared to non-cloud API designing.
In this article I guide you through creating a basic HTTP API, integrating it with Lambda functions, and testing it, all within minutes and without the need for extensive backend coding.
Follow along to learn how to build your own APIs.
If you have any questions, don’t hesitate to add them in the comments!
Simplifying System Design
2 周Supper detailed, Thanks for sharing Uriel Bitton
Pragmatism better than Astigmatism!
2 周Hi Uriel Bitton! Great to know you have expertise in Serverless architecture too! Would be fun to interact on this stuff ??