# Use an elastic IP

Hold a public IPv4 address independently of any machine, attach it to an instance, move it, and release it.

Replace the ids with your own. On your organization's own hosts an elastic IP works differently;
see [Instances on your own hosts](own-hosts.md).

**Important:**
An elastic IP is metered every hour it is allocated, attached or not, until you release it, as an
hourly share of its monthly price. Where elastic IPs are not charged yet, the console shows the
cost as "metered, not charged yet"; you are told before charging starts.

## Prerequisites

- In the console, also `ec2:DescribeInstances` to open the instance's page (without it the page is
  not found).
- Permission for `ec2:AllocateAddress`, `ec2:AssociateAddress`, `ec2:DisassociateAddress` and
  `ec2:ReleaseAddress`, as the step needs.
- In the console, also `ec2:DescribeAddresses` to open **Elastic IPs** (without it the page says
  "Access denied").
- Room in the elastic IP quota.
- To attach the address in the console: an instance that is running or stopped, in the address's
  region.

## Allocate an address

**Console**
1. Open **Compute**, then **Elastic IPs**, and choose **Create Elastic IP**.
2. In **Allocate Elastic IP**, for **Region**, choose the region.
3. Choose **Allocate**.

**CLI**
```sh
dcloud ec2 elastic-ips allocate --region-id de1 --tags project=storefront
```

**API**
```http
POST /ec2/api/elastic-ips
{ "regionId": "de1", "tags": { "project": "storefront" } }
```

**Terraform**
```hcl
resource "dcloud_eip" "web" {
  tags = { project = "storefront" }
}
```

## Attach it to an instance

**Console**
1. Open the instance, then choose **Networking**.
2. Under **Elastic IP**, choose **Associate**.
3. In **Associate Elastic IP**, for **Elastic IP**, choose the address (or allocate one there), then
   choose **Associate**.

Or, on **Elastic IPs**, choose **Associate with instance** in the address's row menu.

**CLI**
```sh
dcloud ec2 elastic-ips associate eip-uuid --instance-id i-3f9a2c07
```

**API**
```http
POST /ec2/api/elastic-ips/0ea56c91-0000-4000-8000-000000000004/associate
{ "instanceId": "i-3f9a2c07" }
```

**Terraform**
```hcl
resource "dcloud_eip_association" "web" {
  elastic_ip_id = dcloud_eip.web.id
  instance_id   = dcloud_instance.web.id
}
```

An address that is already associated is refused with `400` "IP is already associated.
Disassociate it first"; an instance whose machine does not exist yet with `409` "Instance has no
backing server yet". An address with no tags takes the instance's tags when it is associated; tags
it already has are never overwritten.

Instances launched since 17 November 2026 bind the address themselves within 20 seconds. On an older
instance the address does not answer until you run the commands the address's details show under
**On the machine** (Ubuntu or Debian with netplan), once, as root. The details say **Active on the
machine** once the machine has bound it.

## Move or detach it

**Console**
On **Elastic IPs**, choose **Disassociate** in the address's row menu, then choose
**Disassociate** in the dialog; or, on the instance's **Networking** tab, under **Elastic IP**,
choose **Disassociate** (it takes effect at once, without a dialog). Then associate it with the
other instance.

**CLI**
```sh
dcloud ec2 elastic-ips disassociate eip-uuid
```

**API**
```http
POST /ec2/api/elastic-ips/0ea56c91-0000-4000-8000-000000000004/disassociate
```

**Terraform**
Change `instance_id` on the `dcloud_eip_association` (Terraform replaces the association), or remove
it.

The address stays allocated and metered. Disassociating an address that is not attached is refused
with `400` "IP is not associated with any instance". Terminating the instance disassociates it as
well.

## Release it

**Console**
1. On **Elastic IPs**, choose the address to open its side panel.
2. Under **Release this address**, choose **Release address**.
3. Enter the address and choose **Release**.

**CLI**
```sh
dcloud ec2 elastic-ips release eip-uuid
```

**API**
```http
DELETE /ec2/api/elastic-ips/0ea56c91-0000-4000-8000-000000000004
```

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

Releasing an attached address is refused with `400` "Disassociate the IP from its instance before
releasing". A released address returns to the pool and cannot be reclaimed.

## Related

- [Networks and addresses](networks-and-addresses.md)
- `ec2.elastic-ips.allocate` · `dcloud ec2 elastic-ips release` · `dcloud_eip` · `dcloud_eip_association`
