AWS Lambda Function With Amazon API Gateway as the Trigger
According to AWS, Lambda is a serverless computing service which functions according to the following steps —
- Uploading the code to AWS lambda.
- Setting up the triggers.
- And, Running the code.
In this blog, we’ll write an AWS lambda function which will be triggered by Amazon API Gateway.
Amazon API Gateway
According to API gateway documentation, AWS API gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor and secure APIs. API acts as a front door for the application to access data, business logic or functionality from the backend services. It handles all the task involved in accepting and processing up of hundreds or thousands of concurrent API calls, including traffic management, authorization, access control, monitoring and API management.
API Gateway creates RESTful APIs that:
- Are HTTP-based.
- Enable stateless client-server communication.
- Implement standard HTTP methods such as GET, POST, PUT, PATCH, and DELETE.
How Amazon API Gateway works?
The services like Web applications, mobile applications, IoT devices etc. that can call to an HTTP or Websocket endpoint sends that request. these requests are intercepted by API gateway which will take care of managing the traffic of the request. It will send the request to the right service like AWS Lambda, Dynamo DB, EC2 etc.
Some features of Amazon API Gateway
- It can be used in mobile or web back ends.
- It also supports Websockets which makes it possible to build a server-less real-time chat with session management using Lambda, APi gateway and Dynamo DB.
- It can also connect to Dynamo DB directly and perform operations on table without the need to have a Lambda or any server in between.
- It also can be connected to Cognito to validate all incoming requests to make sure the users who are sending them are authorized to access the resource.
API Gateway supports three types of APIs that invoke Lambda functions:
- HTTP API — A lightweight, low-latency RESTful API.
- REST API — A customizable, feature-rich RESTful API.
- WebSocket API — A web API that maintains persistent connections with clients for full-duplex communication.
To understand the basics of AWS Lambda, please visit —
Execute a Basic AWS Lambda Function Without Any Triggers
AWS refers Lambda as a server-less computing service. Let’s understand what is server-less. It has the following three…
Now that we have understood the basics of AWS Lambda and Amazon API Gateway, lets dive in to write the lambda function. As the AWS lambda supports many languages, we’ll use Node.js here.
Step 1: Click on the create function button and then Author from scratch. Fill in all the blanks under the Basic information. Lets give our Lambda function a name of Function_One. Select Runtime as Node.js 12.x. Then under Permissions select Create a new role with basic Lambda permissions as an Execution role. Then hit Create function button.
Step 2: A new page will be opened where we’ll write our lambda function. Then, click on the Add trigger button and select API Gateway and then click on Create an API. Select API type as HTTP API and security as open. By clicking Add button, the API gateway will be added as the trigger.
Now, Scroll down to the text editor. Inside index.js, write the following code and deploy it —
Step 3: Now, in the same page, under Designer section, click on API Gateway button, it will give a link which will be our API endpoint. Open this link in new tab, it will display
Now, at the end of the link append
?name=John , then hit enter. Now the page will display
In this article, we have understood the functioning of AWS Lambda, Amazon API gateway, working of API Gateway, features of API Gateway, and API Types. Finally, we’ve created an AWS lambda function with Amazon API Gateway as the trigger.
Documentation on AWS Lambda