Monday, November 20, 2017

Create your first nodejs lamda function using serverless framework

AWS Lamda :

If you are not aware of lamda function, please visit the following link to know about it.

http://www.fullstacktechnos.com/2017/11/what-is-aws-lamda.html


Serverless Framework :

https://serverless.com/

Serverless framework is the toolkit for deploying and operating serverless architectures. The framework can manage deploying infrastructure and code to AWS, Google Cloud Platofrm, Microsoft Azure and IBM Openwhisk.
If you want to launch your first lamda function to cloud then this framework will definitely help you.

Serverless Setup for Lamda

1) Install nodejs: (Node.js v6.5.0 or later is required)


$node -v
v6.11.3


2) Install serverless framework CLI.


npm install -g serverless

$serverless -v
1.18.1


3) Install AWS-CLI.


$aws --version
aws-cli/1.11.127 Python/3.6.2 Darwin/17.2.0 botocore/1.5.90

If you need any help to install AWS-CLI please refer the following article.
http://www.fullstacktechnos.com/2017/07/how-to-install-aws-cli-in-mac.html


3) Create AWS Admin user and get the keys.

To manage the aws resource serverless needs AWS account credentials which has the required roles and permissions to manage the aws infrastructure. If you don't have a AWS account create a free one.

- Create an user having administrator access (which i think easy to test) and get the access key and secret key for that user.

Note:
Please follow the following article to know how to create aws user and how to add specific roles.

http://www.fullstacktechnos.com/2017/07/how-to-push-local-file-to-s3-using-aws.html

Don't worry about the name of the article. It gives you the information about how to add an aws user,  specific role and how to access the keys.


4) Configure credentials using aws-cli.

Go to the console and type


$ aws configure
AWS Access Key ID [****************]:
AWS Secret Access Key [****************]:
Default region name [None]:
Default output format [None]:


5) Create a nodejs project using npm

Go to the console, create folder called test-lamda, go inside and type "npm init". Follow the instructions.


6) Create a lamda function using serverless

Go to console, Inside your nodejs project folder type



$serverless create --template aws-nodejs

Serverless: Generating boilerplate...
 _______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                           serverless.com, v1.18.1
 -------'

Serverless: Successfully generated boilerplate for template: "aws-nodejs"

Serverless: NOTE: Please update the "service" property in serverless.yml with your service name



Please check the handler.js and serverless.yml file.


handler.js :

It has a single lamda function called "hello" which has 3 input parameters i)event ii) context iii)callback

event contains the req object that has information about path, httpMethod, headers etc.
This basic lamda function when invoked will create a response object and send it back.


serverless.yml:

This file has all the aws infrastructure details that serverless needs to configure when enabled. You can see only service and provider is enabled. You can enable any other features as per requirement



7) Deploy your lamda function to AWS

Go to console, Inside your nodejs project folder type.


$serverless deploy

Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (1.29 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
...............
Serverless: Stack update finished...
Service Information
service: test-lamda-serverless
stage: dev
region: us-east-1
stack: test-lamda-serverless-dev
api keys:
  None
endpoints:
  None
functions:
  hello: test-lamda-serverless-dev-hello



8) Invoke your lamda function


$sls invoke --function hello
{
    "statusCode": 200,
    "body": "{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":{}}"

}

* sls is short form of serverless


9) Check the logs.


$sls logs -f hello -t

START RequestId: 3cc05fc9-cee9-11e8-a9ca-f960d9ae7ppp Version: $LATEST
END RequestId: 3cc05fc9-cee9-11e8-a9ca-f960d9ae7ppp

REPORT RequestId: 3cc05fc9-cee9-11e8-a9ca-f960d9ae7ppp  Duration: 0.83 ms       Billed Duration: 100 ms         Memory Size: 1024 MB    Max Memory Used: 20 MB

* f is short form of function, hello is the function name

Summary:

1) nodejs v6.5.0 or later
2) npm install -g serverless
3) aws configure <Configure aws admin account access key and secret>
4) npm init
5) serverless create --template aws-nodejs
6) serverless deploy
7) sls invoke --function hello


* For more details on serverless framework for aws please visit following links:

https://serverless.com/framework/docs/providers/aws/guide/quick-start/
https://serverless.com/framework/docs/providers/aws/guide/credentials/
https://serverless.com/framework/docs/providers/aws/examples/hello-world/node/


3 comments: