Lately I've been using Serverless on a variety of projects, including personal ones.
Put simply, it's a tool that lets you efficiently create and update resources of the kind known as FaaS (Function as a Service), like AWS Lambda or Google Cloud Functions.
Apex and AWS's own SAM CLI could also be considered similar tools.
Background
With Serverless, you can deploy with a single command, so there's no need to bundle code and dependencies before uploading. That makes it easy to deploy from your local machine or run it from CI.
If the deployment flow were completely unified, there'd be no issue, but sometimes you normally deploy from CI yet want to deploy a small fix locally, or you want to roll back after a failed deploy.
In situations like that, I'd occasionally find myself wondering "wait, what state is this in right now?", so I wanted an easy way to attach version information.
So I made it read the information from package.json and add it at deploy time.
What I did
I had been managing additional information in a separate YAML file, but I recently learned that JSON files can be loaded too.
If you've installed serverless under your project via npm or yarn, you should have a package.json, so let's load its version field as additional info.
As an example, suppose a directory contains the source code along with package.json and serverless.yml.
package.json has a version of 1.0.0.
{
...
"version": "1.0.0",
...
}
To load this in serverless.yml, do the following.
custom:
package: ${file(./package.json)} # Load package.json
With this in place, writing ${self:custom.package.version} lets you attach the version info of 1.0.0 at Serverless deploy time, so I passed it into a VERSION environment variable.
provider:
...
environment:
VERSION: ${self:custom.package.version} # Assign to the VERSION environment variable
This assumes you keep the version info updated appropriately, but now I can easily check which state is currently deployed.