|
| 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 | +The instructions here are a slimmed down version of the more complete getting started guide which |
| 39 | +you can find [here](https://docs.temporal.io/production-deployment/worker-deployments/serverless-workers/aws-lambda). |
| 40 | + |
| 41 | +### 1. Create a lambda function for your Python worker |
| 42 | + |
| 43 | +Use either the AWS web UI or CLI to create a Python runtime Lambda function. Ex: |
| 44 | + |
| 45 | +```bash |
| 46 | +aws lambda create-function \ |
| 47 | + --function-name my-temporal-worker \ |
| 48 | + --runtime python3.13 \ |
| 49 | + --handler lambda_function.lambda_handler \ |
| 50 | + --role arn:aws:iam::<YOUR_ACCOUNT_ID>:role/my-temporal-worker-execution \ |
| 51 | + --timeout 600 \ |
| 52 | + --memory-size 256 |
| 53 | +``` |
| 54 | + |
| 55 | +### 2. Configure Temporal connection |
| 56 | + |
| 57 | +Edit `temporal.toml` with your Temporal Cloud namespace address and credentials. In production, |
| 58 | +we'd recommend reading your credentials from a secret store, but to keep this example simple |
| 59 | +the toml file defaults to reading them from keys bundled along with the Lambda code. |
| 60 | + |
| 61 | +### 3. Create the IAM role |
| 62 | + |
| 63 | +This creates the IAM role that Temporal Cloud assumes to invoke your Lambda function: |
| 64 | + |
| 65 | +```bash |
| 66 | +./mk-iam-role.sh <stack-name> <external-id> <lambda-arn> |
| 67 | +``` |
| 68 | + |
| 69 | +The External ID is provided by Temporal Cloud in your namespace's serverless worker |
| 70 | +configuration. |
| 71 | + |
| 72 | +### 4. (Optional) Enable OpenTelemetry |
| 73 | + |
| 74 | +If you want traces, metrics, and logs, you'll have to attach the ADOT layet to your Lambda function. |
| 75 | +You will need to add the appropriate layer for your runtime and region. See [this page |
| 76 | +](https://aws-otel.github.io/docs/getting-started/lambda#getting-started-with-aws-lambda-layers) |
| 77 | +for more info. |
| 78 | + |
| 79 | +Then run the extra setup to grant the Lambda role the necessary permissions: |
| 80 | + |
| 81 | +```bash |
| 82 | +./extra-setup-steps <role-name> <function-name> <region> <account-id> |
| 83 | +``` |
| 84 | + |
| 85 | +Update `otel-collector-config.yaml` with your function name and region as needed. |
| 86 | + |
| 87 | +### 5. Deploy the Lambda function |
| 88 | + |
| 89 | +```bash |
| 90 | +./deploy-lambda.sh <function-name> |
| 91 | +``` |
| 92 | + |
| 93 | +This installs Python dependencies, bundles them with your code and configuration files, |
| 94 | +and uploads to AWS Lambda. |
| 95 | + |
| 96 | +### 6. Configure Temporal to be able to invoke your lambda function |
| 97 | + |
| 98 | +Refer to the docs [here](https://docs.temporal.io/production-deployment/worker-deployments/serverless-workers/aws-lambda#create-worker-deployment-version). |
| 99 | + |
| 100 | +### 7. Start a Workflow |
| 101 | + |
| 102 | +Use the starter program to execute a Workflow on the Lambda worker, using |
| 103 | +the same config file the Lambda uses for connecting to the server: |
| 104 | + |
| 105 | +From inside this directory: |
| 106 | + |
| 107 | +```bash |
| 108 | +TEMPORAL_CONFIG_FILE=temporal.toml uv run python starter.py |
| 109 | +``` |
0 commit comments