# Multiple tenants

Source: https://docs.autessa.com/docs/cdk/tenants

> Deploy one Autessa CDK project to dev, prod and many customer tenants, each with its own deploy key and variables.

# Multiple tenants

(Preview)

The deploy API and the Autessa CDK are in preview and not yet released. Names and shapes may change before they are generally available.

A tenant is one Autessa company. You can deploy the same project to as many tenants as you like: a dev and prod pair, or one per customer.

## When to use it

- **Dev and prod.** Try a change in dev, then ship the same change to prod.
- **One setup per customer.** You build a product on Autessa and each customer gets their own company.
- **Regions or brands.** The same app with different settings.

## Stages: one tenant each

Each stage in `autessa.stages.yaml` points at one tenant. Give each stage its own deploy key and its own variables.

```yaml
stages:
  - name: dev
    tenant: Ab12Cd34Ef
    deployKeyEnv: AUTESSA_DEPLOY_KEY_DEV
    variables: { greeting: "Hi from dev" }
  - name: prod
    tenant: Gh56Ij78Kl
    deployKeyEnv: AUTESSA_DEPLOY_KEY_PROD
    variables: { greeting: "Hi, thanks for calling" }
```

## Fan-out: many tenants at once

Add a `fanout` group to deploy one project to a list of tenants. A group can also set `variables` and `permissions`, shared by all its tenants.

```yaml
fanout:
  - name: customers
    tenants: [Mn90Op12Qr, St34Uv56Wx, Yz78Ab90Cd]
    deployKeyEnv: AUTESSA_DEPLOY_KEY_FANOUT_{TENANT}
```

The deploy key for each tenant comes from its own environment variable, named by replacing `{TENANT}`:

- **TypeScript and the deploy action:** the tenant ID as written, for example `AUTESSA_DEPLOY_KEY_FANOUT_Mn90Op12Qr`.
- **Python and Java:** the tenant ID in upper case, for example `AUTESSA_DEPLOY_KEY_FANOUT_MN90OP12QR`.

Run a command against the whole group:

```bash
# Python
autessa-cdk diff --stage customers

# TypeScript (add --tenant Mn90Op12Qr to run against one tenant)
npx autessa-cdk diff --fanout customers

# Java (Maven)
mvn autessa-cdk:diff -Dautessa.stage=customers
```

Tenants run one at a time, in the order listed. The diff shows each tenant on its own, and each tenant gets its own `deploymentId`. With TypeScript and Java, a deploy stops at the first tenant that fails, and later tenants are not deployed. Fix the problem and run the deploy again: tenants that are already up to date are not changed.

To roll back a single tenant, use `rollback --fanout customers --tenant <id>` with the TypeScript CLI. Python and Java roll back every tenant in the group.

## Good practice

- **One deploy key per tenant.** Never share a key across tenants. Revoking one key should affect one tenant.
- **Keep secrets per tenant.** Use `SecretRef` in code and set the value in each tenant.
- **Use variables, not branches.** Put per-tenant differences in `variables`, so every tenant runs the same code.
- **Diff before you fan out.** Read the `WIDENS` lines for every tenant.

## Example

A project with dev and prod stages: [autessa-as-code-tenant](https://github.com/Autessa-Inc/autessa-examples/tree/main/examples/autessa-as-code-tenant).

## Related

- [Project files](/docs/cdk/project.md)
- [Commands](/docs/cdk/commands.md)
- [Deploy action](/docs/cdk/deploy-action.md)
- [Organization and tenants](/docs/guide/platform/companies.md)

