Invoking AWS Lambda Function Using AWS API Gateway


In the previous article, we learned how AWS Lambda function can be invoked using CLI, and the data is passed using an event. In this article, we will learn how the Lambda function can be invoked through the AWS API Gateway.
Before we proceed further, let’s understand what AWS API Gateway is.
AWS API Gateway

API Gateway falls under the Application Services category.
API Gateway
Amazon API Gateway routes the request to appropriate microservices. It acts as a front door to connect applications for accessing data, business logic, connecting to storage services, like S3, web application, or code running on AWS Lambda.
In this section, we will try to learn how to create AWS API and how it can be connected to AWS Lambda. So, let’s get started.
  1. Once you select API Gateway, select your region (where you want to deploy API) from the top-right corner dropdown. The following are the regions provided by AWS.

    AWS API Gateway 
  1. Once you hit "Get Started", you should notice the following screen. The API can be created in three ways - New / Import from swagger / Example API.

    AWS API Gateway
  1. Click "Create API".
  2. The next screen is the actual playground where API is configured and set with its methods along with configuring hosting environments (Stage, Prod etc.).

    AWS API Gateway 
Each API includes a set of resources and methods. A resource is a logical entity that an app can access through a resource path.
AWS API Gateway 
What the screen indicates refers to current resource and then the following actions can be created.
  • Create a Method for a resource that includes -

    • ANY
    • DELETE
    • GET
    • HEAD
    • PATCH
    • POST
    • PUT

  • Create Resource allows us to create child resources like /user, /posts, etc.
In this article, we are considering root resource and will create methods by clicking on "Create Method". Then, we will choose to Get and click þ which brings the method setup screen as follows.
AWS API Gateway 
The integration type allows you to configure your endpoint with Lambda, HTTP (any existing endpoint), Mock, or any other AWS Service.
We will consider the first option (Lambda function) for this article. Let's see the Lambda Function integration type in action.
AWS API Gateway

Once you select the Lambda function, you need to configure the Lambda Region. This is one of the amazing features of AWS API Gateway which allows you to connect to any Lambda function in any region.
So, once the region is selected, the next autocomplete allows you to select your available Lambda Functions in that region.
Hit "Save" which should show you the following screen.
AWS API Gateway

Click "OK". This action of yours brings the following screen.
AWS API Gateway

You can test the API by clicking Test and should see your Lambda output as following -
AWS API Gateway

Once you are satisfied with the expected output, go ahead and deploy the API by selecting the "Deploy API" action under API Actions.
AWS API Gateway

The deployment also includes configuring Deployment Stage such as where the user wants to deploy at the current moment; e.g., Stage / QA / Dev / Prod, etc. If no existing deployment stage is available, you can always create a new one. 
Once the deployment stage is selected, you are ready to launch your first API Gateway.
AWS API Gateway

Hit/Copy Invoke URL and you will see your Lambda being invoked.
AWS API Gateway
Similarly, you can have your customized resources with various methods to invoke any AWS Service. 
I hope you enjoyed reading about AWS API Gateway to invoke AWS Lambda. 
Thanks for reading!