# Add a NAT gateway

Give the instances of a network that have no public IPv4 outbound IPv4 internet access through one shared address.

An instance launched with `enablePublicIpv4: false` keeps a public IPv6 address but has no IPv4
route to the internet. A NAT gateway gives the machines of one network outbound IPv4 through one
address, and lets nothing in. It is one small machine run by the platform, at a fixed address in
the network, with the network route `0.0.0.0/0` to it. Replace the ids with your own.

**Important:**
The gateway is a machine, metered for every started hour until you delete it. It is not redundant:
if its machine is lost, it turns `failed`; delete it and create it again.

## Prerequisites

- Permission for `ec2:CreateNatGateway`; deleting needs `ec2:DeleteNatGateway`, reading
  `ec2:DescribeNatGateways`.
- In the console, also `ec2:DescribeVpcs` to open the network's page (without it the page is not
  found).
- One gateway per network: a second is refused with `409` "The VPC already has NAT gateway …; one
  per VPC". Room in the NAT gateways quota.

## Create it

**Console**
1. Open **Compute**, then **Networks**, and open the network.
2. Under **NAT gateway**, choose **Create NAT gateway**.
3. In **Create NAT gateway?**, check the price, then choose **Create NAT gateway**.

**CLI**
```sh
dcloud ec2 nat-gateways create --vpc-id vpc-uuid
dcloud ec2 nat-gateways get nat-0a1b2c3d
```

**API**
```http
POST /ec2/api/nat-gateways
{ "vpcId": "0ea56c91-0000-4000-8000-000000000001" }
```

**Terraform**
```hcl
resource "dcloud_nat_gateway" "prod" {
  vpc_id = dcloud_vpc.prod.id
}
```

## Result

The answer is `202` with the gateway in status `creating`; it turns `available` once its machine
and route are in place. `status` is `creating`, `available`, `failed` or `deleting`;
`publicAddress` is what the internet sees your machines come from, `privateAddress` its address in
the network. The console shows them as **Egress address** and **Address in the network**.

Instances launched without a public IPv4 after the gateway is `available` route through it on their
own. An instance that already existed needs the route once, as root; the console shows the command
with your network's gateway address under **Route an existing instance through it (once)**. For a
`10.0.0.0/16` network:

```sh
sudo ip route replace default via 10.0.0.1 dev $(ip -o route get 10.0.0.1 | awk '{print $3}') onlink
```

A problem the platform finds with the gateway (a missing route, for example) shows as **The
gateway reported a problem.** on the network page, and as `reconcileError` in the API.

## Delete it

**Console**
1. Under **NAT gateway**, choose **Delete NAT gateway**.
2. In **Delete the NAT gateway**, choose **Delete NAT gateway**.

**CLI**
```sh
dcloud ec2 nat-gateways delete nat-0a1b2c3d
```

**API**
```http
DELETE /ec2/api/nat-gateways/nat-0a1b2c3d
```

**Terraform**
Remove the `dcloud_nat_gateway` resource and run `terraform apply`.

The answer is `202` with status `deleting`; the route goes first, then the machine. Machines
without a public IPv4 lose IPv4 internet access. A network with a NAT gateway cannot be deleted
until the gateway is.

## Related

- [Create or delete a network](create-a-network.md)
- `ec2.nat-gateways.create` · `dcloud ec2 nat-gateways delete` · `dcloud_nat_gateway`
