Serverless Framework: A tool to boostrap your AWS Lambda application
Java has Spring. Ruby has Rails. NodeJS has… well, NodeJS. What do we have for Serverless applications?
“The Serverless Framework allows you to deploy auto-scaling, pay-per-execution, event-driven functions to any cloud.”
The first thing you will find when you start looking into Serverless with AWS Lamda is how slow and manual the process to create a Serverless application is: Create your lambda functions, ZIP the code, upload it to S3. Go to the AWS web console and start creating your Lambdas, API Gateways and all the other resources in a process I get tired just to trying to remember.
If you’re versed on Bash, Python or a similar scripting language, maybe you have the time and patience to go through the documentation and do all that with AWS Cli.
But me, being the pampered Java developer (and novice NodeJS developer) I am, I’m trying to not reinvent the wheel and want to use something that someone else with the resources is maintaining.
Enter the Serverless Framework.
The comparative in the first line to other frameworks like Spring or Rails is unfair. Serverless Framework (just Serverless, from now on for the sake of simplicity) does not do all the shiny things those other tools do. But still, Serverless does it’s job and does it well.
Serverless will generate the basic boilerplate code you need to start coding Lambda functions.
Here is a great tutorial on using Serverless.
I won’t go into duplicating step by step the tutorial; but let’s take a look at the files that get generated for a NodeJS project, specifically to serverless.yml.
The commented code is as interesting as the uncommented parts: Not only you can create the API Gateway paths, you can also create any service in AWS in one single command: S3 buckets, Alexa skills, DynamoDB instances, etc.
Internally, Serverless generates CloudFormation scripts which will take care of all the heavy lifting for us.
Also, the choice of NodeJS is on purpose. NodeJS is a really good choice for this project, as it’s lightweight and simple. And there is a chance that you, just like me, have good experience in JavaScript from previous web projects.
This is not as much a framework for AWS Lambda as it is a tool to generate definition files for your whole AWS infrastructure! Put this project in a source control repository and you can gradually add all the services your application will need, becoming a single source of truth for your application.
Now, we mentioned CloudFormation, the service provided by AWS to define all the services in a AWS-based application; why not use it directly? You can. However, Serverless also takes care of packaging our code, and deploy it in the correct place. It’s a unified way to deploy our server-side code.
Integrate your Serverless project with a free CI tool like Travis CI and in just a couple of minutes you will have a full deployment pipeline ready to update your environment on every commit.
This is a good start, but you still have to do some changes to your project to keep your code decoupled from a Lambda specific architecture so, if in the future you want to migrate into something else, you can reuse you application logic without a lot of refactor.
In next articles, we will start building a better project structure to reduce to a minimum the amount of dependency our code has on AWS specific APIs.