3 Tips on Serverless to Step Up Your Game
By: Nicolás Kipreos
Tags: Python, AWS Lambda, Serverless, Beetrack

Serverless is a framework that takes care of all the miserable job of deploying an AWS Lambda function, its triggers, logs, etc. It takes care from zipping all the files and dependencies, uploading it to S3 (versioning), creating a CloudFormation template that builds API Gateway triggers, Cloudwatch events, you name it! Pretty cool, huh?

Unfortunately, chances are that you will need more than the standard libraries included in Python, or maybe you would like to take care of multi-environment deployments, or that you would like to deploy a function that has a custom domain endpoint instead of the horrendous AWS API Gateway default URL.

Below, you can see 3 serverless plugins that are a must when we develop a serverless app here in Beetrack.

Installing Python libraries can be a pain in the ass sometimes if you are running your code on AWS Lambda. This is where Python Requirements, comes in.

Requirements manages the downloading of all the necessary dependencies, adding them to the project, and managing all that uncomfortable stuff that comes into play when adding extra libraries to your AWS Lambda Function.

Take into consideration that this is not a bullet proof extension. There are some packages that include some system dependencies in consideration, so, for example, when using packages that depend on XML managing, like zeep, you’ll probably have some trouble with some binaries that are included for that, if you make the deployment in a Mac OS. In that case, you will need to have an Amazon Linux instance specifically for making the compilation and deployment of your code.

The way in which this plugin works is very simple, you just have to create a requirements.txt file on your directory and the plugin will justo know what to install based on the content of the file. Here we can see an example:

2. Domain Manager

When using an AWS API Gateway to trigger your function, it is likely that you won’t find the automatically generated URL that appealing. Enter, Domain Manager, this extension is quite useful in case you would like to expose and share something like api.mynicedomain.com, instead of https://8sdf9ejsifs.execute-api.us-east-2.amazonaws.com.

Domain manager takes care of building the domain and enabling SSL configuration, but be careful of creating the certificate in ACM region N. Virginia, because it doesn’t work in other regions, I don’t know why but I am guessing that it was because N. Virginia was the first region to implement ACM, so it must have some things sorted out that other regions do not have.

In the code, the configuration of this plugin will look something like this

It is important to mention that you must run the following command the first time you make a deploy:

serverless create_domain

3. Git Variables

One of the major bummers when I started using AWS Lambda was that it was (is) too difficult to keep a decent code versioning, so basically, having for example a staging environment was a difficult task.

This plugin has been very useful to manage multilple environments based on, for example, the branch that you are working on the repository.

By default, you would have to have differences on your serverless.yml file if you wanted to manage for example a staging and a production environment, but this plugin allows you to read from the branch, so you can seamlessly version your configuration file, without having to make a difference between branches.

The traditional approach:

Using Git Variables: