Lambda Executor Modes
LocalStack currently supports three different modes for lambda execution. They differ in when and where your lambda code is executed, and in term of feature set and execution speed.
Execution modes
The active lambda executor can be set using the environment variable, which has the 3 possible options local
, docker
and docker-reuse
.
The default option is docker
, unless LocalStack has no access to a docker daemon itself when it will be set to local
.
Running docker containers inside the LocalStack docker images requires to bind mount the /var/run/docker.sock
. See the example below:
Configuration:
Local executor mode currently supports the following Lambda Platforms:
- Python
- Nodejs
- Go
Lambdas on other platforms like .Net currently need to be executed in one of the docker modes.
Configuration: LAMBDA_EXECUTOR=docker
The docker
execution mode will execute lambdas in a docker container. For this, every lambda invocation creates and runs a new docker container and returns its result when it’s finished. The advantage of this mode is the fresh lambda environment from each start, so possible leftovers during previous invocations will be purged. Due to the nature of this mode, mainly recreating the container for each invocation, this mode is rather slow. A typical invocation of a dummy python lambda can take around 3 seconds from start to finish (awscli invoke - start to finish). All supported lambda types can be used with this executor.
Configuration: LAMBDA_EXECUTOR=docker-reuse
Stay-open mode
The stay-open mode is the new default method when using as lambda executor, however, it has some restrictions:
- Only works if LocalStack runs in a Docker container
- Large Payloads (multiple MBs) do not work
- Problems with error handling in some runtimes
A list of failing tests with this mode can be found in this GitHub issue.
Docker-exec execution mode
This mode is the default if LocalStack is started in host mode. If you experience failures using the stay-open mode (either due to the mentioned restrictions or networking problems), you can force this mode by setting LAMBDA_STAY_OPEN_MODE=0
. Also if you want to use Hot Swapping you should set LAMBDA_STAY_OPEN_MODE=0
.
This execution mode provides a balance between the speed of a local execution and the feature set and isolation of the docker
executor. While the initial call, which creates the container, will take roughly the same time of docker
executor, the subsequent invocations will only take around 1 second (start to finish, invoked using the awscli), which is roughly the time an actual aws invocation using this method takes. The container is kept running 10 minutes after the last invocation for this lambda, then it will be destroyed (and recreated if necessary for the next invocation). The complete lambda process is called using docker-exec
each time of the invocation. While the invocation is still faster than docker
execution mode, it is not as fast as with the stay-open mode (since the lambda has to be loaded and initialized every time).