Building REST API in AWS
REST stands for Representational State Transfer. A REST API (also known as RESTful API) is an application programming interface (API or web API) that defines a set of rules in order to create Web Services.
Why use REST API?
Let’s consider a scenario where you are using the reSkill web app. Now obviously, this application needs a lot of data, as the data present in the application is never static. Either it is a new “challenge” getting added on a frequent basis, or various challenges getting announced at the same time which implies the fact that data is always changing in these applications.
Now, where do you think we get this data from?
Well, this data is received from the Server or a Web-server. So, the client requests the server for the required information via an API and the server sends a response to the client.
So, let’s see how the Client is interacting with the Server (AWS cloud in this case), in which language and more importantly what they are talking about ? I’m so excited, are you?
The Amazon API Gateway supports building different kind of APIs:
HTTP API —
With HTTP APIs, you can easily create RESTful APIs with low latency and lower cost than REST APIs.
HTTP APIs support OpenID Connect and OAuth 2.0 authorization. They come with built-in support for cross-origin resource sharing (CORS) and automatic deployments.
WebSocket API —
You can create a WebSocket API as a stateful frontend for an AWS service (such as Lambda or DynamoDB) or for an HTTP endpoint.
The WebSocket API invokes your backend based on the content of the messages it receives from client apps.
REST API —
An API Gateway REST API constitutes resources and methods. A resource is a logical entity that an app can access through a resource path. A method corresponds to a REST API request that is submitted by the user of your API and the response returned to the user.
Click on Build, and it will give you three options to create a REST API.
We will be going with the first option which is the New API. Provide the name, description(optional) and endpoint type for your API. I will be selecting the endpoint type as Edge optimized as it is best for geographically distributed clients. Edge-optimized APIs are endpoints that are accessed through a CloudFront distribution that is created and managed by API Gateway.
Example- let’s say a user is accessing an API from US and another from Japan then for both the users, the API will serve the request from the nearest CloudFront Point of Presence.
Select the type of API you want to create and click on “Create API”.
Once you complete the create process, you will see a bunch of things on your screen. Then click on the Action button and select Create Resource option from the drop down. As a result, an empty API is created.
As a next step you will see a few options, type test in Resource Name, leave the Resource Path value as given, choose Enable API Gateway CORS, and choose Create Resource.
After creating the resource, we are going to create an API method, Click on the “Action” button and select the Create Method option from the drop down. Next select the Get method from the drop down under your resource. I will select the “Get” method, you can select any one as per your requirement.
Next, select “Integration type” as Lambda Function or if you want to just test it you can select “Mock” as well. Mock feature basically enables API developers to generate API responses from API Gateway directly, without the need for an integration backend. You can use this feature to unblock dependent teams that need to work with an API before the project development is complete.
Then, select the region of the lambda and provide the lambda name and click on the Save button.
Note: I have created a test lambda function here which returns a simple message on success.
Here, our 90% of work is done for our first Rest API.
Now, we will make our API available to be accessible by the clients. So, for that we will again click on the Action button, select Deploy API option , create Deployment stage and click on Deploy.
Once you done with the above steps you will find your API under Stages section where on click the stage name you will see your resource and on click method you will get your first REST API.
Here, we got our first REST API, now we will test it in the browser and see if it works. Copy paste your API in the browser and press enter.
Happy Hacking!
Amazon Api Gateway : https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html