Skip to content

Trigger AWS Lambda with API – Amazon API Gateway

trigger aws lambda with amazon api gateway

In our last post, we saw how we can trigger AWS lambda periodically.  Today we will see how to trigger Lambda with API. We will trigger it using an API created in AWS API Gateway.

What is API ?

API stands for Application Programming Interface.

An application programming interface (API) is an interface that defines interactions between multiple software applications or mixed hardware-software intermediaries. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow, etc.

What is AWS API Gateway ?

Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. API Gateway provides tools for creating and documenting web APIs that route HTTP requests to Lambda functions. You can secure access to your API with authentication and authorization controls.

There are two types of API –

RESTful APIs : Build RESTful APIs optimized for serverless workloads and HTTP backends using HTTP APIs. HTTP APIs are the best choice for building APIs that only require API proxy functionality.

WebSocket APIs : Build real-time two-way communication applications, such as chat apps and streaming dashboards, with WebSocket APIs.

We will use HTTP API to trigger AWS Lambda.

Similar to our last post, we will be sending email using Amazon SES to verify if lambda is getting triggered. We will be using the same code as used in the previous post. You can read more about it here.

Trigger Lambda with API using Amazon API Gateway

In our last post, we saw how we can trigger AWS lambda periodically.  Now we will learn how to trigger it through AWS API Gateway using the same function – lambda_ses (that we used in our last post) which will send an email via Amazon SES.

Whenever we execute our API, we should receive an email or our registered email address.

Architecture

Create API

Add a public endpoint to your Lambda function

  • Open the Functions page on the Lambda console.

aws lambda function

  • Choose a function (say lambda_ses)

aws_func

  • Under Functional overview, choose Add trigger.
  • Select API Gateway.
aws_api_trigger
Lambda with API Trigger
  • For API, choose Create an API.
  • For Security, choose Open.
  • Choose Add.

aws_create_api

Check if the trigger is working

  • Now deploy  your code (lamda_ses).
  • Go to trigger details, click on the API endpoint.

trigger-aws-lambda-with-api-amazon-api-gateway

Result

  • You can see the output message displayed on the screen.

aws-trigger_test

  • Also, a mail has been sent to your recipient`s email address.

 

This how we trigger AWS lambda via API Gateway.

Leave a Reply

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