Manage AnalyticDB for MySQL Resource Groups with Terraform

更新时间:
复制 MD 格式

Provision an AnalyticDB for MySQL Enterprise Edition cluster with Terraform and create three resource groups, one per workload pattern: Disable (fixed reservation), AutoScale (cluster-count scaling), and ServerlessAutoScale (ACU-level scaling).

When to use this guide

  • Manage clusters and resource groups as code, alongside the rest of your infrastructure.

  • Isolate workloads (steady reporting, bursty BI, ad-hoc analytics) into separate resource groups.

  • Reproduce the same layout across dev, staging, and production environments.

Estimated time: 30 minutes — about 5 minutes for the cluster and 3 minutes per resource group.

Before you begin

  • Create an Alibaba Cloud account and activate the AnalyticDB for MySQL service.

  • Create a RAM user and obtain its AccessKey ID and AccessKey Secret. Manage credentials through environment variables instead of hardcoding them.

  • Install Terraform 1.0 or later and configure the Alicloud Provider.

  • Grant the RAM user the following permissions:

    Permission Used for
    vpc:CreateVpc, vpc:DeleteVpc VPC lifecycle
    vpc:CreateVSwitch, vpc:DeleteVSwitch vSwitch lifecycle
    adb:CreateDBCluster, adb:DeleteDBCluster Cluster lifecycle
    adb:CreateDBResourceGroup, adb:ModifyDBResourceGroup, adb:DeleteDBResourceGroup Resource group lifecycle
    bss:PayOrder Pay-as-you-go billing
  • Export your credentials:

      export ALICLOUD_ACCESS_KEY="<your-access-key-id>"
      export ALICLOUD_SECRET_KEY="<your-access-key-secret>"
      export ALICLOUD_REGION="cn-hangzhou"

Resources used

This guide uses the following Terraform resources:

Step 1: Create the VPC and vSwitch

Create main.tf:

terraform {
  required_providers {
    alicloud = {
      source  = "aliyun/alicloud"
      version = "~> 1.230"
    }
  }
}

variable "name" {
  default = "tf-adb-rg-example"
}

provider "alicloud" {}

data "alicloud_adb_zones" "default" {}

resource "alicloud_vpc" "default" {
  vpc_name   = var.name
  cidr_block = "192.168.0.0/16"
}

resource "alicloud_vswitch" "default" {
  vswitch_name = var.name
  vpc_id       = alicloud_vpc.default.id
  cidr_block   = "192.168.1.0/24"
  zone_id      = data.alicloud_adb_zones.default.ids[0]
}

alicloud_adb_zones returns the zones that support AnalyticDB for MySQL — no manual selection needed.

Step 2: Create the Enterprise Edition cluster

Resource groups attach to a Enterprise Edition cluster. Enterprise Edition uses ACU (AnalyticDB Compute Unit) as the unit of capacity. Total cluster capacity = reserved_node_size × reserved_node_count.

resource "alicloud_adb_db_cluster_lake_version" "default" {
  db_cluster_version            = "5.0"
  vpc_id                        = alicloud_vpc.default.id
  vswitch_id                    = alicloud_vswitch.default.id
  zone_id                       = data.alicloud_adb_zones.default.ids[0]
  payment_type                  = "PayAsYouGo"
  product_form                  = "IntegrationForm"
  product_version               = "EnterpriseVersion"
  reserved_node_size            = "8ACU"
  reserved_node_count           = 6
  enable_default_resource_group = false
  db_cluster_description        = var.name
}
Argument Description Recommended value
product_form Cluster product form. IntegrationForm (integrated form).
product_version Cluster edition. Required only when product_form = "IntegrationForm". EnterpriseVersion (Enterprise Edition).
reserved_node_size Specification of one reserved node, in ACU. 8ACU.
reserved_node_count Number of reserved nodes. 6 (cluster total: 48 ACU).
enable_default_resource_group Whether to allocate the cluster's full ACU capacity to the default resource group user_default. false (so the default group does not consume the entire cluster capacity).

Steps 3: Create resource groups for each mode

Each mode targets a different workload pattern. Append any combination of the templates below to main.tf. The total ACU of all resource groups must not exceed the cluster total capacity; otherwise creation fails with free compute resource is not sufficient.

Disable mode — steady workloads

Holds a fixed amount of ACU and does not scale. Use for predictable, latency-sensitive workloads such as scheduled BI dashboards.

resource "alicloud_adb_resource_group" "disable_mode" {
  db_cluster_id        = alicloud_adb_db_cluster_lake_version.default.id
  group_name           = "TF_DISABLE"
  group_type           = "interactive"
  cluster_mode         = "Disable"
  min_compute_resource = "16ACU"
  max_compute_resource = "16ACU"
}

Setting min_compute_resource and max_compute_resource to the same value pins the group at that capacity.

AutoScale mode — variable concurrency

For details, see AutoScale resource group.

resource "alicloud_adb_resource_group" "autoscale_mode" {
  db_cluster_id         = alicloud_adb_db_cluster_lake_version.default.id
  group_name            = "TF_AUTOSCALE"
  group_type            = "interactive"
  cluster_mode          = "AutoScale"
  min_cluster_count     = 1
  max_cluster_count     = 2
  cluster_size_resource = "16ACU"
}

Above: each sub-cluster is 16 ACU; the group scales between 1 and 2 sub-clusters, peaking at 32 ACU.

ServerlessAutoScale mode — sparse, unpredictable workloads

For details, see ServerlessAutoScale resource group.

Important

Requires cluster engine version 3.2.7.0 or later. Older clusters fail with FeatureUnsupported.serverlessXihe.

To view and update the minor version, go to the Configuration Information section on the Cluster Information page in the AnalyticDB for MySQL console.

resource "alicloud_adb_resource_group" "serverless_mode" {
  db_cluster_id        = alicloud_adb_db_cluster_lake_version.default.id
  group_name           = "TF_SERVERLESS"
  group_type           = "interactive"
  cluster_mode         = "ServerlessAutoScale"
  min_compute_resource = "0ACU"
  max_compute_resource = "32ACU"
}

Step 4: Apply and verify

terraform init        # Expect "Terraform has been successfully initialized!"
terraform plan        # Expect "Plan: 6 to add, 0 to change, 0 to destroy."
terraform apply -auto-approve

Total apply time: about 8-10 minutes — 4-5 minutes for the cluster, 2-3 minutes per resource group (created in parallel). On success the output ends with:

Apply complete! Resources: 6 added, 0 changed, 0 destroyed.

In the console, open the cluster's Resource Groups page and confirm all three groups are Running.

Step 6: Clean up

terraform destroy -auto-approve

Expect Destroy complete! Resources: 6 destroyed.. Enterprise Edition cluster deletion takes 3-5 minutes; destroy waits for full release before returning.

Complete example

terraform {
  required_providers {
    alicloud = {
      source  = "aliyun/alicloud"
      version = "~> 1.230"
    }
  }
}

variable "name" {
  default = "tf-adb-rg-example"
}

provider "alicloud" {}

data "alicloud_adb_zones" "default" {}

resource "alicloud_vpc" "default" {
  vpc_name   = var.name
  cidr_block = "192.168.0.0/16"
}

resource "alicloud_vswitch" "default" {
  vswitch_name = var.name
  vpc_id       = alicloud_vpc.default.id
  cidr_block   = "192.168.1.0/24"
  zone_id      = data.alicloud_adb_zones.default.ids[0]
}

resource "alicloud_adb_db_cluster_lake_version" "default" {
  db_cluster_version            = "5.0"
  vpc_id                        = alicloud_vpc.default.id
  vswitch_id                    = alicloud_vswitch.default.id
  zone_id                       = data.alicloud_adb_zones.default.ids[0]
  payment_type                  = "PayAsYouGo"
  product_form                  = "IntegrationForm"
  product_version               = "EnterpriseVersion"
  reserved_node_size            = "8ACU"
  reserved_node_count           = 6
  enable_default_resource_group = false
  db_cluster_description        = var.name
}

resource "alicloud_adb_resource_group" "disable_mode" {
  db_cluster_id        = alicloud_adb_db_cluster_lake_version.default.id
  group_name           = "TF_DISABLE"
  group_type           = "interactive"
  cluster_mode         = "Disable"
  min_compute_resource = "16ACU"
  max_compute_resource = "16ACU"
}

resource "alicloud_adb_resource_group" "autoscale_mode" {
  db_cluster_id         = alicloud_adb_db_cluster_lake_version.default.id
  group_name            = "TF_AUTOSCALE"
  group_type            = "interactive"
  cluster_mode          = "AutoScale"
  min_cluster_count     = 1
  max_cluster_count     = 2
  cluster_size_resource = "16ACU"
}

resource "alicloud_adb_resource_group" "serverless_mode" {
  db_cluster_id        = alicloud_adb_db_cluster_lake_version.default.id
  group_name           = "TF_SERVERLESS"
  group_type           = "interactive"
  cluster_mode         = "ServerlessAutoScale"
  min_compute_resource = "0ACU"
  max_compute_resource = "32ACU"
}

Import an existing resource group

Bring a console-created resource group under Terraform management instead of recreating it.

Syntax:

terraform import alicloud_adb_resource_group.<terraform-name> <db_cluster_id>:<group_name>

Example: import the resource group TF_DISABLE from cluster amv-bp1xxxxxx as the local Terraform resource disable_mode.

terraform import alicloud_adb_resource_group.disable_mode amv-bp1xxxxxx:TF_DISABLE

Related topics