DEV Community

Cover image for How to create API using AWS API Gateway service - Amazon API Gateway Serverless
Muhammad Shakeel
Muhammad Shakeel

Posted on • Updated on

How to create API using AWS API Gateway service - Amazon API Gateway Serverless

Hello everyone!, this is Muhammad Shakeel, I'm going to write my First story on dev.to. so today I'm talking about AWS API Gateway. API Gateway are very useful and give us many advantages like

  • Efficient API development
  • Performance at any scale
  • Cost savings at scale
  • Easy monitoring
  • Flexible security controls
  • RESTful API options

You can read more about AWS API Gateway here

Get started with Amazon API Gateway for REST APIs
so we getting started exercise, you create a serverless API. Serverless APIs let you focus on your applications, instead of spending time provisioning and managing servers. Amazon API Gateway provide three types of APIs

API type

1. HTTP API
HTTP APIs are designed with minimal features so that they can be offered at a lower price.The HTTP API provides an HTTP endpoint for your Lambda function. API Gateway routes requests to your Lambda function, and then returns the function's response to clients.more about HTTP API

2. WebSocket API
WebSocket APIs maintain persistent connections with clients for full-duplex communication

3. REST API
A REST API in API Gateway is a collection of resources and methods that are integrated with backend HTTP endpoints, Lambda functions, or other AWS services. API Gateway REST APIs use a request/response model where a client sends a request to a service and the service responds back synchronously.

4. REST API Private
REST API that is only accessible from within a VPC.

Setup REST API

I'm going to create REST APIs. API Gateway also supports HTTP APIs and WebSocket APIs, but an REST API is the best choice for this exercise. REST APIs support more features than HTTP APIs.

Create Lambda function
You use a Lambda function for the backend of your API. Lambda runs your code only when needed and scales automatically, from a few requests per day to thousands per second.

Lambda functions and Lambda layers See the full story before going on

Create REST API
An API endpoint can be

  • Edge-optimized API endpoints(best for geographically distributed clients. API requests are routed to the nearest CloudFront Point of Presence)
  • Regional API endpoints (intended for clients in the same region. When a client running on an EC2 instance calls an API in the same region, or when an API is intended to serve a small number of clients with high demands, a regional API reduces connection overhead)
  • Private API endpoints(API endpoint that can only be accessed from your Amazon Virtual Private Cloud (VPC) using an interface VPC endpoint)

The taking after endpoint sort changes are supported

  • From edge-optimized to regional or private
  • From regional to edge-optimized or private
  • From private to regional

But You cannot alter a private API into an edge-optimized API

we are goin to create REST API with Edge-optimized API endpoints for the best performance.

  1. Sign in to the API Gateway console at Here

  2. Choose create API.

  3. As you can see there are 4 types of APIs. From REST API, choose Build.

Image description
You can see the new page is open, and there is some defaults options are selected.

  1. In the Settings portion write API name 'sample-Api', Description ( Optional, you can leave it empty ) and choose Endpoint type 'Regional' and Hit the save.

Image description
(You can also Clone from existing API by choosing this option form the portion above the setting and Select API that you want to clone)

  1. Now we need to create and deploy the resource and method. In the Resources tab, click on Actions button and choose Create Method

Image description

Choose choose method POST and click check Icon.

Image description

  1. Now we need to setup POST method. Choose Integration type Lambda Function and in Lambda Function choose sample-function by typing the input field. Press the save

Image description

a pop-up appear with name 'Add Permission to Lambda Function', click 'Ok' for add to give API Gateway permission to invoke your Lambda function.

Image description

you can see the 'smaple-function' Lambda function are attached.
Image description

  1. Now we need to Enable CORS. You can read about CORS here Click Actions and choose 'Enable CORS'. There are some defaults values are selected, don't need to change them and click Blue right Button.

Image description
A Confirm method changes pop-up appear, Click the blue button from pop-up. Its enable the CORS.

Image description

  1. All steps has been done, We just need to be deploy out API to Stage. Click again Action button and choose 'Deploy API' from API ACTION. A Deploy API pop-up appear, select [New Stage] from Deployment stage option. Type stage name 'sampelStage', Type Stage description (optional) and Deployment description (optional). Click blue button Deploy.

Image description

All step has done now we need to test our API. Copy the Invoke URL from Stages tab, that appear on top. you can also see that our created stage are shoeing in this page.

Image description

How does it work!?

Once the Lambda function with REST API has been setup, The REST API from AWS API gateway could be invoked similar to how any other REST APIs are invoked with CURL, Postman.

Lambda function latest code

exports.handler = async (event) => {
return {
"Status": 200,
"Message": "Helo From Sample Lambda"
};
};

Test REST API in postman

Image description
as you can see, our REST API run successfully.

Clean up

  1. Sign in to the API Gateway console at Here
    and choose API 'sample-Api' and click Actions, then choose Delete.
    Image description

  2. An Delete API pop-up appear, click delete from this pop-up.

Image description

Top comments (0)