Design a site like this with WordPress.com
Get started

[13] [ProjectX] – Serverless framework – Best Deployment option

Source code: https://git.io/fjz13

Until now, we have learnt the following methods of deploying serverless applications to AWS cloud

  1. Using AWS Console
  2. Using AWS CLI
  3. Using AWS CloudFormation
  4. Using Claudia.js framework
  5. Using AWS Serverless Application Model (SAM)

Now let’s look at the last and best method for deployment. This uses a framework called Serverless framework (https://serverless.com/).

Currently this is also the most popular method of deploying serverless applications and it supports deployment to all major clouds.

1. Setup and Installation

Create a folder named “booksAPI” and inside it, create a sub-folder called “src”. We will put our Lambda function code into this “src” folder

Then open terminal at “booksAPI” folder and initialize the project using following command

npm init

Enter the following information and leave the rest as default by pressing ENTER button

Name: booksAPI

Description: REST API for books catalog

You should be able to see a package.json file now as shown below

Now let’s install Serverless framework with following command

npm install -g serverless

and finally install Express framework

npm install –save express

Then copy serverless.yml in root folder (booksAPI) and other JavaScript files in “src” folder from GitHub

The 5 JavaScript files above correspond to CRUD operations we will do on books catalog using API’s

JavaScript file Action HTTP Operation Path
Create.js Add book to catalog POST /books
Delete.js Delete a book DELETE /books/{id}
Get.js Get a book GET /books/{id}
List.js Get all books GET /books
Update.js Update a book PUT /books/{id}

This completes writing code for our API’s

2. Defining Deployment Script

Now let’s focus on creating a script that has details of what is to be deployed and how. This is done by creating a file called serverless.yml, which is written in YAML format.

But before that, lets reiterate what resources do we wish to deploy for our API’s to work

  1. API Gateway with 5 endpoints for each operation
  2. Five lambda functions servicing each API endpoint
  3. A Roles with appropriate permissions for lambda function to access other resources in AWS
  4. One DynamoDB database where books will be stored

Hence our deployment script file should have instructions to create above resources.

We already have this file present in GitHub, which you have copied. So, let’s open it. The structure of the file will be as shown below

Let’s understand it section by section

The first line of this file starts with name of the service.

Service in terms of serverless framework is grouping all common operations common on a particular entity. So we can group all operations related to our books catalog as one service and name it as booksAPI

Then we have to describe provider, which means cloud vendor on which deployment will take place. We specify that our code is written for nodejs8.19. We assume we are right now in Development stage of this project. So, we set stage=dev.

Lambda functions interact with DynamoDB database. However, we cannot hardcode the table name, as it is generated dynamically during deployment according to how we define it in this serverless.yml file.  Hence, we have to define an environment variable in lambda functions and pass DynamoDB table name as its value.

So DYNAMODB_TABLE is variable name and its value is generated by stitching variables together. ${self:service} will resolve to  value booksAPI and ${opt:stage} will take value dev

Hence our generated table name will be booksAPI-dev

We also need to generate a role with correct permission to be used by lambda function. The above snippet is for the same.

Note: We are defining Resource as our DynamoDB table here. So, our Lambda functions will be restricted to perform operations only on this one table

Now its time to create Lambda functions

The function section contains definitions for the same.

We name our function create, specify where the starting code is (handler) and give inputs to create an Http based API with path as /books. Method defined is POST and we set CORS option to True so that this API can be called from another domain.

In definition of handler (src/create.create), src is folder, first create is name of file (create.js) and second create is the method name exported in this file (module.exports.create). This is how API is able to execute lambda function code correctly.

Similarly, we define other functions also

This completes defining Lambda functions. Now last thing remaining is creation of DynamoDB table

Here we define creation of a table with Primary key id, which is of type string. Also we set Read and Write capacity values according to which AWS can define processing limits and we are going to be charged as per this execution capacity.

Lastly, we specify name of table.

This completes defining serverless.yml file.

3. Deployment

Open terminal window in root folder (booksAPI) and execute the following command

serverless deploy

Here are the things that happened above

  1. The framework packaged (zipped) our code in src folder to be deployed.
  2. It created a CloudFormation template from our serverless.yml file
  3. It created an S3 bucket and uploaded these artifacts to S3 bucket
  4. It validated template before starting deployment
  5. It started a CloudFormation Starck from template to deploy
  6. It updated stack with more resources to be created
  7. Stack creation completed.

At this point, our deployment is done.

Below, serverless framework gives us details of the deployment

Now we can use the API URLs for each operation.

Also, if we click Show Hidden Files option in Cloud9 IDE, we will find a folder .serverless

Inside it, we find following files

If you were to open CloudFormation-template-update-stack.json, you will find a file with 999 lines.

If you convert it into YAML format, it comes to around 631 lines.

This is the power of serverless framework. It generated a 600+ lines of code for us from our 76 lines serverless.yml file

Also, if you have written CloudFormation templates, you know how many times you make mistakes because it was not formatted correctly.

Once you get used to using serverless framework, you will never go back to writing CloudFormation templates manually.

Also, if you want to get more details while deploying, you can add verbose to the deploy command

serverless deploy -verbose

you can also use shortcut command as follows

sls deploy -v

4. Verification

Let’s verify if all resources were created properly

As a bonus, serverless framework also created separate LogGroups in CloudWatch for each Lambda function. This is where AWS will record errors encountered while executing each Lambda function

5. Testing

Let’s conduct testing to verify whether this API works as expected.

Test-1: Add a book

URL: https://49guj2h9ne.execute-api.us-east-1.amazonaws.com/dev/books

Method: POST

Body/Payload:

{ “name”: “A Game of Thrones”, “author”: “George R. R. Martin”, “price”: “19.99” }

Result:

Add one more book

{ “name”: “Truth and Lies”, “author”: “Caroline Mitchel”, “price”: “10.95” }

Test-2: Get all books

URL: https://49guj2h9ne.execute-api.us-east-1.amazonaws.com/dev/books

Method: GET

Body/Payload: None

Result:

Test-3: Get one books

URL: https://49guj2h9ne.execute-api.us-east-1.amazonaws.com/dev/books/dbd89440-8a33-11e9-bdf0-55fb70e613f6

Method: GET

Body/Payload: None

Result:

Test-4: Update a book

Since Game of thrones is very popular, lets increase its price from 19.99 to 29.99

URL: https://49guj2h9ne.execute-api.us-east-1.amazonaws.com/dev/books/dbd89440-8a33-11e9-bdf0-55fb70e613f6

Method: PUT

Body/Payload:

{ “name”: “A Game of Thrones”, “author”: “George R. R. Martin”, “price”: “29.99” }

Result:

Test-5: Delete a book

URL: https://49guj2h9ne.execute-api.us-east-1.amazonaws.com/dev/books/dbd89440-8a33-11e9-bdf0-55fb70e613f6

Method: DELETE

Body/Payload: None

Result:

Now if you again retrieve all books, it will only return one book

6. Clean up

Execute below command and all resources will be deleted

serverless remove or for shortcut, you can also use

sls remove

Conclusion:

Hope you got a sense of how useful and productive this framework is for deployment of serverless projects and applications. Kudos to the team and all open source contributors who have made this wonderful tool for us.  We have even started hearing of other types of projects (IaaS, PaaS) being deployed using this framework.

From now on, serverless framework will be our primary tool for development/deployment of the project.

Happy Clouding!

Advertisement

2 thoughts on “[13] [ProjectX] – Serverless framework – Best Deployment option

  1. […] our last blog post, we saw how Serverless framework (SLS) is the best way to create/deploy serverless projects. In […]

    Liked by 1 person

  2. […] we had created microservices using REST APIs. In this blog, we will look at creating the same microservices using […]

    Liked by 1 person

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

%d bloggers like this: