How to create API using AWS API Gateway service - Amazon API Gateway Serverless
Tags: serverless, aws
Published: 2022-07-03 13:09:12

**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](https://aws.amazon.com/api-gateway/)

**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](https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started.html)

_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](https://medium.com/@m.shakeel0581/aws-appsync-graphql-apis-to-invoke-lambda-function-fc9b871e8d10)

**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](https://console.aws.amazon.com/apigateway)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yesdyiswxe6gu724ydf7.jpg)
You can see the new page is open, and there is some defaults options are selected. 

4. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o4k8doh9u95te9rjxv3j.jpg)
(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)

5. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/axyel07b9og982pgjfb1.jpg)

Choose choose method POST and click check Icon.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sihg3cgrf8800s2tgdyf.jpg)

6. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qute8ld2awprr9dhksw.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p79lkeqdu3zlai5u00cl.jpg)

you can see the 'smaple-function' Lambda function are attached.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssxhdfuo05sfyj500uyq.jpg)

7. Now we need to Enable CORS. [You can read about CORS here](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wl9u2h7yzs9x8t7o8m6.jpg)
A Confirm method changes pop-up appear, Click the blue button from pop-up. Its enable the CORS.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iudaj7yfwzla17rqi6zo.jpg)

8. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/165okcna0aqkzrb50ggd.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5l04sthr1dqvzfj5hv3z.jpg)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9q15px7ajptvpa6uhiw.jpg)
as you can see, our REST API run successfully.

## Clean up
1. Sign in to the API Gateway console at [Here](https://console.aws.amazon.com/apigateway)
and choose API 'sample-Api' and click Actions, then choose Delete.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55zjsn16mxito3gp3drg.jpg)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dqsnp8jqyxbwqy4ssor.jpg)