⚡️ 6 Serverless CLI Commands You Didn't Know Existed
Tags: serverless, aws, productivity, cloud
Published: 2021-08-24 20:57:31

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ib1k6mtnwvznd6jjheb.png)

[CLI Reference](https://www.serverless.com/framework/docs/providers/aws/cli-reference/create/)

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](https://www.npmjs.com/package/generator-sls-node) or my post on how to set up lightning fast builds with esbuild instead! 

{% link https://dev.to/aws-builders/lightning-fast-simple-typescript-serverless-builds-33ed %}

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eiga114a2i854g9tevim.png) 

[CLI Reference](https://www.serverless.com/framework/docs/providers/aws/cli-reference/generate-event/)

## 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)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0vp35plcglsgpbk4u8p.gif) 

[CLI Reference](https://www.serverless.com/framework/docs/providers/aws/cli-reference/config-tabcompletion/)

## 4. Invoke Deployed Functions

How to invoke Lambda functions locally, with the popular [serverless-offline](https://www.npmjs.com/package/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ipybyabiuj35hoc5712.png)
 
[CLI Reference](https://www.serverless.com/framework/docs/providers/aws/cli-reference/invoke/)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odeyixgg2d8iuuqf944o.png) 

[CLI Reference](https://www.serverless.com/framework/docs/providers/aws/cli-reference/logs/)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71tybvoypwsi7uz56zw5.png)
 
[CLI Reference](https://www.serverless.com/framework/docs/providers/aws/cli-reference/metrics/)

---

If you enjoyed this post and want to see more, follow me on Twitter at [@TastefulElk](https://twitter.com/TastefulElk) where I frequently write about serverless tech, AWS, and developer productivity! This post even [started out as a thread](https://twitter.com/TastefulElk/status/1424817977615134724) over there!