# Move a load balancer to another network

Move a network load balancer to another VPC, into one, or out of its VPC, and see beforehand what the move interrupts.

Replace _3d0c5a1e-8f4b-4c2a-9e7d-5b6a1f2c3d4e_ with your load balancer's id and
_6f1c2b8e-4a5d-4e7f-9b0c-1d2e3f4a5b6c_ with the id of the VPC it moves to.

A network load balancer (`lb11`, `lb21`, `lb31`) keeps its public addresses, listeners, services
and targets when it moves. What changes is the VPC it reaches targets in, and its address there.

## Prerequisites

- Permission for `lb:ModifyLoadBalancerNetwork` (a destructive action: targets it reaches by
  private address are interrupted for a moment).
- In the console, also `lb:DescribeLoadBalancers` to open the load balancer's page.
- The load balancer is a network load balancer or a private dedicated load balancer, in status
  `active`. Application load balancers and public dedicated load balancers are in no VPC and have
  nothing to move. One another service owns (a Kubernetes Service, a database endpoint) moves with
  its owner.
- Every target it reaches by private address (`usePrivateIp`) is an instance in the VPC it moves
  to. Otherwise the move is refused with `409`, naming each target. Move those instances first,
  switch those targets to their public address, or remove them.
- For a private dedicated load balancer, every target is in the VPC it moves to: an instance in
  that VPC, or an address in its range. It always stays in a VPC (`"vpcId": null` is refused with
  `400`), and that VPC has fewer than 5 private load balancers and already has a network (it gets
  one with its first instance or database).
- The VPC is in the load balancer's region and `available`.

## What the move interrupts

A load balancer is in one network at a time, so it leaves its VPC before it joins the new one,
then adds its targets reached by private address again.

| Move                     | Targets reached by private address                                    | Public addresses and public targets |
| ------------------------ | --------------------------------------------------------------------- | ----------------------------------- |
| From one VPC to another  | unreachable for about half a minute, connections to them drop         | not affected                        |
| Into a VPC from none     | none exist yet                                                        | not affected                        |
| Out of its VPC (to none) | not allowed: the move is refused while it reaches any target that way | not affected                        |

While private targets are unreachable, their health checks fail, and traffic returns once they
pass again for the healthy threshold. With no public target, the listeners answer with errors
until then. The load balancer's address in the VPC changes: a firewall rule or allow-list that
names the old address needs the new one.

A private dedicated load balancer switches over: it stops serving in its old VPC at once and
serves in the new one once that VPC's load balancer nodes have it, within about 2 minutes, or
typically several minutes when the VPC has no private load balancer yet and its nodes start
first. Open connections drop. Its name stays the same and answers its two new addresses; clients
that connect by address need the new ones. To undo the move, move it back.

## Move

**Console**
1. Open the load balancer.
2. In **Network**, choose **Change network**.
3. For **VPC**, choose the network, or **No network** to leave its VPC.
4. Read **What happens**: the interruption, the effects and the targets it reaches privately.
5. Choose **Move load balancer**. The section says the move is running; **Network changes** lists
   each step.

**CLI**
```sh
dcloud lb load-balancers network-move-plan 3d0c5a1e-8f4b-4c2a-9e7d-5b6a1f2c3d4e --vpc-id 6f1c2b8e-4a5d-4e7f-9b0c-1d2e3f4a5b6c
dcloud lb load-balancers change-network 3d0c5a1e-8f4b-4c2a-9e7d-5b6a1f2c3d4e --vpc-id 6f1c2b8e-4a5d-4e7f-9b0c-1d2e3f4a5b6c
dcloud lb load-balancers events 3d0c5a1e-8f4b-4c2a-9e7d-5b6a1f2c3d4e
```

**API**
```http
GET /lb/api/load-balancers/3d0c5a1e-8f4b-4c2a-9e7d-5b6a1f2c3d4e/network-move-plan?vpcId=6f1c2b8e-4a5d-4e7f-9b0c-1d2e3f4a5b6c
```

```http
POST /lb/api/load-balancers/3d0c5a1e-8f4b-4c2a-9e7d-5b6a1f2c3d4e/actions/change-network
{ "vpcId": "6f1c2b8e-4a5d-4e7f-9b0c-1d2e3f4a5b6c" }
```

The move answers `202` with the plan, and the load balancer's `vpcId` names the new VPC at once;
`networkMove` is set until the move ends. Send `"vpcId": null` to leave its VPC (the plan takes
`vpcId=none`). A load balancer already in that VPC answers `200` and nothing changes.

**Terraform**
Change `vpc_id` on the `dcloud_lb` and run `terraform apply`. The load balancer is moved in place,
not replaced. When instances it reaches by private address move in the same apply, make the load
balancer `depends_on` them, so they are in the new VPC first.

## Result

**Network changes**, and `GET …/events`, show each step with how long it took: "Left default
(10.0.0.5) in 5,1 s", "Joined app-net at 10.20.0.3 in 7,9 s" and "Moved from default to app-net,
13,4 s after it was asked for". If joining fails, it goes back to its old network, and the reason
is in **Network** and in `reconcileError`. A private dedicated load balancer is `provisioning`
until it serves in the new VPC, then "Moved from … to …" is listed.

## Related

- [Add or remove a target](add-or-remove-a-target.md) · [Load balancer types](load-balancer-types.md)
- `lb.load-balancers.change-network` · `dcloud lb load-balancers change-network` · `dcloud_lb`
