Serverless Framework— Create Nodejs AWS Lambda

Serverless Framework helps to build and operate serverless applications easier and faster. I mostly work with AWS resources now and if I were to use different cloud providers in the future, I can still use this framework. I am planning to start a series of documenting AWS Serverless Applications using Serverless Framework.

AWS Lambda is the most widely used serverless feature widely used. Let us start by building a simple lambda function using this framework.

Let us use the Nodejs template of serverless framework to create the new project. The high-level architecture would look like as below,

AWS Lambda (Generated from CloudFormation Designer)

Create project using node js template

serverless create — template aws-nodejs — path serverless-demo

Assuming AWS_PROFILE is set in the path in the terminal, let us go ahead and deploy the project created without making any changes.

serverless deploy

It will package and deploy the resources in the AWS Account as shown in the logs below.

Serverless Logs

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 serverless-demo.zip file to S3 (390 B)…
Serverless: Validating template…
Serverless: Updating Stack…
Serverless: Checking Stack update progress…
……………
Serverless: Stack update finished…
Service Information
service: serverless-demo
stage: dev
region: us-east-1
stack: serverless-demo-dev
resources: 6
api keys:
None
endpoints:
None
functions:
hello: serverless-demo-dev-hello
layers:
None

The summary shows that, it has created,
1. default environment as dev (stage) variable.
2. default region : us-east-1
3. Cloudformation stack is named serverless-demo-dev (service name + stage name defined in serverless.yml)
4. 6 AWS resources. We will look into the CloudFormation stack to see what were all the AWS resources created.
5. Created a lambda function named serverless-demo-dev-hello. ( service name defined in serverless.yml + function name defined in serverless.yml + stage in serverless.yml ). This is the default name generated for the lambda function but can be overridden.

While looking into the CloudFormation stack, following are the AWS resources created.

  • AWS::Lambda::Function — serverless-demo-dev-hello
  • AWS::Lambda::Version — arn:aws:lambda:us-east-1:AccountID:function:serverless-demo-dev-hello:1
  • AWS::Logs::LogGroup — /aws/lambda/serverless-demo-dev-hello
  • AWS::IAM::Role — IamRoleLambdaExecution
  • AWS::S3::Bucket — S3 Bucket for our package
  • AWS::S3::BucketPolicy — S3 Bucket Policy

Any serverless projects creates an S3 Bucket and a Bucket Policy attached to the bucket that has the cloudformation template and the package deployed containing the code for the lambda functions.

In this serverless project, a lambda function and an IAM role attached to the lambda function to CreateLogStream, CreateLogGroup and PutLogEvents’, a log group where the lambda’s log events will be traced were created.

Backend Engineer, Cloud Practitioner