⚡️ 6 Serverless CLI Commands You Didn't Know Existed
The Serverless Framework CLI can do a lot more than just deploy your app. Here are a few more ways you can use it in your everyday work that might not be obvious at first sight!
1. Bootstrap a Project
The CLI can help you initialize and bootstrap your new project, letting you choose between quite a few different project types and languages.
serverless lets you configure your project through a setup wizard with the most common project types and leads you all the way up to deploying the project for the first time.
serverless create is similar but instead expects you to provide it with either the name of a built-in project starter or an external one from Github or Bitbucket. Example usage:
serverless create --template aws-nodejs-typescript.
If you want a slightly more fully-fledged starter project that includes testing, linting & optional TS support with esbuild, feel free to check out my Yeoman generator or my post on how to set up lightning fast builds with esbuild instead!
2. Generate Mock Events
Have you ever deployed a project, published a message to the SNS topic, gone to CloudWatch, scoured the logs for the right output from your Lambda - just so that you could copy the SNS payload to use for local debugging or as a test event? I sure have.
As it turns out, there's a much easier way! The Serverless CLI can help generate mock events from SNS, SQS, DynamoDB, and many other service integrations, just by running
3. Install tab-completion
Install tab-completion to get the CLI to help you suggest available parameters and subcommands!
Currently supported shells are fish, bash, and zsh.
4. Invoke Deployed Functions
How to invoke Lambda functions locally, with the popular serverless-offline plugin, or by running
serverless invoke local is something that I think new serverless devs usually pick up on pretty fast.
Something that's surprisingly easy to overlook, however, is that the "invoke" command can also be used to invoke already deployed functions. This can be incredibly helpful in shortening the feedback loop when debugging those pesky issues that never seem to happen on your local machine!
serverless invoke --function myFunction, optionally including a payload, to run the deployed function!
5. Print or Tail Logs
Another nifty feature to further shorten the feedback loop is the ability to print or tail the logs of a deployed Lambda function to avoid having to open up the console for the CloudWatch logs. With this, you can even tail multiple function logs at the same time, side by side, by running the command in more than one window.
serverless logs --function myFunction [--tail]
6. Print Metrics
Print the metrics for the service or a given function to get a glance at how it's performing, right from the comfort of your CLI. This could for example be used to quickly figure out whether or not there have been any errors in any of the functions in the service or if the average execution time is creeping up.
serverless metrics [--function]
If you enjoyed this post and want to see more, follow me on Twitter at @TastefulElk where I frequently write about serverless tech, AWS, and developer productivity! This post even started out as a thread over there!