Serverless: APIs with Node.js and AWS Lambda

Yesterday, i'm playing around with Node.js API running on AWS Lambda.

By using AWS Lambda: 
1. We only charged when our application accessed by user, so it'll minimize the server cost. 
2. We don't need to do server maintenance, since all will be handled by AWS.

There are a lot of framework to help us develop serverless application, but one of the most popular one was Serverless (https://www.serverless.com). 

Using Serverless, we can do alot of thing faster, since serverless do a lot of things for us: one command deployment / application templates / or event local testing.

Here's my documentation when toying with Serverless & AWS Lambda.

0. Prerequisite

AWS Account, you'll need an AWS Account to create your Lambda function right?

1. Installation

Of course you will need to install Node.js before run the command bellow :)

npm install -g serverless

2. Generate Serverless Project

Run the command below
sls
serverless

You will be asked some questions :

Choose AWS - Node.js - Starter 

Fill Project Name

You will also asked to login into your AWS account to generate access key required to generate all of things required to make sure you code is running on AWS Lambda.


3. Run & Configure 

Go to you Project Name. You will see the following structures: 



Run serverless invoke --function hello to invoice a specific function

{
    "statusCode": 200,
    "body": "{\n  \"message\": \"Go Serverless v3.0! Your function executed successfully!\",\n  \"input\": {}\n}"
}

If you wanna see our basic app is running in cloud with url that we can access directly, update serverless.yml, by adding url : true

Run serverless deploy

Deploying aws-node-project to stage dev (us-east-1)

✔ Service deployed to stack aws-node-project-dev (72s)

endpoint: https://random-url-blabla.lambda-url.us-east-1.on.aws/
functions:
  hello: aws-node-project-dev-hello (1.4 kB)

We can do changes in handler.js, after do changes, we will need to run serverless deploy again.

That's all!

Comments