Create your first API with AWS lambda and Node
Tags: serverless, javascript, aws, beginners
Published: 2022-11-12 09:23:06

In the previous post we saw how to create a serverless lambda function with netlify now let create a function directly on AWS.

we are going to use 2 AWS services namely lambda serverless function service from AWS and API gateway a service which it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale.

## Setup AWS API Gateway

- Login to your AWS console and search for API in the search bar and select API gateway

![API gateway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilq0c71gr8ieu79mt29o.png)

- You have various options like HTTP, Rest and sockets but for the purpose of this post select HTTP API.

![HTTP API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qowk3qnr010uju9qxrp.png)

- You will be redirected to a page to create your API, so give a name to your API endpoint and click next also click next on the route's registration page as we will configure the later.

![Name your api](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qizy9flndox090fci6lb.png)

- Now name your stage and make sure the auto-deploy switch is active and hit next [FYI: a stage is just a logical reference to a lifecycle state of your API (for example, dev, prod, beta, or v2).]

![Stage Name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8ixe20dnbgtnrg1n0h5.png)

- Now Review your changes and create the API

![Create API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ncp33p3dyjca8dezrrz9.png)

## Creating an API route
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/feaq5eqa6ct6ea5m3w7l.png)
Now we have an API endpoint setup in gateway now we have to configure routes like get student by id.

- Click on routes in the sidebar

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/daxxxjcszrpg8p5z4xzh.png)

- Click on create

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9n98cgk28n0ojqud88gl.png)

- Now choose the Request type from the dropdown and add the route name and hit create [NOTE: the /{studentID} in the route indicates a route parameter which we will extract in the function]

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fe7h154rc1outdixmhm6.png)

- Now you have created an api endpoint the next step is to create a lambda function and link it to the route we just created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b556hvjmepvb8kul7gtg.png)



## Creating an Lamda function from the console

- Now search for lambda in the search bar select it and click on create function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93j50be0vr0tg9k3gb7l.png)

- Select author from scratch 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5knmuvo4hwjkea0vl4bp.png)

- Give your function a name while keeping the default config and hit create function.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2z46ig7fd98gk78swfy.png)

- Now you should see a view like this where you can setup various triggers to run your function and we are going to use API gateway's HTTP endpoint to trigger the function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdfni1is1nqv7etpjd6p.png)

- And if you come down you should be able to view the function code and edit it.

- Here we are extracting the studentID from the route and returning the student if their ID is present in the array.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tza37chahv2i8vjqh3mj.png)

- Hit Deploy and your function must be live.

## Linking the endpoint to your lambda function

- Go Back to the routes section of the API gateway and click on attach integration

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2tb9t5dp8eci57sn5iv.png)

- Then click on create and attach integration 

![link](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ao3ret3l3d7vcji089z.png)

- From the drop down choose Lambda function as integration type

![select function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuox0cs87xzh3s6p6s9q.png)

- Then in the Integration details section select the lambda function we just created and make sure the invoke permission switch is active and then hit the create button.

![details aws ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7ztcic06mf5pd9ak8sc.png)

- Now if you come down to configurations under lambda function you should see the endpoint to trigger the function.

![lambda trigger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8i87yc27zp7uegpayiz4.png)

- Now if you call the URL from the browser or postman you'll see the response.

![Api Call](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3q9kibohcm0uvf84y5hu.png)


Thankyou, 

for following this post in the next post I'll try to cover using Dynamo DB and how you can setup your serverless infrastructure using code with a framework called serverless