DEV Community

Cover image for ⚡️ 6 Serverless CLI Commands You Didn't Know Existed
Sebastian Bille for AWS Community Builders

Posted on

⚡️ 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.

Running just 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.

sls_boilerplat

CLI Reference

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 serverless generate-event

sls_generate-event

CLI Reference

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.

2021-08-09_20-55-31_(1)

CLI Reference

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!

Simply run serverless invoke --function myFunction, optionally including a payload, to run the deployed function!

sls_invoke

CLI Reference

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]

sls_logs

CLI Reference

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]

sls_metrics

CLI Reference


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!

Top comments (4)

Collapse
 
dvddpl profile image
Davide de Paolis

Proud of myself that I knew and use already 5 of them.. But how could I miss the most useful one!
Generate Mock Events that's brilliant.
Thanks for sharing!!

Collapse
 
tastefulelk profile image
Sebastian Bille

That's great Davide! Glad you found it helpful! 🎉

Collapse
 
jimingeorge profile image
jimingeorge

Informative! Discovered couple more commands that's helpful. Thanks

Collapse
 
tastefulelk profile image
Sebastian Bille

Great to hear! 🚀