DEV Community

Cover image for Create your first API with AWS lambda and Node
ckmonish2000
ckmonish2000

Posted on

Create your first API with AWS lambda and Node

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

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

HTTP API

  • 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

  • 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

  • Now Review your changes and create the API

Create API

Creating an API route

Image description
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

  • Click on create

Image description

  • 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

  • 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

Creating an Lamda function from the console

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

Image description

  • Select author from scratch

Image description

  • Give your function a name while keeping the default config and hit create function.
    Image description

  • 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

  • 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

  • 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

  • Then click on create and attach integration

link

  • From the drop down choose Lambda function as integration type

select function

  • 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

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

lambda trigger

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

Api Call

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

Top comments (0)