# Migrating Cluster Connection

ScyllaDB Cloud allows you to connect to a cluster in different ways:

* [AWS Transit Gateway (TGW) VPC Attachment Connection](https://cloud.docs.scylladb.com/stable/cluster-connections/aws-tgw-vpc-attachment.md)
* [AWS Virtual Private Network (VPC) Peering](https://cloud.docs.scylladb.com/stable/cluster-connections/aws-vpc-peering.md)
* [GCP Virtual Private Network (VPC) Peering](https://cloud.docs.scylladb.com/stable/cluster-connections/gcp-vpc-peering.md)

You can migrate from one connection to another, in some cases, without network interruption.

This guide provides general steps to migrate from one connection to another and a particular example of how to migrate from *AWS Virtual Private Network (VPC) Peering* to *AWS Transit Gateway (TGW) VPC Attachment Connection*.

## Cluster Connection Statuses

There are many connection statuses, but in regard of migration, the following statuses are relevant:

* **ACTIVE** - Connection is deployed on the cloud and actively used in making routing decisions. Traffic is going to be routed through it unless there are common CIDRs with other **ACTIVE** connections.
* **INACTIVE** - Connection is deployed on the cloud but it is not participating in routing decisions. Traffic is never routed through it.

## Cluster Connection Routing

Cluster connections are prioritized by creation date, with older connections having priority.
This priority plays out only when two connections have common CIDRs; for each common CIDR, the oldest **ACTIVE** cluster connection is picked as the routing endpoint while other connections are ignored.

## Prerequisites

Before proceeding with migration, make sure that you have a personal access token for your ScyllaDB Cloud account.
See [Create a Personal Token for Authentication](https://cloud.docs.scylladb.com/stable/api-docs/create-api-token.md) for instructions on how to obtain it.

## General Connection Migration Guide (General Steps)

### Migrate to a New Cluster Connection

1. Create **NEW** cluster connection. It is assumed that at least some of CIDRs exist on both **OLD** and **NEW** connections.
2. At this point, traffic from both ends goes via **OLD** cluster connection.
3. Direct traffic from your network to the cluster via **NEW** cluster connection. It is done in your **AWS Account** and VPC.
4. At this point, traffic from your network goes via **NEW** cluster connection, while traffic from the cluster to your network goes through **OLD** cluster connection.
5. Set **OLD** cluster connection to **INACTIVE**.
6. ScyllaDB Cloud will direct cluster traffic through **NEW** cluster connection.
7. Now traffic from both ends goes through **NEW** cluster connection.

### Migrate Back to the Old Cluster Connection

1. Direct traffic from your network to the cluster via **OLD** cluster connection. It is done in your **AWS Account** and VPC.
2. At this point, traffic from your network goes via **OLD** cluster connection, while traffic from the cluster to your network goes through **OLD** cluster connection.
3. Set **OLD** cluster connection to **ACTIVE**; optionally you can set **NEW** cluster connection to **INACTIVE**.
4. ScyllaDB Cloud will direct cluster traffic via **OLD** cluster connection. Now traffic from both ends goes through **OLD** cluster connection.

## Example: Migration from AWS VPC Peering to AWS TGW VPC Attachment

The following example shows you how to migrate from AWS VPC Peering to AWS TGW VPC Attachment
(and back) via ScyllaDB Cloud API using your account ID. You can get the account ID using
the `https://api.cloud.scylladb.com/account/default` endpoint;
see [Gives account details tied to authorized user](https://cloud.docs.scylladb.com/stable/api.html#tag/Account/operation/getCloudAccount).

1. Create **AWS TGW VPC Attachment** cluster connection, following the steps from
   [Setup AWS TGW VPC Attachment Connection](https://cloud.docs.scylladb.com/stable/cluster-connections/aws-tgw-vpc-attachment.md#setup-aws-tgw-vpc-attachment-connection).

   By [Routing traffic from your application to the cluster via TGW](https://cloud.docs.scylladb.com/stable/cluster-connections/aws-tgw-vpc-attachment.md#aws-tgw-route-traffic),
   you are going to direct traffic from your network to the cluster via the new cluster connection.
2. Set **AWS VPC Peering** cluster connection to **INACTIVE**.
   ```default
   curl --request PATCH \
   --url https://api.cloud.scylladb.com/account/<ACCOUNT-ID>/cluster/<CLUSTER-ID>/network/vpc/peer/<VPC-PEERING-PEERING-CONNECTION-ID> \
   --header "Authorization: Bearer <TOKEN>" \
   --header "Content-Type: application/json" \
   -d '{"status": "INACTIVE"}'
   ```

### Migration Back from AWS TGW VPC Attachment to AWS VPC Peering

1. Set **AWS VPC Peering** cluster connection to **ACTIVE**.
   ```default
   curl --request PATCH \
   --url https://api.cloud.scylladb.com/account/<ACCOUNT-ID>/cluster/<CLUSTER-ID>/network/vpc/peer/<VPC-PEERING-PEERING-CONNECTION-ID> \
   --header "Authorization: Bearer <TOKEN>" \
   --header "Content-Type: application/json" \
   -d '{"status": "ACTIVE"}'
   ```
2. Set **AWS TGW VPC Attachment** to **INACTIVE**.
   ```default
   curl --request PATCH \
   --url https://api.cloud.scylladb.com/account/<ACCOUNT-ID>/cluster/<CLUSTER-ID>/network/vpc/connection/<TGW-VPC-ATTACHMENT-CONNECTION-ID> \
   --header "Authorization: Bearer <TOKEN>" \
   --header "Content-Type: application/json" \
   -d '{"status": "INACTIVE"}'
   ```

   It will direct traffic from the cluster via **AWS VPC Peering**, while traffic from the cluster to your network through **AWS TGW VPC Attachment** cluster connection.
3. Direct traffic from your network to the cluster via **AWS VPC Peering**:
   > 1. Open AWS Cloud Console at [VPC> Route tables](https://console.aws.amazon.com/vpcconsole/home#RouteTables:).
   > 2. Find and select the main route table for your VPC (the one that has **Yes** in the **Main** column).
   > 3. Click **Actions> Edit routes**.
   > 4. Find the route to [Cluster Datacenter CIDR](https://cloud.docs.scylladb.com/stable/cluster-connections/glossary-cluster-connection.md#term-Cluster-Datacenter-CIDR).
   >    In the **Destination** column, select **Peering Connection** for **Target** and select your VPC peering ID
   >    from the drop-down list below it.
   >    ![image](cluster-connections/images/create-route-traffic-to-vpc-peering-1.png)
   > 5. Click **Save changes**.
