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 AWS API Gateway.
Before we proceed further, let’s understand what AWS API Gateway is.
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.
- Once you select API Gateway, select your region (where you want to deploy API) from top-right corner dropdown. Following are the regions provided by AWS.
- 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.
- Click "Create API".
- The next screen is the actual playground where API is configured and set with its methods along with configuring hosting environments (Stage, Prod etc.).
Each API includes a set of resources and methods. A resource is a logical entity that an app can access through a resource path.
What the screen indicates refers to current resource and then the following actions can be created.
- Create Method for resource that includes -
- ANY
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
- Create Resource allows us to creat 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 Get and click þ which brings method setup screen as follows.
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 Lambda Function integration type in action.
Once you select Lambda function, you need to configure 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 to select your available Lambda Functions in that region.
Hit "Save" which should show you the following screen.
Click "OK". This action of yours brings the following screen.
You can test the API by clicking Test and should see your Lambda output as following -
Once you are satisfied with the expected output, go ahead and deploy the API by selecting "Deploy API" action under API Actions.
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.
Hit/Copy Invoke URL and you will see your Lambda being invoked.
Similarly, you can have your customized resources with various methods to invoke any AWS Service.
Hope you enjoyed reading about AWS API Gateway to invoke AWS Lambda.
Thanks for reading!