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.
Add a public endpoint to your Lambda function
- Open the Functions page on the Lambda console.
- Choose a function (say lambda_ses)
- Under Functional overview, choose Add trigger.
- Select API Gateway.
- For API, choose Create an API.
- For Security, choose Open.
- Choose Add.
Check if the trigger is working
- Now deploy your code (lamda_ses).
- Go to trigger details, click on the API endpoint.
- You can see the output message displayed on the screen.
- Also, a mail has been sent to your recipient`s email address.
This how we trigger AWS lambda via API Gateway.
Vivek is a Senior Embedded Engineer at Robert Bosch. He has been working on Embedded Systems for the past 10 years. He loves to share his knowledge and train those who are interested. Nerdyelectronics.com was started out of this interest.