Create your first API on AWS Lambda and API Gateway using NodeJs

A simple step-by-step process to create your first lambda API.

Shiva kumar Mangina
Towards AWS

--

AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. Therefore you don’t need to worry about which AWS resources to launch, or how well you manage them. Instead, you need to put the code on Lambda, and it runs.

In AWS Lambda the code is executed based on the response of events in AWS services such as add/delete files in S3 bucket, HTTP request from Amazon API Gateway, etc.

Let's create our first API using Lambda and API Gateway.

  1. Search for Lamba in AWS and open it.

2.Click on Create Function.

3.Name the function. And select Nodejs for runtime.

4. Write the following code in the code tab and click on deploy.

exports.handler = async (event) => {
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};

5.Search API Gateway and open it.

6.Select Rest API and click on Build.

7.Name your API and click on create API.

8.In the resources tab click on create method. and select GET method.

9.Select Integration type as Lambda function. select the region and name of the lambda function.

10.And click on deploy(Very important**) from actions dropdown.

11. Select a stage. By default it is default.

12.When you click on deploy. You will get the API end point to call.

13. Go call your first API.🎉

BONUS: POST Request

  1. Write the following code in the lambda and deploy.
exports.handler = async (event) => {

const body = JSON.parse(event.body)

const response = {
statusCode: 200,
body: JSON.stringify('Hello '+ body.name),
};
return response;
}

2. In API Gateway create a POST method, click save and Deploy API.

3. Go call your first POST API.

body:

{"name":"shiva"}

Thanks!! Happy Coding!!

--

--