I just updated PasteThePlan, and the change you’ll notice first is that we’re using the latest version of Justin Pealing’s html-query-plan. It adds missing index hints, yellow bangs for warnings like implicit conversions and no join predicate, and more. Check out this example plan:
Plus variable line widths for different amounts of data:
One of the behind-the-scenes updates that you can’t see is that we’re now using the Serverless Framework. If you haven’t heard of the Serverless Framework that’s okay, it’s still fairly new. I’ve been using the Serverless Framework over the past year or so on other projects, and I figured it’s about time to start using it with Paste The Plan. The Serverless Framework is a huge help in deploying serverless applications to the cloud.
Before Serverless, in order to deploy a Paste The Plan function to Amazon Web Services, I had to:
- Run a gulp function that collected the necessary files and place it in a deployment folder.
- Zip the deployment folder.
- Go to the function in the Lambda control panel, upload the zip file, and save.
That doesn’t sound like much work, but I had to create and maintain those gulp functions that moved all of the files related to the function. Let’s just say it’s a bit messy. But the biggest problem is that these steps take time, and when you’re testing these functions in the cloud you want to move fast.
This is where the Serverless Framework comes in. The Serverless Framework packages and deploys serverless functions to the cloud. One of the best things about the Serverless Framework is that It’s not tied to any cloud provider. It works with cloud providers AWS, Azure, and Google Cloud. It even works with more obscure cloud providers like OpenWhisk and Kubeless. With the Serverless framework deployment is just one command.
In order to put Paste The Plan in serverless deployment heaven I had to accomplish the following steps:
- Install Serverless Framework via NPM
- Configure Serverless yaml file
- Configure environment yaml file
- Ensure package.json has the correct packages
Install Serverless Framework via NPM
This was the easiest step since I already had it installed. But if you’re really interested in how to install the Serverless Framewok then with NPM (Node Package Manager) installed use the following command:
npm install serverless -g
And that’s it. It’s installed. Pretty easy.
Configure Serverless yaml file
The serverless.yml file is the magic of the Serverless Framework. This file is where you tell the Serverless Framework about your function(s) and your cloud environment. From here you can define what files are packaged, the settings of your function, and the role the function is associated with. But my favorite thing that you can do in the serverless.yml file is create other cloud services. You can create S3 buckets, roles, policies, Virtual Private Clouds, API Gateways, queues, and pretty much cloud service you can think of. This is extremely powerful. This means that you can configure entire serverless applications within one file and deploy them in one command. This also means that, if configured correctly, you can deploy stage environments via a single command. See the Serverless Framework documentation on how to configure the serverless.yml file for your cloud provider.
Configure environment yaml file
I had been using the dotenv npm package to use environment variables. I had to do this because Paste The Plan was created before the Lambda service had environment variables available. The environment yaml file can be named what ever you like. I prefer to use the functionname-env.yml format. You can then specify the environment yaml filename in the Serverless yaml file. In the case of Paste The Plan, it was pretty easy to reformat the dotenv environment file into the environment yaml format.
One of the things that I had to do was merge configuration files. For dotenv we had two configuration files for each function, one for production and one for development. For the Serverless Framework the environment file needed to be one file. So in the Serverless environment file we defined two sections, one for prod and one for dev, and placed the appropriate settings under each.
Ensure package.json has the correct packages
Each function in Paste The Plan had it’s own package.json. This file is created by NPM and has a list of the packages that the function uses. As it turns out there were a few unnecessary packages in package.json.
Now this didn’t affect the code, but it does affect the performance. The general rule with serverless is the larger your function is in size the slower it will take to start up. Since we have no control over when a function is started or stopped it’s best to keep the size of the function as small as possible. When going through the package.json files I noticed a few were referencing the aws-sdk. We don’t really need this package in the function because all Lambda instances already have the aws-sdk installed. I also removed references to dotenv since we wouldn’t be using it anymore.
Deployment through the Serverless Framework
Getting all of this setup really wasn’t a big deal BUT getting it deployed was tougher. Because the functions already existed in Lambda, I had to delete the existing function. While the deployment through the Serverless Framework worked, access to the function through API Gateway broke. It turns out that the new function was missing a permission that the old function had. Once the permission was added everything worked.
The goal of this exercise was to ease the pain of deployment to the cloud. The Serverless Framework has a bunch of features that I haven’t discussed. You can stream logs, invoke functions, and even run functions locally.
At some point I would like to put all of the resources in the serverless.yml file including the API Gateway configuration, virtual private network, S3 buckets, and security roles. Overall the Serverless Framework is pretty awesome, and if you’re using serverless architecture, you should be using this framework.
Brent says: as a small business owner who wants to give back to the community, I love serverless architecture because it keeps my hosting costs low. Here are the costs for PasteThePlan over the last few months – this is the kind of community initiative I can totally sustain:
DynamoDB is the NoSQL database we’re using, and S3 is where we store the plan files. The prices on that part don’t even round up to a penny! Gotta love the cloud.