Skip to content

cloud: add terraform migrate cluster resource doc #20659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TOC-tidb-cloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@
- [Use Backup Resource](/tidb-cloud/terraform-use-backup-resource.md)
- [Use Restore Resource](/tidb-cloud/terraform-use-restore-resource.md)
- [Use Import Resource](/tidb-cloud/terraform-use-import-resource.md)
- [Migrate Cluster Resource](/tidb-cloud/terraform-migrate-cluster-resource.md)
- [Vercel](/tidb-cloud/integrate-tidbcloud-with-vercel.md)
- [Zapier](/tidb-cloud/integrate-tidbcloud-with-zapier.md)
- Reference
Expand Down
87 changes: 87 additions & 0 deletions tidb-cloud/terraform-migrate-cluster-resource.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: Migrate Cluster Resource to Serverless or Dedicated Cluster Resource
summary: Learn how to migrate a cluster resource to a serverless or dedicated cluster resource.
---

# Migrate Cluster Resource to Serverless or Dedicated Cluster Resource

Starting from TiDB Cloud Terraform Provider v0.4.0, the `tidbcloud_cluster` resource is replaced by two new resources: `tidbcloud_serverless_cluster` and `tidbcloud_dedicated_cluster`. If you are using TiDB Cloud Terraform Provider v0.4.0 or a later version, you can follow this document to migrate your `tidbcloud_cluster` resource to the `tidbcloud_serverless_cluster` or `tidbcloud_dedicated_cluster` resource.

> **Tip:**
>
> The steps in this document use the configuration generation feature of Terraform to simplify the migration process by automatically recreating the `.tf` configuration for your cluster resource. To learn more about it, see [Generating configuration](https://developer.hashicorp.com/terraform/language/import/generating-configuration) in Terraform documentation.

## Prerequisites

- Upgrade to [TiDB Cloud Terraform Provider v0.4.0 or later](https://registry.terraform.io/providers/tidbcloud/tidbcloud/latest)

## Step 1. Identify the `tidbcloud_cluster` resource to migrate

1. List all `tidbcloud_cluster` resources:

```shell
terraform state list | grep "tidbcloud_cluster"
```

2. Choose a target cluster resource to migrate and get its cluster `id` for later use:

```shell
terraform state show ${your_target_cluster_resource} | grep ' id '
```

## Step 2. Remove the existing resource from the Terraform state

Remove your target cluster resource from the Terraform state:

```shell
terraform state rm ${your_target_cluster_resource}
```

## Step 3. Delete the configuration of your target cluster resource

In your `.tf` file, find the configuration of your target cluster resource and delete the corresponding code.

## Step 4. Add an import block for the new serverless or dedicated cluster resource

- If your target cluster is TiDB Cloud Serverless, add the following import block to your `.tf` file, replace `example` with a desired resource name, and replace `${id}` with the cluster ID you get from [Step 1](#step-1-identify-the-tidbcloud_cluster-resource-to-migrate):

```
# TiDB Cloud Serverless
import {
to = tidbcloud_serverless_cluster.example
id = "${id}"
}
```

- If your target cluster is TiDB Cloud Dedicated, add the following import block to your `.tf` file, replace `example` with a desired resource name, and replace `${id}` with the cluster ID you get from [Step 1](#step-1-identify-the-tidbcloud_cluster-resource-to-migrate):

```
# TiDB Cloud Dedicated
import {
to = tidbcloud_dedicated_cluster.example
id = "${id}"
}
```

## Step 5. Generate the new configuration file

Generate the new configuration file for the new serverless or dedicated cluster resource according to the import block:

```shell
terraform plan -generate-config-out=generated.tf
```

Do not specify an existing `.tf` file name in the preceding command. Otherwise, Terraform will return an error.

## Step 6. Review and apply the generated configuration

Review the generated configuration file to ensure it meets your needs. Optionally, you can move the contents of this file to your preferred location.

Then, run `terraform apply` to import your infrastructure. After applying, the example output is as follows:

```shell
tidbcloud_serverless_cluster.example: Importing...
tidbcloud_serverless_cluster.example: Import complete

Apply complete! Resources: 1 imported, 0 added, 0 changed, 0 destroyed.
```