Create your first API with AWS lambda and Node
ckmonish2000
Posted on November 12, 2022
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
- You have various options like HTTP, Rest and sockets but for the purpose of this post select 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.
- 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).]
- Now Review your changes and create the API
Creating an API route
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
- Click on create
- 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]
- 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.
Creating an Lamda function from the console
- Now search for lambda in the search bar select it and click on create function.
- Select author from scratch
Give your function a name while keeping the default config and hit create function.
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.
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.
- 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
- Then click on create and attach integration
- From the drop down choose Lambda function as integration type
- 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.
- Now if you come down to configurations under lambda function you should see the endpoint to trigger the function.
- Now if you call the URL from the browser or postman you'll see the response.
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
Posted on November 12, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.