AWS

How to Trigger AWS Lambda Using API Gateway

Like some other AWS Services, “API gateway” can also trigger and invoke the AWS Lambda functions. This is done by connecting these two services, i.e., AWS Lambda and API gateway, through some configurations. Triggers from any AWS service are added through the “Add trigger” option present in the AWS Lambda console.

In this post, we will discuss how to trigger the lambda function directly through the lambda console and also by creating a new API in the API gateway service.

How to Trigger AWS Lambda Using API Gateway?

To trigger the lambda function using the API gateway service of AWS, the user needs to configure some settings in both services. Let’s start with the process.

Prerequisite: Create a Lambda Function

Log in to the AWS Management Console and then search for AWS Lambda in the services of AWS. after opening the AWS Lambda console, click on the “Create function” button present on the very first interface:

For this post, we are selecting the method of creation as “Author from Scratch”. Name the function and define a runtime language for the lambda function in which the code will be executed in AWS Lambda:

Select the architecture and then click on the “Create function” button:

Method 1: Creating API Directly Through AWS Lambda

After creating the lambda function, there will be an “Add trigger” option in the details of the created lambda function. Simply click on that:

Search for the “API gateway” in the trigger configuration and select it:

This will instantly display a configuration form. To directly create a new API without visiting the API gateway separately, simply select the “Create a new API” option and then select “HTTP API” as the API type. We will also discuss the other method later in this article.

Click on the “Add” button, and this will add the API to trigger the Lambda function:

There will be an “API Gateway” displayed in the details of the lambda function:

Scroll down to the “Code” section of the lambda function. There will be a default code written in the programming language selected while creating the lambda function. Here, it displays a Python code. Now, to view its execution, we will use the API gateway trigger:

In the “Triggers” section of the lambda function in the AWS Lambda console, there will be an API endpoint in the form of a URL: simply open the link:

This will execute the lambda function and display the output in the web browser:

In the API gateway service, the API created automatically while triggering the lambda function through the API gateway service will be displayed:

Method 2: Creating API in API Gateway

Search for the API Gateway in the AWS Services and open the API gateway console:

Click on the “Create API” button on the very first interface and then click on the “Built” button of the “REST API”:

Now, as it is required to create a REST API, click on the “REST” option and then select “Create new API”. After that, name the API and add a description:

Click on the “Create API” button:

In this way, the API has been created through manual configurations.

Create a Rest Resource

Now, the user is supposed to create a rest resource for the created API. to do that, click on the “Actions” in the API gateway console and then select the “Create Resource” option:

Name the resource and then simply click on the “Create resource button. This will create the resource for the API:

Create Invoke Method

Now, it is required to create an invoke method according to which the lambda function will be triggered. Open the “Actions” dropdown menu again and from there, select the “Create Method” option:

For this post, we select the “GET” method:

Grant Permission to Invoke Lambda Function

Now, connect the created API to the lambda function that is supposed to invoke or trigger. Simply click on the “Lambda Function” as the Integration type and then add the created lambda function into the space given for the “Lambda Function”:

This will display a wizard to confirm giving the API gateway permission to invoke the selected Lambda function. Click “OK”:

Deploy API

It is important to deploy the API to view its outcome. Select the “Deploy API” option from the “Actions” dropdown menu:

Click on “Deploy” after adding a stage name and description:

Click on the created method from the side menu. The method that we created was “GET”, click on it to open it:

There will be an “Invoke URL”. Simply click on the URL link to open it and view invoke results:

This will open the link in the web browser and display the output of the python code present in the lambda function selected while giving the API permission and access to the function:

This was a complete description of how to trigger the AWS Lambda function using the API gateway service of AWS.

Conclusion

The AWS Lambda function can be triggered and invoked using the API gateway service of AWS by first creating a function in the AWS Lambda console and then creating the API gateway, resource, and invoke method, and then granting the created API gateway access to the selected lambda function. The created API can be deployed to view the results when the lambda function is invoked.

About the author

Hadia Atiq

A Software Engineer and Technical Writer passionate about learning and spreading knowledge of the latest technology. I utilize my writing skills to help readers understand the importance and usage of modern technology.