Tạo Node JS Serverless Với AWS Lambda Trong 10mins

Why should you use a server-less API? Find out here. Already convinced? Follow these basic steps:

Step 1

Create an AWS account. After you have created an AWS account from https://console.aws.amazon.com/ and have signed in; you will be taken to this screen:

AWS console home page

Step 2

Create a new Lambda function by searching for “Lambda” in the Find Services section. You’ll be taken to this screen:

Functions Page

From the above screen, click on the “Create Function” orange button. On this page, give your function a name of something you like and pick the latest Node.js version that AWS Lambda provides support for when you’re reading this article. In my case it is version 12:

Create your function

The next part is pretty obvious: click on the “Create Function”.

Step 3

Write your code! After clicking on the button, you will be taken to your function’s details. If you scroll down further you will see the default code of the function. In my case it looks something like this:

Default Lambda Code

You can edit the code to anything you like. I went with this:

Customized code

Remember to click the “Save” button after making your changes.

Step 4

Our Lambda function is now complete but we need a trigger for it. A “trigger” is essentially a condition that calls our function. Since one of the most common interfaces these days are HTTP API’s, we will create such an API. To do that; scroll back up to the top of the page and click on “Add trigger” like here:

Add trigger button

From the options on this screen, pick “API Gateway” for the type of API. And for the next input, select “Create a new API” and keep the default HTTP API template. Like this:

Trigger Configuration

Now scroll down and click on the “Add” confirmation button. Once that is done; you will now have a URL to your ‘Server-less’ API endpoint. It can be seen here hovered on by my mouse:

API URL

Consuming your API

Simply copy the API endpoint and paste it on a browser and you will see the response of your API:

Your API’s Response

Who knew that it could be so quick? What’s even more awesome about this AWS infrastructure is that you can change the code of your Lambda function, hit “Save” and your endpoint is updated. No need to go through a pipeline to deploy changes in your API. Hope you enjoyed this short tutorial!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.