A wrapper for running Temporal workers inside AWS Lambda.
A single run_worker call handles the full per-invocation lifecycle: connecting to the
Temporal server, creating a worker with Lambda-tuned defaults, polling for tasks, and gracefully
shutting down before the invocation deadline.
Quick start:
from temporalio.common import WorkerDeploymentVersion
from temporalio.contrib.aws.lambda_worker import LambdaWorkerConfig, run_worker
def configure(config: LambdaWorkerConfig) -> None:
config.worker_config["task_queue"] = "my-task-queue"
config.worker_config["workflows"] = [MyWorkflow]
config.worker_config["activities"] = [my_activity]
lambda_handler = run_worker(
WorkerDeploymentVersion(
deployment_name="my-service",
build_id="v1.0",
),
configure,
)
Configuration
Client connection settings (address, namespace, TLS, API key) are loaded automatically from a TOML
config file and/or environment variables via temporalio.envconfig. The config file is
resolved in order:
- TEMPORAL_CONFIG_FILE env var, if set.
- temporal.toml in $LAMBDA_TASK_ROOT (typically /var/task).
- temporal.toml in the current working directory.
The file is optional -- if absent, only environment variables are used.
The configure callback receives a LambdaWorkerConfig dataclass with fields pre-populated
with Lambda-appropriate defaults. Override any field directly in the callback. The task_queue
key in worker_config is pre-populated from the TEMPORAL_TASK_QUEUE environment variable if
set.
| Module | otel |
OpenTelemetry helpers for Temporal workers running inside AWS Lambda. |
| Module | _configure |
Configuration for the Lambda worker. |
| Module | _defaults |
Lambda-tuned defaults for Temporal worker and client configuration. |
| Module | _run |
No module docstring; 0/1 variable, 0/1 type alias, 5/6 functions, 1/1 class documented |
From __init__.py:
| Class | |
Passed to the configure callback of run_worker. |
| Function | run |
Create a Temporal worker Lambda handler. |
WorkerDeploymentVersion, configure: ConfigureCallback) -> Callable[ [ Any, Any], None]:
(source)
ΒΆ
Create a Temporal worker Lambda handler.
Calls the configure callback to collect workflow/activity registrations and option overrides, then returns a Lambda handler function. On each invocation the handler connects to the Temporal server, starts a worker with Lambda-tuned defaults, polls for tasks until the invocation deadline approaches, and then gracefully shuts down.
The configure callback is invoked once per invocation and may be synchronous or asynchronous:
- Synchronous def configure(config) -> None β runs per invocation. Use for static worker definition (task queue, registrations, option tuning) and resources that are not bound to an event loop.
- Async async def configure(config) -> None β awaited per invocation. Use when setup must await (for example, opening an async client). Pair with shutdown_hooks for teardown.
- Async generator async def configure(config): ...; yield; ... (or an equivalent @contextlib.asynccontextmanager-decorated function) β entered per invocation. Statements before the single yield run before the client connects; the worker runs while the generator is suspended at the yield; statements after the yield run as teardown once the worker has stopped. Any shutdown_hooks registered before the yield run after the worker stops but before the post-yield teardown, so this resource outlives the hooks (e.g. a telemetry flush hook can still emit before the resource is closed). This is the recommended shape for event-loop-bound resources that must live for the duration of the invocation, such as an aioboto3 S3 client backing the external-storage data converter (see the async example below).
The callback runs per invocation (not once at cold start) because event-loop-bound resources cannot be created at cold start (there is no running loop) and cannot be shared across invocations (each invocation runs under a fresh asyncio.run loop).
The version parameter identifies this worker's deployment version. run_worker always enables Worker Deployment Versioning (use_worker_versioning=True). To provide a default versioning behavior for workflows that do not specify one at registration time, set deployment_config in worker_config in the configure callback.
The returned handler has the signature handler(event, context) and should be set as your Lambda function's handler entry point.
Example
Synchronous configure (static worker definition):
from temporalio.common import WorkerDeploymentVersion
from temporalio.contrib.aws.lambda_worker import (
LambdaWorkerConfig,
run_worker,
)
def configure(config: LambdaWorkerConfig) -> None:
config.worker_config["task_queue"] = "my-task-queue"
config.worker_config["workflows"] = [MyWorkflow]
config.worker_config["activities"] = [my_activity]
lambda_handler = run_worker(
WorkerDeploymentVersion(
deployment_name="my-service",
build_id="v1.0"),
configure,
)
Async generator configure, bracketing an aioboto3 S3 client. The session lives at module scope (it is not event-loop-bound and caches credentials across warm invocations); only the loop-bound client is opened per invocation:
import aioboto3
import dataclasses
from temporalio.contrib.aws.s3driver import S3StorageDriver
from temporalio.contrib.aws.s3driver.aioboto3 import new_aioboto3_client
from temporalio.converter import DataConverter, ExternalStorage
session = aioboto3.Session()
async def configure(config: LambdaWorkerConfig):
config.worker_config["task_queue"] = "my-task-queue"
config.worker_config["workflows"] = [MyWorkflow]
async with session.client("s3") as s3_client:
driver = S3StorageDriver(
client=new_aioboto3_client(s3_client), bucket="my-payloads",
)
config.client_connect_config["data_converter"] = dataclasses.replace(
DataConverter.default,
external_storage=ExternalStorage(drivers=[driver]),
)
yield
lambda_handler = run_worker(
WorkerDeploymentVersion(
deployment_name="my-service",
build_id="v1.0"),
configure,
)
| Parameters | |
version:WorkerDeploymentVersion | The worker deployment version. Required. |
configure:ConfigureCallback | A callback that receives a LambdaWorkerConfig
(pre-populated with Lambda defaults) and configures workflows,
activities, and options on it. May be sync, async, or an async
generator (see above). |
| Returns | |
Callable[ | A Lambda handler function. |