Recent Posts

How to Create Rest API in AWS API Gateway. | infodpsoft

Creation of REST API in an API Gateway



Here is the process which explains to you how to create your Lambda function. But before this let us understand what AWS Lambda is
How to Create Rest API in AWS API Gateway

Let us understand what AWS lambda is:-

Aws lambda is a program that lets you run code without provisioning or managing servers. It runs our code in response to events and automatically manages the underlying compute resources for us. Using Lambda, you can run code for any type of backend service. You can simply run your code in any language which is supported by lambda.

API Architecture:-

How to Create Rest API in AWS API Gateway


1) Log in to your AWS Management console:-

If you are a new user, the first step is to sign up for an Amazon Web Services account or to log in to your existing AWS account if you are an existing user.


2) Open the AWS management console:-

After logging in to the main console page. The new page will be displayed on your screen, which is shown below.

3) Select on the search bar and search for the AWS API Gateway and open it:-

After entering the Console Home click on the API Gateway button and using Python language create REST API.

4) Creation of Rest API:-

As shown in the screenshot below, click the ‘Create API’ button to create a REST API using Python language.

5) There are four ways to create an API in an API Gateway:-

After clicking the Create API button, you will be presented with four options for creating an API Gateway, which is as follows.
  • HTTP API
  • WEBSOCKET API
  • REST API (Public)
  • REST API (Private)
Among these click on the REST API ‘Build’ button as shown below.



6) Create the REST API in an Amazon API Gateway:-

After clicking the 'Build' button, an API page will open in which you must enter the necessary information such as your API name, select your name, type the description, and then click the 'Create API' button.




7) You must first create a Lambda function before you create a REST API:-

For the creation of a Lambda function, use the AWS services to search for a keyword related to Lambda.

When you select the lambda function, you will be directed to the page shown below. And making a function out of the four provided radio buttons. The image below shows how to create a lambda function from scratch.


After selecting the Lambda function from scratch, you must fill in the basic information, such as the function name, as shown in the image below, and then click the Create function button.


8) To successfully triggered API Gateway in a Lambda function:-

The Lambda page will appear after the function has been created, indicating that the API Gateway has been successfully triggered in the Lambda function.


You must register your data after creating the Lambda function. For example, you must insert data into the MongoDB database and register your database using the references shown below.

Code:-

from pymongo import MongoClient 
import json 

def lambda_handler(event,context): 

    print(event['Test']) 

    # Connect with MongoDB Database  
    MongoClient = "mongodb://mongodb0.example.com:27017" 

    # Access database  
    mydatabase = MongoClient['DB_name'] 

    # Access collection of the database  
    mycollection=mydatabase['collection_name'] 

    response = mydatabase.collection_name.insert_one(event) 

    print(response)  

    return { 
            'statusCode': 200, 
            'body': json.dumps('Successfully connected with MongoDB') 
        }

9) Checking Cloud Watch logs from AWS Lambda Function:-

After you've created the Lambda function, go to the monitor and click on 'view logs in Cloud Watch,' where you'll be able to see all the logs.


10) Response for your AWS Lambda Function:-

After reviewing all the logs, you will receive the AWS Lambda function's response.


11) Create your resource for REST API:-

After clicking on drop-down the Action button, The resource A drop-down list will appear to you. And you have to select 'create resources amongst all to create your API gateway resource. down the

12) Successfully create the resource in AWS API Gateway:-

When you create a resource, it will ask you for basic information such as the name of your resource. Then, as shown in the image, click on the Create Resource button at the bottom of the page.

13) Create a Method for your API Gateway:-

After creating the resource, you must choose your method by clicking on the 'Actions' button, where you will be displayed with options such as GET, POST, ANY, DELETE, HEAD, OPTIONS, Patch, and PUT. Choose the method that is most convenient for you. We've chosen the POST method for this.


14) Choose the function you created:-

After you've created the method, a dropdown list will appear, from which you have to select The Lambda function and enter the name of your Lambda function.

15) Deploy your REST API before testing your API:-

However, before testing an API in an API Gateway, you must first deploy your REST API to receive the response. So, to get your response, select your Deployment stage, write your stage name, enter the description, and click the 'Deploy' button as shown below.

16) Save the API configuration by adding a lambda function, then open this API testing page:-

To test the REST API, click the 'Test' button, and you will receive a response based on the code written in the Lambda function.

17) Add request parameter:-

Add the POST method request parameter, or if you choose the GET method, you will get the response immediately by clicking on the 'Test' button, but if you choose the POST method, you must first add the request parameter and then click on the 'Test' button to get the response.


18) Response of API Gateway through REST API POST method:-

Once the request parameter has been added, click the 'Test' button to get the response for any method, whether POST or GET, that you choose.


Other Blog:- 


Written by

Shivani Patel

Rajesh Panchal




No comments:

Powered by Blogger.