Skip to main content

Deploy a Serverless Worker on Modal

View Markdown

Deploy your Worker code as a Modal function and configure Temporal to invoke that function when Tasks need capacity. The function starts a Worker that connects to Temporal and polls its Task Queue.

For the invocation lifecycle and scaling controls, see Serverless Workers on Modal.

Prerequisites​

  • A Temporal Cloud Namespace where you can configure Worker Deployments and start Workflow Executions.
  • A Modal account with permission to deploy apps and create secrets in your target environment.
  • Python 3.12. This guide also uses uv to manage dependencies.
  • Temporal CLI v1.10.0 or later.
  • A Workflow with a versioning behavior and the Activities it uses.
Tip

If you don't have a Workflow ready, use the Workflow and Activity files from the Python Modal Worker sample.

1. Write Worker code​

Use the Python Modal Worker integration to run a Temporal Worker inside a Modal function. The Worker connects to Temporal, polls for Tasks, and shuts down before the function timeout.

Create a project directory and the Worker package.

mkdir -p modal-worker/modal_worker
cd modal-worker
touch modal_worker/__init__.py

Create modal_worker/worker.py. Import your Workflow and Activity definitions and register them in the configure callback. The example registers GreetingWorkflow and greet on my-task-queue.

python-sandbox/workers/modal_worker/worker.py

from datetime import timedelta

import modal
from temporalio.common import WorkerDeploymentVersion
from temporalio.worker_integrations.modal.function import ModalWorkerConfig, run_worker

from modal_worker.activities import greet
from modal_worker.workflows import GreetingWorkflow

FUNCTION_TIMEOUT = timedelta(minutes=5)
VERSION = WorkerDeploymentVersion(deployment_name="my-app", build_id="build-1")

image = (
modal.Image.debian_slim(python_version="3.12")
.pip_install("temporalio-modal-function-worker==0.0.1")
.add_local_python_source("modal_worker")
)
app = modal.App("temporal-worker", image=image)


def configure(config: ModalWorkerConfig) -> None:
config.client_connect_config["tls"] = True
config.worker_config["task_queue"] = "my-task-queue"
config.worker_config["workflows"] = [GreetingWorkflow]
config.worker_config["activities"] = [greet]


@app.function(
timeout=int(FUNCTION_TIMEOUT.total_seconds()),
secrets=[modal.Secret.from_name("temporal-cloud-credentials")],
)
async def temporal_worker() -> None:
await run_worker(
VERSION,
configure,
modal_function_timeout=FUNCTION_TIMEOUT,
)

Keep the function timeout in @app.function equal to the timeout passed to run_worker.

2. Deploy the function​

Deploy the function to the Modal environment where you want the Worker to run. During deployment, Modal builds the image described in worker.py, installs its dependencies, and includes your source files.

2.1 Prepare the deployment​

From the project root, initialize the Python project and install the integration.

uv init --python 3.12
uv add temporalio-modal-function-worker==0.0.1

In Modal, select the workspace and environment where the Worker will run. Create a secret named temporal-cloud-credentials with the following values.

KeyValue
TEMPORAL_ADDRESSYour Namespace's gRPC endpoint, from its connection settings.
TEMPORAL_NAMESPACEYour Namespace name.
TEMPORAL_API_KEYA Temporal Cloud API key authorized to access the Namespace.

The Worker example explicitly enables TLS and attaches this secret through secrets=[modal.Secret.from_name("temporal-cloud-credentials")]. Secrets are scoped to Modal environments. Create the secret in the same environment as the function, unless you explicitly configure a cross-environment lookup.

For instructions on creating the secret in the dashboard, see the Modal secrets guide.

2.2 Deploy the app​

Authenticate the Modal CLI to the intended workspace if you have not already done so:

uv run modal setup

From the project root, deploy to an existing Modal environment. Replace temporal-workers with its name.

export MODAL_ENVIRONMENT=temporal-workers
uv run modal deploy --env "$MODAL_ENVIRONMENT" -m modal_worker.worker

Confirm that temporal-worker contains a function named temporal_worker in the intended environment.

2.3 Record the app version​

In the app's Deployment History, record the numeric version for this deployment. Set it for the commands below, replacing 1 with that version:

export MODAL_APP_VERSION=1

Use the numeric app version when configuring Temporal. It identifies the deployed code for this Build ID.

3. Authorize Temporal to invoke the function​

Connect the Modal workspace that contains your App to your Temporal Cloud account. You must be a Temporal Cloud Account Owner to authorize the connection in the Temporal Cloud UI. The authorization covers one workspace. You select the App and function when you create the Worker Deployment Version in the next step.

  1. In Temporal Cloud, go to Settings > Connections.
  2. Under Modal, select Connect Modal. If the account already has a workspace connection, select Connect Workspace.
  3. In the Modal window that opens, select the workspace you deployed to and approve the authorization request.
  4. When the window confirms the connection, close it. On the Connections page, confirm that your workspace appears under Modal.

4. Create Worker Deployment Version​

Create a Worker Deployment Version that points to your Modal function. The deployment name and Build ID must match the values in modal_worker/worker.py. Use the numeric app version you recorded in step 2.3 to keep this Worker Deployment Version tied to the same code.

  1. In Temporal Cloud, open your Namespace and select Workers.

  2. Select Create Worker Deployment.

  3. Enter my-app for the deployment name and build-1 for the Build ID.

  4. Under Compute, select Modal and enter the following values.

    FieldValue
    Modal WorkspaceThe workspace you connected in step 3.
    App Nametemporal-worker.
    Function Nametemporal_worker.
    EnvironmentThe Modal environment you deployed to in step 2.2.
    App VersionThe numeric version you recorded in step 2.3.
  5. Save the Worker Deployment. The creation form validates the compute configuration and sets the initial version as current. Continue to Verify deployment.

To check the connection, go to Workers > Deployments, select your deployment, and choose Validate Connection from the version's Actions menu. This checks whether Temporal can access the configured Modal function.

5. Set version as current​

If you created the deployment in the Temporal UI, continue to Verify deployment. For a version created through the CLI, wait for the initial Modal invocation to register the Worker's Task Queue, then set the version as current.

Confirm that the Worker has registered the expected Task Queue before directing new Workflows to it:

temporal worker deployment version describe \
--deployment-name my-app \
--build-id build-1

If my-task-queue is missing, follow Troubleshoot Serverless Workers on Modal to check invocation and Worker startup. Do not bypass the missing-Task-Queue check.

Set build-1 as the Current Version for this Worker Deployment:

temporal worker deployment set-current-version \
--deployment-name my-app \
--build-id build-1

6. Verify deployment​

Use a Task Queue dedicated to this test. In Modal, check that the initial invocation has finished. Then check the Task Queue's poller information.

temporal task-queue describe --task-queue my-task-queue

Start a Workflow to check that Temporal requests a new Modal invocation. Replace the Workflow Type and input if you registered your own Workflow.

temporal workflow start \
--workflow-id modal-greeting-test-1 \
--type GreetingWorkflow \
--task-queue my-task-queue \
--input '"World"' \
--execution-timeout 5m

Use a new Workflow ID for each test. Check the result:

temporal workflow show --workflow-id modal-greeting-test-1

The sample Workflow should complete with "Hello, World!". Verify the deployment in both systems.

  • Temporal UI — Check the Workflow's Event History for completed Tasks and confirm that the intended Worker Deployment Version processed them.
  • Connection status — Go to Workers > Deployments and use Validate Connection to check access to the configured function.
  • Modal — Check the function's invocation history and logs for a new invocation after you started the Workflow. The Worker can keep polling until its invocation budget ends.

If the Workflow does not progress or Modal does not receive an invocation, see Troubleshoot Serverless Workers on Modal.

Configure scale-up for rate-limited Task Queues​

If your Modal Workers poll a rate-limited Task Queue, set the dispatch-rate epsilon to 0.05 in the scaling group. While the queue has a backlog, the scaler can pause new Worker starts when the dispatch rate stays within the configured band. See the Modal CLI reference for the related settings.

temporal worker deployment version compute-config modal-function update \
--deployment-name my-app \
--build-id build-1 \
--scale-up-dispatch-rate-epsilon 0.05

Add --scaling-group <SCALING_GROUP> if the group has a name other than default. The update replaces the group's scaler settings. Include any other scaler flags you want to keep.