# Restrict outbound traffic

Let an app connect only to the host names and networks you list, instead of anywhere.

An app can connect anywhere by default. An egress allow-list turns that around: the app reaches
only what the list names, plus what its own bindings need. It applies to the app's machines and to
pooled hosting alike, and to the running app within a minute, without a redeploy. Replace
app-id with the app's id.

## Prerequisites

- Permission for `deploy:UpdateApp` on the app. Without it the console shows the section read
  only.
- In the console, also `deploy:DescribeApp` to open the app's page.

## Set the allow-list

**Console**
1. Open the app, then choose the **Configuration** tab.
2. Under **Outbound traffic**, select **Only the destinations listed**.
3. For **Allowed destinations**, enter one destination per line, for example `api.stripe.com`,
   `.github.com` and `192.0.2.0/24`.
4. Choose **Save changes**. **Saved** appears beside the button.

To allow everything again, select **Any destination** and choose **Save changes**.

**CLI**
```sh
dcloud deploy apps update app-id \
  --egress-allow '["api.stripe.com", ".github.com", "192.0.2.0/24"]'
```

`--egress-allow null` allows everything again.

**API**
```http
PATCH /deploy/api/apps/3f1c2b4a-5d6e-4f70-8a91-b2c3d4e5f601
{
  "egressAllow": ["api.stripe.com", ".github.com", "192.0.2.0/24"]
}
```

`"egressAllow": null` allows everything again. The app's `egressAllow` and `egressImplicit` fields
show the list and what is always reachable.

**Terraform**
```hcl
resource "dcloud_app" "web" {
  name         = "my-app"
  builder      = "image"
  image_ref    = "org-example1234.ecr.example.com/orders:1.4.2"
  egress_allow = ["api.stripe.com", ".github.com", "192.0.2.0/24"]
}
```

An entry must be one of the four kinds in the table below. The update is refused with `400` for any
other entry, and for more than 100 entries.

## What an entry matches

| Entry             | Allows                                    |
| ----------------- | ----------------------------------------- |
| `api.example.com` | That name only, not `www.api.example.com` |
| `.example.com`    | `example.com` and every name under it     |
| `192.0.2.7`       | That address                              |
| `192.0.2.0/24`    | That network                              |

Every port is allowed for an entry. IPv6 is not reachable while a list is set. An empty list
allows nothing but the destinations that are always reachable.

## Always reachable

The hosts of the app's bindings (a bound database, cache, Kafka or RabbitMQ cluster, and its Error
Tracking project) stay reachable whatever the list says, so a list never cuts an app off from its
own database. The console shows them under **Always reachable**; the API returns them as
`egressImplicit`. They follow the bindings: a new binding is added on the app's next deployment.

A list only narrows what the app reaches. On pooled hosting private networks stay unreachable even
when a list names one.

## How it is enforced

Each machine, and each pool host for a pooled app, resolves the app's DNS queries itself. A name on
the list is resolved as usual, and the addresses in the answer become reachable for the app for
the answer's lifetime (at least one minute) plus five minutes. A name that is not on the list is
answered as not existing and is never sent on, so DNS cannot carry data out either. Each refused
name appears in the app's activity as `Outbound blocked: <name> is not on the app's egress
allow-list`, at most once a minute per name.

A connection to an address that is neither listed nor answered for a listed name is refused at
once. When you change the list, connections opened under the old list are closed; the app opens
them again under the new one.

Addresses that serve many names, such as a content delivery network's, can be reached through a
listed name and then used for another name at the same address. Connections made to an address
directly, without a DNS query, are refused but not listed in the activity.

## Result

The app reaches only the listed destinations and its bindings. Other connections fail at once, and
refused names show in the app's activity.

## Related

- [Bind a database](bind-a-database.md)
- [Security](security.md)
- `deploy.apps.update` · `dcloud deploy apps update` · `dcloud_app`
