How to create, publish and maintain high scalable APIs using AWS API Gateway?

Reading Time: 12 minutes

To access data, business logic, and functionalities from backend services, API can act as an interface! Using API Gateway, we can enable two-way communication in real-time applications. In this blog, we will discuss Amazon API Gateway, its architecture, key concepts, use cases, and features. As part of hands-on, we will also create API Gateway and integrate with Lambda service and get the endpoint URL to consume in our client-side code.

In this blog, we will cover:

  • What is an API?
  • What is the Amazon API Gateway?
  • Amazon API Gateway Architecture & Key Concepts
  • Features of Amazon API Gateway
  • Amazon API Gateway endpoint types
  • Types of Amazon API Gateway
  • Benefits of Amazon API Gateway
  • Working with Amazon API Gateway
  • Use cases of Amazon  API Gateway
  • Hands-On
  • Conclusion

What is an API?

An API is an acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Each time you use an app like Facebook, send an instant message or check the weather on your phone, you’re using an API.

When you use an application on your mobile phone, the application connects to the Internet and sends data to a server. The server then retrieves that data interprets it, performs the necessary actions, and sends it back to your phone. The application then interprets that data and presents you with the information you wanted in a readable way. This is what an API is – all of this happens via API.

To explain this better, let’s take a familiar example:

Imagine that you’re sitting at a table in a restaurant with a menu of choices to order from. The kitchen is the part of the system that will prepare your order. What is missing is the critical link to communicate your order to the kitchen and deliver your food back to your table. That’s where the waiter or API comes in. The waiter is the messenger or API that takes your request or order and tells the kitchen for example the system, and what to do. Then the waiter delivers the response back to you; in this case, it is the food.

API

The client uses the following HTTP methods to communicate with the server:

  • GET (to read)
  • POST (to insert data)
  • PUT (to update data)
  • DELETE (to delete data)

What is the Amazon API Gateway?

To answer this, first, we all know that Lambda is a serverless service and it can use DynamoDB as a database for our API and we can create, update, delete, etc. But what if we want our client to be able to invoke this Lambda function in some way? So, there are multiple ways of doing it. First, we can have clients directly invoke our lambda function by giving them proper IAM permissions. Second, we can use ALB between the client and the lambda function, and that would expose our lambda function as an HTTP endpoint. There is one last way we can use an API Gateway.

What is the Amazon API Gateway?

An API Gateway is a serverless service from AWS that allows us to create REST APIs that are going to be public and accessible to the clients. So, the client will talk to the API Gateway. The API Gateway will then proxy the request to our lambda function. We use an API Gateway because it provides us with more than just an HTTP endpoint to connect to other services. It provides us with a lot of features such as Authentication, Usage plans, Development stages, etc.

Amazon API Gateway Architecture & Key Concepts

To understand the API Gateway architecture the components can be decomposed at a high level into

  • Client ( May be  Mobile, Web, or any other service)
  • Types of API based on endpoints ( Edge Optimized, Regional & Private)
  • Integration

API endpoint

The API endpoint is the hostname of the API. It will be in the form <<api-id>>execute-api.<<region>>.amazonaws.com.

The types of API endpoints are

  • Edge-optimized API endpoint
  • Private API endpoint
  • Regional API endpoint 

Integration

The heart of the API Gateway is the Integration that connects the route to backend resources. API Gateway supports the following as integrations

  •  Lambda Function – Can connect via proxy or direct integration
  •  HTTP  – Connect to an HTTP endpoint either inside or outside of AWS
  •  Mock – Quick response without connecting to backend
  •  AWS Services – Can connect to 100+ endpoints inside AWS such as DynamoDB or Kinesis
  •  VPC Link – Connect to ELB via the given VPC link privately.

Features of Amazon API Gateway

We can integrate our API Gateway with lambda, which gives us a fully serverless architecture, which means we do not have to manage the infrastructure.

  • It supports both stateful (WebSocket) and stateless (HTTP and REST) APIs.
  • It has a powerful authentication mechanism through AWS IAM Roles and Policies, Amazon Cognito User pools, and Custom authorizer 
  • Canary release development to roll out the changes without affecting the functionalities.
  • Monitoring of API Usage through Cloud Trail.
  • Monitoring of logs through CloudWatch and can also set alarms.
  • Integrated with WAF (Web Application Firewall)
  • Integrated with AWS X-Ray

Amazon API Gateway endpoint types

There are 3 ways to deploy API Gateway which are called Endpoint Types 

Edge-optimized (default):  This is for your global clients, so this means that your API Gateway is going to be accessible from anywhere in the world and the requests are going to be routed through all the CloudFront Edge locations, which will improve the latency. Your API Gateway is still only in one region, where you created it, but it’s accessible, efficiently, from every Edge location.

Regional: This is when we don’t want to use CloudFront Edge locations. So, it is used when we expect all of our users to be within the same region where we created our API Gateway, and if we wanted to, we could create your own platform distribution, and this will give us the same result as an edge-optimized distribution, but this time, we have more control over the caching strategies, and the CloudFront settings themselves.

Private: A private API Gateway can only be accessed from within our VPC, and it will use interface VPC endpoints for our ENIs. And to define access for an API Gateway, we can use a resource policy.

Types of Amazon API Gateway

HTTP API:

Its Low latency and low-cost API. Currently not offering API management functionality.

REST API:

REST APIs offer API management features such as usage plans, API keys, publishing, and monetizing APIs.

WebSocket API:

WebSocket APIs maintain a persistent connection between connected clients to enable real-time message communication.

API Gateway REST API:

The API Gateway REST API has the full-feature flagship service to build REST APIs and has been available since 2015. But in the re:invent 2019 Amazon introduced a faster and low-cost HTTP API. As implied by the name, the service is about making RESTful web services.

API gateway acts as an entry point to access data, business logic, or any backend services. API Gateway is responsible for accepting and processing up to hundreds of thousands of concurrent API calls. 

A feature that makes the API Gateway REST API service stand out from the crowd is user/tenant-based throttling. This feature allows you to limit the number of requests per user or tenant. It has the functionality that is important for building APIs for a Software-as-a-Service business or any other scenario where 3rd parties interact with your API.

API Gateway HTTP API:

AWS announced HTTP APIs as a modern alternative to REST APIs. Despite the name, API Gateway HTTP API is also about RESTful APIs. AWS promises to deliver lower latency, reduced costs, and improved user experience with HTTP APIs. However, it has missing user/tenant-based throttling capabilities.

API Gateway WebSocket API:

API Gateway WebSocket API offers an event-driven API that allows you to send messages from the client to the server but also the other way around.  The client establishes a connection to the API Gateway, while both sides aim to keep the connection alive. When the client sends an event over the WebSocket, the API Gateway forwards the event to a backend.

The following table clearly analyzes the difference between REST API & HTTP API in various aspects. The developer can choose either of these based on the requirement. 

Types of Amazon API Gateway

Benefits of API Gateway

Decoupling: If your clients which you have no control over communicate directly with many separate services, renaming or moving those services can be challenging as the client is coupled to the underlying architecture and organization. API gateways enable you to route based on path, hostname, headers, and other key information enabling you to decouple the publicly facing API endpoints from the underlying microservice architecture.

Reduce Round Trips: Certain API endpoints may need to join data across multiple services. API gateways can perform this aggregation so that the client doesn’t need complicated call chaining and reduce the number of round trips.

Security: API gateways provide a centralized proxy server to manage rate limiting, bot detection, authentication, CORS, among other things. Many API gateways allow setting up a datastore such as Redis to store session information.

Cross-Cutting Concerns: Logging, Caching, and other cross-cutting concerns can be handled in a centralized appliance rather than deployed to every microservice. In fact, Moesif provides plugins for many API gateways like Kong and Tyk so you can obtain modern customer and API analytics without installing any SDKs.

Use cases of Amazon API Gateway

API Gateway scales from tens of requests to thousands of requests per second without provisioning any infrastructure. It is used by various enterprises to accelerate their business. We will discuss a few of them here to understand the power of API Gateway in real-time.

UK Driver & Vehicle Licensing Agency (DVLA)

DVLA has been gradually digitizing its services for more than 10 years using an API-based approach. With more than 50 million vehicle-licensing transactions per year and records on every vehicle in the UK, DVLA is the definitive source of truth for a huge amount of valuable public information. 

PhotoVogue 

PhotoVogue is an online photography platform launched in 2011 and part of Vogue Italia The main focus of this is to allow upcoming photographers to demonstrate their skills.  The challenge for PhotoVogue is that a very high number of photos are getting published day by day and the existing IT infrastructure was not sufficient to manage this workload. With the implementation of AWS Lambda and AWS Gateway, Photovogue has cut the IT cost by 30% and the operating speed has been increased by 90%. 

Below is the use case diagram where we can see that the Client side code can be used to call the API Gateway through an HTTP endpoint provided by our API Gateway. The API Gateway is then proxying the request to the AWS Lambda service. The Lambda code is then used to do the appropriate CRUD operation by interacting with DynamoDB.

We can have any other services also apart from below as per our requirements. 

Use cases of Amazon API Gateway

Hands-On

In this hands-on, we will see how we can create API Gateway and integrate with Lambda service and we shall get the endpoint URL to consume in our client-side code.

To implement this, we will do the following:

  • Login to your AWS account and navigate to Amazon API Gateway to create a new API.
  • Create a Rest API from the API Gateway console
  • Create a Get Method and integrate it with Lambda

To integrate with, create a lambda function and write the function which we want to call using the API Gateway endpoint. As of now, we will not integrate it with any database and create a function that will return a simple text.

Similarly, under this Get method, create a new resource and integrate it with a new Lambda function.

Based on the resource in the endpoint URL, the appropriate Lambda function will get triggered.

That URL can be used in our client-side code where we can consume the service using REST API.

Log in to AWS and navigate to the API Gateway dashboard to create a new API. Here, we would be able to see multiple options to create API and we can choose anyone based on our requirements. For simplicity, currently, we will be creating a Rest API, so under that click on ‘Build’.

How to create, publish and maintain high scalable APIs using AWS API Gateway?

Enter the name for your API based on your preference.

Under the Actions dropdown, click on ‘Create Method’ and then select GET method. We can create any method such as POST, DELETE, GET etc. But, for simplicity, we are using GET here.

How to create, publish and maintain high scalable APIs using AWS API Gateway?
How to create, publish and maintain high scalable APIs using AWS API Gateway?

Here, we will be integrating with a new lambda function, so select the Integration type as ‘Lambda Function’ and we will be selecting ‘Use Lambda Proxy Integration’ as it allows the client to call a single Lambda function in the backend.

Before proceeding further here, we first need to put a Lambda function name for which let’s create a Lambda function.

So, open the console in a new tab and navigate to the Lambda function and click on ‘Create function’.

Enter the appropriate name for the lambda function and here we are using Runtime as Python 3.8. You can choose any as per your requirement. Now, click on Create Function.

How to create, publish and maintain high scalable APIs using AWS API Gateway?

Now, in the Function code section, put the below function handler code. The body will be called from the API Gateway endpoint URL. We can also get the status code and headers and log it in CloudWatch logs. But, as of now, we would skip looking at the CloudWatch logs. However, we will check the whole response with all details while Testing the API Gateway call.

Code:

After putting the code, click on Deploy and then click on Test. Put an Event name and then click on ‘Create’.

How to create, publish and maintain high scalable APIs using AWS API Gateway?

At the top of the screen, we must see the response and below:

How to create, publish and maintain high scalable APIs using AWS API Gateway?

Now, coming back to our first tab where we created our API Gateway’s GET method.

Put the lambda function name that we just created and click on ‘Save’.

Now, we can test our integration by clicking on TEST

We should now see the response that is getting called from our Lambda Function.

So, we have our API Gateway triggering Lambda function.

How to create, publish and maintain high scalable APIs using AWS API Gateway?

Now, similarly, we can create resource inside our GET method for the data to be called from sub-paths

We have created a resource called ‘states’. You can use it as per your requirement. Once done, click on ‘Create Resource’

Now, again we will create a method for this New Resource ‘states’ and integrate it with a new Lambda function.

How to create, publish and maintain high scalable APIs using AWS API Gateway?

As shown above, create a new Lambda function with the same steps as shown above and put the appropriate code inside the Lambda function handler.

How to create, publish and maintain high scalable APIs using AWS API Gateway?
How to create, publish and maintain high scalable APIs using AWS API Gateway?

Once we are done till here, we can test this method also by clicking on ‘TEST’.

The Response Body should be as per our Lambda Function.

Now, to consume these methods, we can have an HTTP endpoint, which we can call from client side code. To do that, in the Action dropdown, click on ‘Deploy API’.

Here, we can select New Stage Deployment and put the appropriate Stage name such as dev, test, prod. We would now use dev.

How to create, publish and maintain high scalable APIs using AWS API Gateway?

As we can see, we have now got an Invoke URL from AWS.

Copy that URL and try calling it from the browser. We should get the response from the Lambda function that is being triggered by our API Gateway.

Also, test the URL by putting /states in the URL for testing the sub resource that we created above.

If we will try to invoke any other resources which do not exist, we would get the below error.

Conclusion

In this blog, we have seen how the API Gateway helps us in integrating our client-side code with the AWS cloud network. In this hands-on, we have used the Lambda function to get triggered but we can use it with many other AWS services. We will discuss more of API Gateway and its other implementations in our upcoming blog. Stay tuned to keep getting all updates about our upcoming new blogs on AWS and relevant technologies.

Meanwhile …

Keep Exploring -> Keep Learning -> Keep Mastering

This blog is part of our effort towards building a knowledgeable and kick-ass tech community. At Workfall, we strive to provide the best tech and pay opportunities to AWS-certified talents. If you’re looking to work with global clients, build kick-ass products while making big bucks doing so, give it a shot at workfall.com/partner today.

Back To Top