|
| 1 | +# lambda_worker |
| 2 | + |
| 3 | +A wrapper for running [Temporal](https://temporal.io) workers inside AWS Lambda. A single |
| 4 | +`run_worker` call handles the full per-invocation lifecycle: connecting to the Temporal server, |
| 5 | +creating a worker with Lambda-tuned defaults, polling for tasks, and gracefully shutting down before |
| 6 | +the invocation deadline. |
| 7 | + |
| 8 | +## Quick start |
| 9 | + |
| 10 | +```python |
| 11 | +# handler.py |
| 12 | +from temporalio.common import WorkerDeploymentVersion |
| 13 | +from temporalio.contrib.aws.lambda_worker import LambdaWorkerConfig, run_worker |
| 14 | + |
| 15 | +from my_workflows import MyWorkflow |
| 16 | +from my_activities import my_activity |
| 17 | + |
| 18 | + |
| 19 | +def configure(config: LambdaWorkerConfig) -> None: |
| 20 | + config.worker_config["task_queue"] = "my-task-queue" |
| 21 | + config.worker_config["workflows"] = [MyWorkflow] |
| 22 | + config.worker_config["activities"] = [my_activity] |
| 23 | + |
| 24 | + |
| 25 | +lambda_handler = run_worker( |
| 26 | + WorkerDeploymentVersion( |
| 27 | + deployment_name="my-service", |
| 28 | + build_id="v1.0", |
| 29 | + ), |
| 30 | + configure, |
| 31 | +) |
| 32 | +``` |
| 33 | + |
| 34 | +## Configuration |
| 35 | + |
| 36 | +Client connection settings (address, namespace, TLS, API key) are loaded |
| 37 | +automatically from a TOML config file and/or environment variables via |
| 38 | +`temporalio.envconfig`. The config file is resolved in order: |
| 39 | + |
| 40 | +1. `TEMPORAL_CONFIG_FILE` env var, if set. |
| 41 | +2. `temporal.toml` in `$LAMBDA_TASK_ROOT` (typically `/var/task`). |
| 42 | +3. `temporal.toml` in the current working directory. |
| 43 | + |
| 44 | +The file is optional -- if absent, only environment variables are used. |
| 45 | + |
| 46 | +The configure callback receives a `LambdaWorkerConfig` dataclass with fields |
| 47 | +pre-populated with Lambda-appropriate defaults. Override any field directly in |
| 48 | +the callback. The `task_queue` key in `worker_config` is pre-populated from the |
| 49 | +`TEMPORAL_TASK_QUEUE` environment variable if set. |
| 50 | + |
| 51 | +## Lambda-tuned worker defaults |
| 52 | + |
| 53 | +The package applies conservative concurrency limits suited to Lambda's resource |
| 54 | +constraints: |
| 55 | + |
| 56 | +| Setting | Default | |
| 57 | +| --- | --- | |
| 58 | +| `max_concurrent_activities` | 2 | |
| 59 | +| `max_concurrent_workflow_tasks` | 10 | |
| 60 | +| `max_concurrent_local_activities` | 2 | |
| 61 | +| `max_concurrent_nexus_tasks` | 5 | |
| 62 | +| `workflow_task_poller_behavior` | `SimpleMaximum(2)` | |
| 63 | +| `activity_task_poller_behavior` | `SimpleMaximum(1)` | |
| 64 | +| `nexus_task_poller_behavior` | `SimpleMaximum(1)` | |
| 65 | +| `graceful_shutdown_timeout` | 5 seconds | |
| 66 | +| `max_cached_workflows` | 100 | |
| 67 | +| `disable_eager_activity_execution` | Always `True` | |
| 68 | + |
| 69 | +Worker Deployment Versioning is always enabled. |
| 70 | + |
| 71 | +## Observability |
| 72 | + |
| 73 | +Metrics and tracing are opt-in. The `otel` module provides convenience helpers |
| 74 | +for AWS Distro for OpenTelemetry (ADOT): |
| 75 | + |
| 76 | +```python |
| 77 | +from temporalio.common import WorkerDeploymentVersion |
| 78 | +from temporalio.contrib.aws.lambda_worker import LambdaWorkerConfig, run_worker |
| 79 | +from temporalio.contrib.aws.lambda_worker.otel import apply_defaults, OtelOptions |
| 80 | + |
| 81 | + |
| 82 | +def configure(config: LambdaWorkerConfig) -> None: |
| 83 | + config.worker_config["task_queue"] = "my-task-queue" |
| 84 | + config.worker_config["workflows"] = [MyWorkflow] |
| 85 | + config.worker_config["activities"] = [my_activity] |
| 86 | + apply_defaults(config, OtelOptions()) |
| 87 | + |
| 88 | + |
| 89 | +lambda_handler = run_worker( |
| 90 | + WorkerDeploymentVersion( |
| 91 | + deployment_name="my-service", |
| 92 | + build_id="v1.0", |
| 93 | + ), |
| 94 | + configure, |
| 95 | +) |
| 96 | +``` |
| 97 | + |
| 98 | +You can also use `apply_metrics` or `apply_tracing` individually. |
| 99 | + |
| 100 | +If you use OTEL, you can use |
| 101 | +[ADOT](https://aws-otel.github.io/docs/getting-started/lambda/lambda-python) |
| 102 | +(the AWS Distro For OpenTelemetry) to automatically integrate with AWS |
| 103 | +observability functionality. Namely, you will want to add the Lambda layer in |
| 104 | +the aforementioned link. We'll handle setting up the SDK for you. |
0 commit comments