Amazon API Gateway With Examples

API stands for “Application Programming Interface”. Amazon API gateway is the front door for your APIs on AWS cloud. An API Gateway provides a set of API endpoints and encapsulates the internal structure without revealing how it is being constructed.

Amazon API gateway process and handles the request and response from thousands and millions of concurrent API calls. Similar to other services on AWS cloud, it is also build to massively scale.

AWS offers Amazon API Gateway service on a very reasonable cost and also available in AWS free tier. It allows you 1 million API calls in a month for free in the free tier and for 12 months in a row. If your application use exceeds the tiers, you simply pay standard, pay-as-you-go service rates.

What is API Gateway?

To understand and use Amazon API Gateway, you need to know what is an API Gateway and its applications.

In a large application, you may have more than one microservice running and all of them exposes some of the API endpoints. You can connect to these API endpoints from the microservice url. But think about when you consume these API endpoints in your UI or give them to some third party. You will end up having many URLs. And for each of the service, you have to manage cache, throttling, etc separately.

API gateway removes having several URLs for REST services and provides you a front door for your APIs. It encapsulates the internal structure of your services. And restricts the direct access to your APIs which makes the APIs secure.

Mostly, API Gateways are a proxy based application which routes the request to the right resources. And take care of all the front door activities like throttling, security, load balancing, logging, authentication, and authorization, etc.

Amazon API Gateway

Amazon API Gateway is an AWS service that is the front door for all your APIs running of AWS cloud. In addition, it offers service for creating, publishing, maintaining, monitoring, and securing REST and WebSocket APIs at any scale.

You can create a REST API with the help of Amazon API Gateway and deploy it instantly. The API you can use in your application or can also make it available for the third party.

Let’s take an example of Amazon API Gateway application to understand it (Source: Amazon Web services):

 

Amazon Api Gateway Application

The example above explains the Amazon API Gateway and how it fits well with other services. So, in this application, the APIs are built on API Gateway and connected to invoke the lambda functions.

If you don’t know about Amazon S3 and how to host public site then please read our post on:

  1. Amazon S3 – AWS Simple Storage Service
  2. Host a host a public static website on Amazon S3?

Note: We will build this application in our next post to give you an end-to-end application overview of Amazon API Gateway, AWS Lambda, and Amazon S3.

Architecture

The architecture of Amazon API gateway is quite simple. API Gateway is the internet facing service which connects the request to the specific API calls. Below diagram demonstrate how API Gateway works:

 

Amazon Aws Architecture (Source: Amazon Web Services)

Why Amazon API Gateway?

Amazon API Gateway offers a number of benefits which makes it one of the highly used services on AWS Cloud platform. Let’s discuss some of many reasons of why use Amazon API Gateway?

REST Endpoints

Instead of creating an application which exposes some RESTFul endpoints and deploying it, Amazon API Gateway offers to create REST endpoints without worrying about the application. Create resource-based APIs and use API Gateway’s data transformation capabilities to generate the requests in the language target services expect.

You can just create the API endpoint and connect them to the Lambda functions.

Security Controls

You can make your API highly secure with Amazon Cognito, Identity and Identity Access Management (IAM). You can execute authorization flow from API Gateway which internally executes the Lambda.

API Versioning

You can deploy multiple versions of the same API and can run them simultaneously with the help of Amazon API Gateway. The best thing is that you only pay for the usage and not for the multiple deployments.

Performance Scale, Traffic Throttle and Cache

Similar to other services Amazon API Gateway offers high scalability with very low latency. Like AWS CloudFront, it takes advantage of the global network of edge location. Read about Amazon CloudFront & Edge Locations.

Traffic Throttle and Cache makes sure that backend APIs are not called unnecessarily. Which makes it highly reliable and available.

Monitoring

Monitoring the API is an integral part of the Amazon API Gateway. A number of metrics available on Amazon CloudWatch to monitor your APIs. You can create dashboards and set alarms and notification in CloudWatch.

Features of Amazon API Gateway

Amazon API Gateway offers a number of features. Some of them we have already discussed in Why Amazon API Gateway” section.

Amazon API gateway Offers features such as the following:

  • CloudWatch Access logging and monitoring which cinludes creation of Alarms and notifications.
  • Supports both stateless and statefull APIs. For example – Stateful WebSocket and Stateless RESTFul APIs
  • Supports custom domain names for your Amazon API gateway – Instead of using the default API gateway URL, you can set and use your domain.
  • Allow multiple version of the same API to run simultaneously
  • Ability to use CloudFormation templates to create and do the whole deployent without doing a single manual configuration
  • Provides Authorization and Authentication mechanism with Conginito and Lambda funcions
  • Caching and request throtteling
  • Single point access to all of your APIs

Setup API Gateway with sample REST API

Let’s create an API gateway and a REST API.

Prerequisite

  • Login to the AWS management console (https://console.aws.amazon.com/)
  • Search for “API Gateway” in services
  • Click on the “API Gateway” service and you will see the below screen to get started with Amazon API gateway
  • get-started-with-amazon-api-gateway-1024x495

    Get Started With Amazon Api Gateway

Get Started

Click on “Get Started” button and you will see the below screen with a sample API specs in Swagger 2.0

Click on “OK” to import the selected API.

Choose to create

  1. Choose the protocol – Select whether you would like to create a REST API or a WebSocket API. We will choose “REST” option for the example.
  2. Create new API – In Amazon API Gateway, a REST API refers to a collection of resources and methods that can be invoked through HTTPS endpoints.
    We will choose the “Example API”. You can also create a New API or import from the Swagger Json for existing API.
  3. Example API – The sample API Json that will be used to crate your first API.

You may ignore rest of the options for now and click on “Import” button. See the screen below:

 

Import Example Api

Deploy the API

AWS will take few seconds to create API and Gateway. Once done, you will see the below screen:

If you want to add more API end points then you can do so. Click on the “Action”
button and you will see the below options:

 

Api Actions

In the API Sections, click on “Deploy API” to deploy the API. The below window will prompt to enter the stage that you want to deploy.

View Deployed API

Once you click on the “Deploy” button, AWS will take few seconds/minutes based on the number of API endpoints to deploy, please wait while deployment completes. Once done, you will see the below screen:

 

Api Gateway And Api

You can see the URL for your API: https://2ikmujgy4g.execute-api.us-east-1.amazonaws.com/stage.
Click on the in the new browser window and you will see the below documentation:

Connect to Your API

You can use postman to connect to your API OR just connect through the curl command.

Connect To Api

By admin

Leave a Reply

%d bloggers like this: