Serverless – CLI for AWS provider

Serverless CLI common commands for AWS provider, Summarized from https://serverless.com/framework/docs/providers/aws/cli-reference

0
113

I. Login

serverless login

II. Config Credentials

serverless config credentials --provider provider --key key --secret secret

Options

  • --provider or -p The provider (in this case aws). Required.
  • --key or -k The aws_access_key_idRequired.
  • --secret or -s The aws_secret_access_keyRequired.
  • --profile or -n The name of the profile which should be created. Default id “default
  • --overwrite or -o Overwrite the profile if it exists.

Example:

#Configure the default profile
serverless config credentials --provider aws --key 1234 --secret 5678

#Configure a custom profile
serverless config credentials --provider aws --key 1234 --secret 5678 --profile custom-profile

#Update an existing profile
serverless config credentials --provider aws --key 1234 --secret 5678 --profile custom-profile --overwrite

III. Deploy

The sls deploy command deploys your entire service via CloudFormation. Run this command when you have made infrastructure changes (i.e., you edited serverless.yml). Use serverless deploy function -f myFunction when you have made code changes and you want to quickly upload your updated code to AWS Lambda or just change function configuration.

serverless deploy

Options

  • --config or -c Path to your conifguration file, if other than serverless.yml|.yaml|.js|.json.
  • --stage or -s The stage in your service that you want to deploy to. Default is “dev
  • --region or -r The region in that stage that you want to deploy to. Default is “us-east-1
  • --package or -p path to a pre-packaged directory and skip packaging step.
  • --verbose or -v Shows all stack events during deployment, and display any Stack Output.
  • --force Forces a deployment to take place.
  • --function or -f Invoke deploy function (see above). Convenience shortcut – cannot be used with --package.
  • --conceal Hides secrets from the output (e.g. API Gateway key values).
  • --aws-s3-accelerate Enables S3 Transfer Acceleration making uploading artifacts much faster. You can read more about it here. It requires additional s3:PutAccelerateConfiguration permissions. Note: When using Transfer Acceleration, additional data transfer charges may apply.
  • --no-aws-s3-accelerate Explicitly disables S3 Transfer Acceleration. It also requires additional s3:PutAccelerateConfiguration permissions.

Artifacts

After the serverless deploy command runs, the framework runs serverless package in the background first then deploys the generated package.

Examples

#Deployment without stage and region options
serverless deploy

#Deployment with stage and region options
serverless deploy --stage production --region eu-central-1

#Deployment from a pre-packaged directory
serverless deploy --package /path/to/package/directory

IV. Deploy Function

The sls deploy function command deploys an individual function without AWS CloudFormation. This command simply swaps out the zip file that your CloudFormation stack is pointing toward. This is a much faster way of deploying changes in code.

serverless deploy function -f functionName

Note: This command now deploys both function configuration and code by default. Just as before, this puts your function in an inconsistent state that is out of sync with your CloudFormation stack. Use this for faster development cycles and not production deployments

Options

  • --function or -f The name of the function which should be deployed
  • --stage or -s The stage in your service that you want to deploy to.
  • --region or -r The region in that stage that you want to deploy to.
  • --update-config or -u Pushes ONLY Lambda-level configuration changes e.g. handler, timeout or memorySize

Examples

#Deployment without stage and region options
serverless deploy function --function helloWorld

#Deployment with stage and region options
serverless deploy function --function helloWorld --stage dev --region us-east-1

#Deploy only configuration changes
serverless deploy function --function helloWorld --update-config

V. Invoke

Invokes deployed function. It allows to send event data to the function, read logs and display other important information of the function invocation.

serverless invoke [local] --function functionName

Note: Please refer to this guide for event data passing when your function uses the http event with a Lambda Proxy integration.

Options

  • --function or -f The name of the function in your service that you want to invoke. Required.
  • --stage or -s The stage in your service you want to invoke your function in.
  • --region or -r The region in your stage that you want to invoke your function in.
  • --qualifier or -q The version number or alias to invoke your function in. Default is $LATEST.
  • --data or -d String data to be passed as an event to your function. By default data is read from standard input.
  • --raw Pass data as a raw string even if it is JSON. If not set, JSON data are parsed and passed as an object.
  • --path or -p The path to a json file with input data to be passed to the invoked function. This path is relative to the root directory of the service.
  • --type or -t The type of invocation. Either RequestResponseEvent or DryRun. Default is RequestResponse.
  • --log or -l If set to true and invocation type is RequestResponse, it will output logging data of the invocation. Default is false.

VI. Invoke Local

This runs your code locally by emulating the AWS Lambda environment. Please keep in mind, it’s not a 100% perfect emulation, there may be some differences, but it works for the vast majority of users. We mock the context with simple mock data.

serverless invoke local --function functionName

Note: Please refer to this guide for event data passing when your function uses the http event with a Lambda Proxy integration.

Options

  • --function or -f The name of the function in your service that you want to invoke locally. Required.
  • --path or -p The path to a json file holding input data to be passed to the invoked function as the event. This path is relative to the root directory of the service.
  • --data or -d String data to be passed as an event to your function. Keep in mind that if you pass both --path and --data, the data included in the --path file will overwrite the data you passed with the --data flag.
  • --raw Pass data as a raw string even if it is JSON. If not set, JSON data are parsed and passed as an object.
  • --contextPath or -x, The path to a json file holding input context to be passed to the invoked function. This path is relative to the root directory of the service.
  • --context or -c, String data to be passed as a context to your function. Same like with --data, context included in --contextPath will overwrite the context you passed with --context flag.
  • --env or -e String representing an environment variable to set when invoking your function, in the form <name>=<value>. Can be repeated for more than one environment variable.
  • --docker Enable docker support for NodeJS/Python/Ruby/Java. Enabled by default for other runtimes.
  • --docker-arg Pass additional arguments to docker run command when --docker is option used. e.g. --docker-arg '-p 9229:9229' --docker-arg '-v /var:/host_var'

VII. Remove

The sls remove command will remove the deployed service, defined in your current working directory, from the provider.

serverless remove

Options

  • --stage or -s The name of the stage in service.
  • --region or -r The name of the region in stage.
  • --verbose or -v Shows all stack events during deployment.

LEAVE A REPLY

Please enter your comment!
Please enter your name here