• Documentation
  • Pricing
© 2026 Serverless, Inc. All rights reserved.

Framework

  • Overview
  • Documentation
  • Plugins360
  • Pricing

Learn

  • Blog
  • GuidesUpdated
  • Examples240
  • Courses

Resources

  • Support
  • Security
  • Trust Center
  • Status

Community

  • Slack
  • GitHub47k
  • Forum
  • Meetups

Company

  • About
  • Careers
  • Contact
  • Partners

Legal

  • Terms of Service
  • Privacy Policy
  • Trademark
  • DMCA
Serverless Framework Logo

Serverless Framework

Intro
SetupUpgrading To V4ConceptsTutorialAWS CredentialsLicense Keys
DeployingPackagingBuildingTestingDev ModeServicesFunctions
OverviewHTTP (API Gateway v2)REST (API Gateway v1)ActiveMQApplication Load BalancerAlexa SkillAlexa Smart HomeCloudWatch EventCloudWatch LogCloudFrontCognito User PoolEventBridge EventIoTIoT Fleet ProvisioningKafkaKinesis & DynamoDBMSKRabbitMQS3ScheduleSNSSQSWebsocket
LayersManaged InstancesWeb Apps (Lambda Web Functions)Sandboxes (Lambda MicroVMs)MCP ServersAlertsVersion PruningDomainsIAM Function PermissionsParameters
OverviewSelf-reference serverless.ymlServerless CoreEnvironment VariablesCLI OptionsExternal YAML/JSON FilesJavascript propertiesGitDoppler
OverviewS3 ObjectsSSM Parameter Store & Secrets ManagerCloudFormation Stack Outputs
OverviewVaultTerraform State Output
ResourcesComposing ServicesDeployment BucketStateAgent SkillsPython support
OverviewRuntimeGatewayMemoryBrowserCode InterpreterDev Mode
API Gateway Proxy
OverviewGeneral ConfigurationAuthenticationAPI KeysData SourcesResolversPipeline FunctionsSubstitutionsCachingDelta SyncCustom DomainWAFCLI Commands
Deploying SAM/CFN TemplatesWorkflow Tips
OverviewCreating PluginsCLI OutputCustom CommandsCustom VariablesExtending the Configuration schemaExtending and overriding configuration
OverviewDashboardAxiom
Overviewagent docsagent inspectagent setupagent skills installagent skills list / readdeploydeploy functiondeploy listdevdiffinfoinvokeinvoke localloginlogin awslogin aws ssologsmetricspackageplugin installplugin uninstallprintprunereconcileremoverollbackrollback functionusage
OverviewConcepts
OverviewMetricsTracesTroubleshoot
OverviewNode.jsPython
OutputsProviders
OverviewBranch DeploymentsPreview DeploymentsCustom ScriptsTestingPrivate PackagesNotificationsMono ReposDeploy in your own CI/CDBest PracticesTroubleshootingFAQ
OverviewSetupToolsAWS Integration
Serverless.yml Reference
Examples and TutorialsConfiguration Validation
  1. CLI Reference
  2. Invoke Local

serverless invoke local

serverless invoke local runs one function's handler on your machine with the event you pass, in an emulation of the Lambda environment: Lambda's environment variables, the function's environment, and a mock context. Nothing is deployed. Use it to test code before serverless deploy; to call the deployed function in AWS, use serverless invoke, and to run a stage's real events on your working copy, use serverless dev.

serverless invoke local --function hello --data '{"name":"Ada"}'

For a Node.js function, the command prints the handler's return value as JSON:

{
    "statusCode": 200,
    "body": "Hello, Ada"
}

The emulation is not exact: the function runs with your AWS credentials instead of its IAM role, and, without --docker, on the language runtime installed on your machine.

Options

serverless invoke local accepts these options:

OptionAliasValueDefaultRequiredDescription
--function-fstringYesFunction to run, as named under functions in serverless.yml.
--data-dstringStandard inputNoEvent to pass. JSON is parsed into an object unless --raw is set.
--path-pfile pathNoJSON or YAML file holding the event, relative to the service directory or absolute. Ignored when --data is set.
--rawbooleanfalseNoPass --data and --context as strings even when they are valid JSON.
--contextstringNoContext to pass. See Mock context. No short alias: -c is --config.
--contextPath-xfile pathNoJSON or YAML file holding the context, relative to the service directory or absolute. Takes precedence over --context.
--env-eNAME=valueNoEnvironment variable to set for the function (repeatable). Overrides every other source.
--dockerbooleanfalseNoRun the function in Docker even when its runtime can run on your machine. See Run a function in Docker.
--docker-argstringNoOne extra docker run argument and its value, such as '-p 9229:9229'. The option takes one value.
--stage-sstringprovider.stage, else devNoStage whose configuration to use.
--region-rstringprovider.region, else us-east-1NoAWS Region set as AWS_REGION and used to resolve Ref and Fn::ImportValue.
--aws-profilestringprovider.profileNoAWS profile whose credentials the function receives.
--config-cfile pathserverless.ymlNoService configuration file to use.

The command also accepts the options every command takes: --param="key=value" (repeatable), --org, --app, --verbose, --debug and --help (-h).

How each runtime runs

serverless invoke local picks how to run the function from its runtime:

RuntimeHow the function runs
nodejs*Inside the CLI's own Node.js process. The handler module is imported, trying .js, .mjs and .cjs when the path has no extension.
python3.7 to python3.15Under the python3 on your PATH (python on Windows), with the active virtual environment's bin first.
java8, java8.al2023, java11, java11.al2023, java17, java17.al2023, java21, java25Under java, through a bridge that Maven (mvn) builds on first use. The service-level package.artifact must exist; a function's own package.artifact isn't used.
ruby2.7, ruby3.2, ruby3.3, ruby3.4, ruby4.0Under the ruby on your PATH.
Any other runtimeIn Docker.

On your machine, the function runs on the installed interpreter, not the version its runtime names (for nodejs* functions, the Node.js version that runs the CLI): a function declared runtime: python3.13 runs on whatever python3 resolves to, and the difference is not reported. To run on the declared runtime, add --docker.

When the service uses the Framework's built-in esbuild bundling (TypeScript handlers, or build.esbuild; see Building), serverless invoke local bundles the function before it runs, as serverless deploy would. With --docker, the command also runs serverless package.

A function deployed from a container image stops the command with Local invocation of lambdas pointing AWS ECR images is not supported.

A function on a customer-managed runtime stops the command with Function "NAME" uses a customer-managed runtime, which cannot run locally. Deploy and run "serverless invoke --function NAME" instead. The runtime's contents are a container image that only Lambda reads, so there is nothing to run locally.

Run a function in Docker

With --docker, or for a runtime the preceding table doesn't list, serverless invoke local:

  1. Runs serverless package and extracts the package into .serverless/invokeLocal/artifact.
  2. Builds an image named sls-docker-<runtime> from the AWS base image for the runtime (public.ecr.aws/lambda/<language>:<version>, for Node.js, Python, Java, Ruby, .NET and provided runtimes), with the function's layers copied to /opt. Layers referenced by ARN are downloaded and cached.
  3. Starts the container with the package mounted read-only at /var/task, sends the event to it, and prints the container's log and the function's response.

Docker must be installed and running; otherwise the command stops with Please start the Docker daemon to use the invoke local Docker integration. On macOS and Windows, install Docker Desktop; on Linux, install Docker Engine and add your user to the docker group so Docker runs without sudo.

In Docker, the function receives AWS_REGION, AWS_DEFAULT_REGION, AWS_LAMBDA_FUNCTION_NAME, AWS_LAMBDA_FUNCTION_MEMORY_SIZE, AWS_LAMBDA_LOG_GROUP_NAME, your AWS credentials, the function's environment and the --env values. The command exits with code 1 when the function returns an error.

Event data

serverless invoke local takes the event from the first of these that is set:

  1. --data.
  2. --path: a .json or .yml/.yaml file is parsed; any other file is read as text.
  3. Standard input, when standard input is not a terminal.

Unless --raw is set, text that is valid JSON is parsed and passed as an object; other text is passed as a string. To test a function behind an http or httpApi event, pass an event in the API Gateway Lambda proxy input format, for example in lib/data.json:

{
  "resource": "/",
  "path": "/",
  "httpMethod": "GET"
}

Mock context

serverless invoke local passes the handler a mock context. The function's timeout (default 6 seconds) drives the remaining-time method:

RuntimeMock context--context and --contextPath
Node.jsawsRequestId (a random UUID), functionName, functionVersion, logGroupName, logStreamName, memoryLimitInMB, getRemainingTimeInMillis(), succeed(), fail(), done()Replace the whole mock context.
Pythonfunction_name, function_version, invoked_function_arn, memory_limit_in_mb, aws_request_id, log_group_name, log_stream_name (all read-only), get_remaining_time_in_millis()Set name, version and timeout, or add new attributes. Setting a read-only attribute stops the invocation with an AttributeError.
Rubyfunction_name, function_version, aws_request_id, invoked_function_arn, log_group_name, log_stream_name, memory_limit_in_mb, deadline_ms, get_remaining_time_in_millisSet function_name, version, timeout and memory_limit_in_mb. Other keys are stored without a reader method, so context.<key> raises NoMethodError in the handler. aws_request_id, invoked_function_arn, log_group_name and log_stream_name are computed after the overrides, so values for them are ignored.
Javaname, version, logGroupName, timeoutReplace the whole mock context.

Without --raw, serverless invoke local parses the --context text as JSON only when the event is JSON text from --data or standard input, or when there is no event. With an event file from --path, or a --data value that isn't JSON, the --context text isn't parsed: a Node.js handler receives it as a string. To pass a context object with an event file, put the context in a file and pass it with --contextPath:

serverless invoke local --function hello --path event.json --contextPath context.json

Environment variables

serverless invoke local sets these environment variables for a function that runs on your machine, each source overriding the ones before it:

  1. Lambda's variables, with local values: LANG, LD_LIBRARY_PATH, LAMBDA_TASK_ROOT, LAMBDA_RUNTIME_DIR, AWS_REGION, AWS_DEFAULT_REGION, AWS_LAMBDA_LOG_GROUP_NAME, AWS_LAMBDA_LOG_STREAM_NAME, AWS_LAMBDA_FUNCTION_NAME, AWS_LAMBDA_FUNCTION_MEMORY_SIZE (the function's memorySize, else provider.memorySize, else 1024), AWS_LAMBDA_FUNCTION_VERSION ($LATEST) and NODE_PATH.
  2. The CLI's AWS credentials: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN, and AWS_PROFILE when a profile is set with --aws-profile or provider.profile.
  3. provider.environment, then the function's environment.
  4. IS_LOCAL set to true, so code can tell a local run apart.
  5. The --env values.

IS_LOCAL and the --env values are set after serverless.yml is resolved, so ${env:IS_LOCAL} and other ${env:…} variables in serverless.yml don't see them.

In environment, the Ref and Fn::ImportValue CloudFormation intrinsic functions are resolved against the deployed stack and the Region's exports. Any other intrinsic function stops the command with Could not resolve "<name>" environment variable: Unsupported environment variable format. The short forms !Ref and !ImportValue work the same way. Ref reads the stack of the --stage and --region the command runs with, so that stack must be deployed; otherwise the command stops with Could not resolve "<name>" environment variable. The functions are resolved before the --env values apply, so --env doesn't avoid that error: deploy the stack, or replace the intrinsic function with a ${…} variable for local runs. Framework variables such as ${sls:stage} are resolved with the rest of serverless.yml. In this example, EXT_TABLE_NAME gets the value of the exported-tableName export and REF_TABLE_NAME the physical ID of myTable:

functions:
  hello:
    handler: handler.hello
    environment:
      EXT_TABLE_NAME:
        Fn::ImportValue: exported-tableName
      REF_TABLE_NAME:
        Ref: myTable

AWS credentials and permissions

A deployed function calls AWS with its execution role, which holds the permissions from provider.iam.role.statements. Under serverless invoke local that role isn't available: the function's AWS SDK calls use the credentials the CLI uses, passed as AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN. Those credentials usually grant different permissions than the role, so a call that succeeds locally can be denied in AWS, and the reverse. The AWS SDK for JavaScript credentials guide explains how the SDK picks them up.

Side effects and CI use

  • serverless invoke local creates and changes nothing in AWS, but the function's own AWS calls act on real resources with your credentials. Resolving Ref and Fn::ImportValue reads the deployed stack and the Region's exports.
  • With --docker, the command writes the package and the extracted artifact under .serverless/, and builds a local Docker image.
  • The command does not prompt. Without --data or --path, it reads the event from standard input whenever standard input is not a terminal, so in CI pass --data or --path, or redirect standard input from /dev/null.
  • For a Node.js function that throws or returns an error, the command prints errorMessage, errorType and stackTrace as JSON and exits with code 1.

Provided lifecycle events

  • invoke:local:loadEnvVars
  • invoke:local:invoke

Examples

Run a function with data

serverless invoke local --function hello --data '{"a":"bar"}'

Pass data as a raw string

serverless invoke local --function hello --raw --data "hello world"

Run a function with data from standard input

node dataGenerator.js | serverless invoke local --function hello

Run a function with an event file

serverless invoke local --function hello --path lib/data.json

Override the timeout in a Python function's context

serverless invoke local --function report --context '{"timeout":120}'

Run a function with a context file

serverless invoke local --function hello --contextPath lib/context.json

Set environment variables

serverless invoke local --function hello --env VAR1=value1 --env VAR2=value2

Run a function in Docker with a debugger port

serverless invoke local --function hello --docker --docker-arg '-p 9229:9229'

Related commands

  • serverless invoke calls the deployed function in AWS.
  • serverless dev runs a stage's real invocations on your working copy.
  • serverless package builds the package that --docker runs.
Edit this page
Prev invokeNextlogin

Contents

  • serverless invoke local
  • Options
  • How each runtime runs
  • Run a function in Docker
  • Event data
  • Mock context
  • Environment variables
  • AWS credentials and permissions
  • Side effects and CI use
  • Provided lifecycle events
  • Examples
  • Run a function with data
  • Pass data as a raw string
  • Run a function with data from standard input
  • Run a function with an event file
  • Override the timeout in a Python function's context
  • Run a function with a context file
  • Set environment variables
  • Run a function in Docker with a debugger port
  • Related commands

Related

GuidesPluginsExamplesSlack CommunitySupport