Skip to content
On this page

cargo lambda watch

The watch subcommand emulates the AWS Lambda control plane API. Run this command at the root of a Rust workspace and cargo-lambda will use cargo-watch to hot compile changes in your Lambda functions. Use flag --no-reload to avoid hot compilation.


This command works best with the Lambda Runtime version 0.5.1. Previous versions of the runtime are likely to crash with serialization errors.

cargo lambda watch

The function is not compiled until the first time that you try to execute it. See the invoke command to learn how to execute a function. Cargo will run the command cargo run --bin FUNCTION_NAME to try to compile the function. FUNCTION_NAME can be either the name of the package if the package has only one binary, or the binary name in the [[bin]] section if the package includes more than one binary.

Environment variables

If you need to set environment variables for your function to run, you can specify them in the metadata section of your Cargo.toml file.

Use the section package.metadata.lambda.env to set global variables that will applied to all functions in your package:

name = "basic-lambda"

RUST_LOG = "debug"
MY_CUSTOM_ENV_VARIABLE = "custom value"

If you have more than one function in the same package, and you want to set specific variables for each one of them, you can use a section named after each one of the binaries in your package, package.metadata.lambda.bin.BINARY_NAME:

name = "lambda-project"

RUST_LOG = "debug"



name = "get-product"
path = "src/bin/"

name = "add-product"
path = "src/bin/"

You can also set environment variables on a workspace

RUST_LOG = "debug"


These behave in the same way, package environment variables will override workspace settings, the order of precedence is:

  1. Package Binary
  2. Package Global
  3. Workspace Binary
  4. Workspace Global

Function URLs

The emulator server includes support for Lambda function URLs out of the box. Since we're working locally, these URLs are under the /lambda-url path instead of under a subdomain. The function that you're trying to access through a URL must respond to Request events using lambda_http, or raw ApiGatewayV2httpRequest events.

You can create functions compatible with this feature by running cargo lambda new --http FUNCTION_NAME.

To access a function via its HTTP endpoint, start the watch subcommand cargo lambda watch, then send requests to the endpoint http://localhost:9000/lambda-url/FUNCTION_NAME. You can add any additional path after the function name, or any query parameters.

Enabling features

You can pass a list of features separated by comma to the watch command to load them during run:

cargo lambda watch --features feature-1,feature-2

Release mode

You can also run your code in release mode if needed when the emulator is loaded:

cargo lambda watch --release

Released under the MIT License.