# Use a wildcard domain for a project

Give every app of a project a name under one domain of yours, such as orders.shop.example.com, with one certificate for all of them.

With a wildcard domain such as `shop.example.com` on a project, every app of the project also
answers at `<app>.shop.example.com`, where `<app>` is the app's short name (its slug). New apps of
the project, and apps moved into it, get their name at once. One certificate for
`*.shop.example.com` serves all of them and is renewed 30 days before it expires.

Each project has at most one wildcard domain. For one domain for the whole organization, set it on
the Default project and keep the apps there. Two projects cannot share a domain, and one project's
domain cannot be under another's: use `shop.example.com` and `blog.example.com`, not
`example.com` and `shop.example.com`.

## Prerequisites

- The domain in one of the organization's hosted zones, delegated to the platform's nameservers:
  the zone `example.com`, or a zone for `shop.example.com` itself. See
  [Create a hosted zone](/docs/dns/guide/create-a-hosted-zone) and
  [Delegate a domain](/docs/dns/guide/delegate-a-domain).
- Permission for `deploy:PutProjectDomain` to set it and `deploy:DeleteProjectDomain` to remove
  it; `deploy:DescribeProjectDomain` to see it. Policies can limit these to a project with the
  condition key `ews:ResourceProject`.
- In the console, also `deploy:DescribeApp` to open **Apps**.

## Set the domain

The project is the one the request names: the project picked at the top of the console, or
`--project` in the CLI (the `X-EWS-Project` header in the API). Without one, it is the Default
project.

**Console**
1. Pick the project at the top of the console.
2. Open **Apps**.
3. Under **Wildcard domain**, for **Domain**, enter the domain, for example `shop.example.com`.
4. Choose **Set domain**. The domain, the certificate and one line per app of the project appear.

To use another domain, choose **Change**. With **All projects** picked, **Wildcard domains** lists
every project's domain.

**CLI**
```sh
dcloud deploy project-domain put --domain shop.example.com --project shop
dcloud deploy project-domain get --project shop
dcloud deploy project-domain list
```

**API**
```http
PUT /deploy/api/project-domain
X-EWS-Project: shop

{
  "domain": "shop.example.com"
}
```

**Terraform**
```hcl
resource "dcloud_project_domain" "shop" {
  project = "shop"
  domain  = "shop.example.com"
}
```

The request is refused with `400` when the domain is not in one of your hosted zones, and with
`409` when another project uses it, or a domain under it or over it.

## What the platform does

- It writes an `ALIAS` record `<app>.<domain>` for every app of the project into the hosted zone.
  The record follows the app: its machine, or its pool host for pooled hosting.
- It never writes over a record that is already there. An app whose name is taken shows
  **Name taken** in the console and `taken` in the API; remove your record and the next pass, or a
  new **Set domain**, writes the app's.
- It issues one certificate for `*.<domain>` within a minute or two, answering the DNS challenge
  in your zone, and gives it to every app's machine and pool host. Until it is issued the
  certificate shows **Issuing**.
- When an app moves to another project, its name under the old project's domain is removed and,
  if the new project has a domain, its name there is written. There is no redirect from the old
  name.

Each app's line shows where it stands:

| State              | Meaning                                                                    |
| ------------------ | -------------------------------------------------------------------------- |
| **Answering**      | The record points at the app                                               |
| **Writing record** | The record is written on the next pass                                     |
| **Name taken**     | The name has a record of yours; it is left alone                           |
| **Not served**     | The app runs behind a load balancer, which is not served at the domain yet |

Preview environments are not given a name under the domain.

## Remove the domain

In the console, pick the project, then under **Wildcard domain** choose **Remove**, then
**Remove**. With the CLI:

```sh
dcloud deploy project-domain delete --project shop
```

The records the platform wrote and the certificate are removed. A name that is also one of the
app's own custom domains stays. A project with a wildcard domain cannot be deleted until the
domain is removed.

## Result

Every app of the project answers at `<app>.<domain>` over HTTPS with the project's certificate.

## Related

- [Add a custom domain](add-a-custom-domain.md)
- `deploy.project-domain.put` · `dcloud deploy project-domain put` · `dcloud_project_domain`
