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 back to Temporal and polls its Task Queue. Modal builds and runs the container image from your Python image definition; you do not need a separate image registry.

This guide uses the Python Modal integration. For the invocation lifecycle and scaling controls, see Serverless Workers on Modal.

Prerequisites​

  • A Temporal Namespace with the Modal compute provider enabled, and permission to configure Worker Deployments and start Workflow Executions in that Namespace.
  • Authorization for Temporal to invoke functions in your Modal workspace. This is separate from the credentials your Worker uses to connect to Temporal.
  • Access to deploy apps and create secrets in a Modal workspace and environment.
  • Python 3.12 and uv for the example.
  • A Temporal CLI build that provides temporal worker deployment version compute-config modal-function. Check that this command's --help works before continuing.
  • A configured Temporal CLI connection. Use the same Namespace for every Temporal command in this guide.

The Python integration is experimental. Availability of its package does not establish availability of the Modal compute provider in your Temporal environment. Confirm access with your administrator before deploying.

Prepare the Worker code​

Follow the Python integration guide to install the integration and create activities.py, workflows.py, and worker.py. The complete example uses these values:

SettingValue
Temporal Worker Deployment namemy-app
Temporal Build IDbuild-1
Temporal Task Queuemy-task-queue
Workflow TypeGreetingWorkflow
Modal app nametemporal-worker
Modal function nametemporal_worker
Modal secret nametemporal

The Modal function must be callable without arguments. Temporal starts it as a Worker, rather than passing the Workflow input to the function. The Worker retrieves Workflow and Activity Tasks from Temporal.

When adapting the example, declare a versioning behavior for each Workflow and keep the Modal and integration timeouts equal.

Configure the Worker's Temporal credentials​

In the Modal dashboard, select the workspace and environment where the Worker will run. Create a secret named temporal containing:

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

The Worker example explicitly enables TLS and attaches this secret through secrets=[modal.Secret.from_name("temporal")]. 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 the Client configuration options, see Configure the Temporal connection. For secret management, see Modal secrets.

Deploy the app​

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

uv run modal setup

Select an existing Modal environment for the deployment. Replace temporal-workers with its name:

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

Modal builds the image, installs the dependencies, includes the local source modules, and deploys the function. Confirm that temporal-worker contains a function named temporal_worker in the intended environment.

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

A Modal deployment tag is separate from the numeric app version. Use the numeric version when configuring Temporal. See Modal deployments.

Authorize Temporal to invoke Modal​

Have your Temporal administrator confirm that the Namespace's account has an authorized connection to the Modal workspace containing your function. Record the connected workspace identifier as MODAL_WORKSPACE_ID for the next step. Creating a Modal secret with a Temporal API key does not grant this invocation permission.

Replace the placeholder with the connected workspace identifier:

export MODAL_WORKSPACE_ID="<connected-workspace-id>"

The compute provider selects a connected workspace through --workspace-id when Temporal uses per-workspace OAuth credentials. A server configured to use a single Modal API key uses a different credential mode and omits this flag. Use the mode configured by your administrator; do not omit the workspace identifier to work around an authorization error.

Configure the Worker Deployment Version​

Create the Worker Deployment if it does not already exist:

temporal worker deployment create --name my-app

Create a new Worker Deployment Version with Modal compute configuration:

temporal worker deployment version compute-config modal-function create \
--deployment-name my-app \
--build-id build-1 \
--app-name temporal-worker \
--function-name temporal_worker \
--environment "$MODAL_ENVIRONMENT" \
--workspace-id "$MODAL_WORKSPACE_ID" \
--app-version "$MODAL_APP_VERSION"

The deployment name and Build ID must match the values in worker.py. The app, function, environment, and app version must identify the code you deployed to Modal. Pinning the app version prevents a later app deployment from changing the code used by this Worker Deployment Version.

For an existing version, use modal-function update instead of create. Provider details are replaced as a whole: pass all provider fields, including the workspace identifier and app version, when changing them. Updating provider details also resets scaler settings that are not supplied. See the CLI reference.

Inspect the saved configuration:

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

Set the version as Current​

Creating a version with compute configuration requests an initial Modal invocation to register the Worker's Task Queues. You do not need to start a Workflow or manually invoke the function for this registration.

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

Verify automatic invocation​

After the initial Worker invocation exits, confirm that no Workers are polling the example Task Queue. Start a Workflow to check that Temporal requests a new Modal invocation:

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 Workflow should complete with "Hello, World!". In Modal, check the function's invocation history and logs for a corresponding Worker invocation. In Temporal, verify the Task Queue and Worker Deployment Version that processed it. Starting a Workflow submits work to Temporal; you do not need to invoke Modal separately for every Workflow.

A connected provider status alone does not prove the deployed Worker contains your Workflow and Activity definitions. Verify both invocation and successful Task processing. The Worker can keep polling after this Workflow completes, until its invocation budget ends.

Update or remove the deployment​

For a new Worker build, change the Build ID in worker.py, deploy the app, and record its new numeric app version. Create a new Worker Deployment Version pointing to that app version, then use Worker Versioning to roll it out. Keep older app versions available while pinned Workflow Executions still depend on them.

Before deleting the Modal app or its credentials, remove its compute configuration in Temporal after dependent Workflows have finished or moved to another version. Otherwise, Temporal can continue requesting invocations of a function that no longer exists. Delete or revoke the Modal resources only after Temporal no longer needs them.