|
| 1 | +# Lambda Worker |
| 2 | + |
| 3 | +This sample demonstrates how to run a Temporal Worker inside an AWS Lambda function using |
| 4 | +the [`lambda_worker`](https://python.temporal.io/temporalio.contrib.aws.lambda_worker.html) |
| 5 | +contrib package. It includes optional OpenTelemetry instrumentation that exports traces |
| 6 | +and metrics through AWS Distro for OpenTelemetry (ADOT). |
| 7 | + |
| 8 | +The sample registers a simple greeting Workflow and Activity, but the pattern applies to |
| 9 | +any Workflow/Activity definitions. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +- A [Temporal Cloud](https://temporal.io/cloud) namespace (or a self-hosted Temporal |
| 14 | + cluster accessible from your Lambda) |
| 15 | +- AWS CLI configured with permissions to create Lambda functions, IAM roles, and |
| 16 | + CloudFormation stacks |
| 17 | +- mTLS client certificate and key for your Temporal namespace (place as `client.pem` and |
| 18 | + `client.key` in this directory) |
| 19 | +- Python 3.10+ |
| 20 | + |
| 21 | +## Files |
| 22 | + |
| 23 | +| File | Description | |
| 24 | +|------|-------------| |
| 25 | +| `lambda_function.py` | Lambda entry point -- configures the worker, registers Workflows/Activities, and exports the handler | |
| 26 | +| `workflows.py` | Sample Workflow that executes a greeting Activity | |
| 27 | +| `activities.py` | Sample Activity that returns a greeting string | |
| 28 | +| `starter.py` | Helper program to start a Workflow execution from a local machine | |
| 29 | +| `temporal.toml` | Temporal client connection configuration (update with your namespace) | |
| 30 | +| `otel-collector-config.yaml` | OpenTelemetry Collector sidecar configuration for ADOT | |
| 31 | +| `deploy-lambda.sh` | Packages and deploys the Lambda function | |
| 32 | +| `mk-iam-role.sh` | Creates the IAM role that allows Temporal Cloud to invoke the Lambda | |
| 33 | +| `iam-role-for-temporal-lambda-invoke-test.yaml` | CloudFormation template for the IAM role | |
| 34 | +| `extra-setup-steps` | Additional IAM and Lambda configuration for OpenTelemetry support | |
| 35 | + |
| 36 | +## Setup |
| 37 | + |
| 38 | +### 1. Configure Temporal connection |
| 39 | + |
| 40 | +Edit `temporal.toml` with your Temporal Cloud namespace address and credentials. In production, |
| 41 | +we'd recommend reading your credentials from a secret store, but to keep this example simple |
| 42 | +the toml file defaults to reading them from keys bundled along with the Lambda code. |
| 43 | + |
| 44 | +### 2. Create the IAM role |
| 45 | + |
| 46 | +This creates the IAM role that Temporal Cloud assumes to invoke your Lambda function: |
| 47 | + |
| 48 | +```bash |
| 49 | +./mk-iam-role.sh <stack-name> <external-id> <lambda-arn> |
| 50 | +``` |
| 51 | + |
| 52 | +The External ID is provided by Temporal Cloud in your namespace's serverless worker |
| 53 | +configuration. |
| 54 | + |
| 55 | +### 3. (Optional) Enable OpenTelemetry |
| 56 | + |
| 57 | +If you want traces, metrics, and logs, you'll have to attach the ADOT layet to your Lambda function. |
| 58 | +You will need to add the appropriate layer for your runtime and region. See [this page |
| 59 | +](https://aws-otel.github.io/docs/getting-started/lambda#getting-started-with-aws-lambda-layers) |
| 60 | +for more info. |
| 61 | + |
| 62 | +Then run the extra setup to grant the Lambda role the necessary permissions: |
| 63 | + |
| 64 | +```bash |
| 65 | +./extra-setup-steps <role-name> <function-name> <region> <account-id> |
| 66 | +``` |
| 67 | + |
| 68 | +Update `otel-collector-config.yaml` with your function name and region as needed. |
| 69 | + |
| 70 | +### 4. Deploy the Lambda function |
| 71 | + |
| 72 | +```bash |
| 73 | +./deploy-lambda.sh <function-name> |
| 74 | +``` |
| 75 | + |
| 76 | +This installs Python dependencies, bundles them with your code and configuration files, |
| 77 | +and uploads to AWS Lambda. |
| 78 | + |
| 79 | +### 5. Start a Workflow |
| 80 | + |
| 81 | +Use the starter program to execute a Workflow on the Lambda worker, using |
| 82 | +the same config file the Lambda uses for connecting to the server: |
| 83 | + |
| 84 | +```bash |
| 85 | +TEMPORAL_CONFIG_FILE=temporal.toml uv run python lambda_worker/starter.py |
| 86 | +``` |
0 commit comments