Trigger a Lambda Function Using AWS API Gateway

In this tutorial, we are going to see how we can use the AWS API Gateway as a trigger to an AWS Lambda function. This tutorial requires you to be familiar with building and deploying Serverless Applications with AWS Lambda.

Prerequisite: Build and Deploy a Serverless Spring Boot Web Application with AWS Lambda

Firstly, add the necessary maven dependencies into the pom.xml file.


Also, add the maven shade plugin to build a shaded jar for the Spring boot application that we shall build.


Next, create the file which contains the main function, and paste the below code.


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class AWSLambdaDemoApplication {
    public static void main(String[] args) {, args);

Now, the next step is to create a Handler. The request to this handler is routed through an object of the class APIGatewayProxyRequestEvent which is present in the package. Hence, the handleRequest function takes an APIGatewayProxyRequestEvent object as the first parameter.

Create and paste the following code into it.


public class ServiceHandler implements RequestHandler<APIGatewayProxyRequestEvent, String> {

    public String handleRequest(APIGatewayProxyRequestEvent req, Context context) {
        context.getLogger().log("Input: ");
        return "Lambda Function is invoked through the AWS API Gateway....";

The return type of the handler function is String. It returns a String object when invoked.

We are going to verify the same at the end of the tutorial.

The next step is to build the deployment file by running the maven package command. Then, create a Lambda function and upload the jar. If you are not sure about how to do it, refer to the prerequisites section.

Adding API Gateway as the Trigger

After the Lambda function is ready, we need to add a Trigger to it. A Trigger is nothing but the API Gateway. In the Function overview tab, click on ‘Add trigger’.

  • Select API Gateway as the Trigger Configuration.
  • Select ‘Create an API’ for API, ‘HTTP API’ for API Type, and
  • ‘Open’ for Security.

In the Additional setting tab, you can give a custom name to the Gateway API. You can also set up cross-origin resource access and enable detailed metrics. Click on Add.

Once, this is completed, click on API Gateway in the Function overview to see the list of Triggers. You can also find it in the Configuration tab -> Triggers.

In the Trigger description, you can find the HTTP API endpoint. Click on it. A new tab opens, and displays the message ‘Lambda Function is invoked through the AWS API Gateway….’

You have now successfully triggered the Lambda function via an AWS API Gateway.

If you wish to make sure that the API Gateway has been created successfully, click on ‘Network & Content Delivery’ under ‘all services.’ Click on API Gateway. You can see the Trigger that you have just created. Click on it to see the details. You can edit it if you want.

Link to Code : GitHub

That’s it for this tutorial. Hope this has helped you. Stay tuned for more tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *