Create an HBase cluster with Terraform

更新时间:
复制 MD 格式

This topic describes how a RAM user can use Terraform to create an ApsaraDB for Hbase cluster.

Note

The sample code in this topic is ready to run. You can run it directly. Run Now

Prerequisites

  • Because an Alibaba Cloud account has full permissions for all resources, compromised credentials pose a significant security risk. We recommend that you create a RAM user and an AccessKey for that user. For more information, see Create a RAM user and Create an AccessKey.

  • Grant the RAM user the AliyunHBaseFullAccess (to manage ApsaraDB for HBase) and AliyunVPCFullAccess (to manage Virtual Private Cloud) permissions. The following is a sample policy. For more information about how to grant permissions, see Manage RAM user permissions.

    {
        "Version": "1",
        "Statement": [
            {
                "Action": [
                    "vpc:CreateVpc",
                    "vpc:DescribeVpc",
                    "vpc:ModifyVpcAttribute",
                    "vpc:DeleteVpc",
                    "vpc:CreateVSwitch",
                    "vpc:DescribeVSwitch",
                    "vpc:ModifyVSwitchAttribute",
                    "vpc:DeleteVSwitch"
                ],
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Action": "hbase:*",
                "Resource": "*",
                "Effect": "Allow"
            }
        ]
    }
    
  • Prepare a Terraform runtime environment. You can use Terraform in one of the following ways:

Important

Ensure that your Terraform version is v0.12.28 or later. To check the version, run the terraform --version command.

Note

Some resources used in this tutorial incur charges. To avoid unnecessary costs, release these resources promptly when they are no longer needed.

Resources

Create an HBase cluster with Terraform

  1. Create a working directory and create a configuration file named main.tf in it. main.tf is the main Terraform file and defines the resources to be deployed.

    variable "region" {
      default = "cn-qingdao"
    }
    provider "alicloud" {
      region = var.region
    }
    data "alicloud_hbase_zones" "default" {}
    variable "instance_type" {
      default = "hbase.sn2.large"
    }
    variable "name" {
      default = "hbasetest"
    }
    # Create an alicloud_vpc resource.
    resource "alicloud_vpc" "vpc1" {
      vpc_name   = var.name
      cidr_block = "172.16.0.0/12"
    }
    # Create an alicloud_vswitch resource within the VPC and specify a zone.
    resource "alicloud_vswitch" "default" {
      vswitch_name = var.name
      cidr_block   = "172.16.20.0/24"
      vpc_id       = alicloud_vpc.vpc1.id
      zone_id      = data.alicloud_hbase_zones.default.zones[0].id
    }
    resource "alicloud_hbase_instance" "default" {
      # (Required) The name of the HBase instance. The name must be 2 to 128 characters in length and can contain Chinese characters, letters, digits, periods (.), underscores (_), and hyphens (-).
      name        = var.name
      #  (Optional, ForceNew) The zone in which to launch the HBase instance.
      zone_id                = data.alicloud_hbase_zones.default.zones[0].id
      # (Optional, ForceNew) The ID of the VSwitch. 
      vswitch_id             = alicloud_vswitch.default.id
      #  (Optional, ForceNew, Available from v1.185.0) The ID of the VPC.
      vpc_id                 = alicloud_vpc.vpc1.id
      #  (Optional, ForceNew) The engine of the instance. Valid values: "hbase", "hbaseue", "bds". "hbaseue" and "bds" are supported from v1.73.0. A single-node HBase instance requires `engine="hbase"` and `core_instance_quantity=1`.
      engine                 = "hbaseue"
      # (Required, ForceNew) The major engine version. Valid values for different engines: hbase (1.1, 2.0), hbaseue (2.0), bds (1.0).
      engine_version         = "2.0"
      # (Required) The instance type of the master nodes.
      master_instance_type   = var.instance_type
      # (Required) The instance type of the core nodes.
      core_instance_type     = var.instance_type
      # (Optional) The number of core nodes. Default value: 2. Valid values: 1 to 200. An instance with more than one core node is a cluster. An instance with one core node is a single-node instance.
      core_instance_quantity = "2"
      #  (Optional, ForceNew) The disk type of the core nodes. Valid values: cloud_ssd, cloud_essd_pl1, cloud_efficiency, local_hdd_pro, and local_ssd_pro. The `local_disk` has a fixed size. If `engine` is set to `bds`, this parameter is not required.
      core_disk_type         = "cloud_ssd"
      # (Optional) The storage capacity of each core node, in GB. This parameter is valid only when `engine` is "hbase" or "hbaseue". For the `bds` engine, `core_disk_size` is not required. Value ranges: cluster [400, 64000] in 40 GB increments; single-node [20, 500] in 1 GB increments.
      core_disk_size         = 400
      #  (Optional) The billing method. Valid values: PrePaid and PostPaid. Default value: PostPaid. You can change the billing method from PostPaid to PrePaid. From v1.115.0+, you can also change from PrePaid to PostPaid.
      pay_type               = "PostPaid"
      # (Optional) The cold storage capacity in GB. Valid values: 0 or 800 to 1,000,000 in 10 GB increments. A value of 0 indicates that cold storage is disabled. A value greater than 0 indicates that cold storage is enabled.
      cold_storage_size      = 0
      # (Optional, Available from v1.109.0) Specifies whether to delete the instance immediately. If true, the instance is deleted immediately. If false, it is deleted after a retention period.
      immediate_delete_flag  =  true
      # (Optional) The subscription duration in months. This parameter is valid only when `pay_type` is PrePaid. Valid values: 1 to 9, 12, 24, and 36. Values of 12, 24, and 36 correspond to 1, 2, and 3 years, respectively.
      duration               = 1
      # (Optional, Available from v1.73.0) The deletion protection switch. A value of true enables deletion protection. A value of false disables deletion protection. You must set this parameter to false to delete the instance.
      deletion_protection    = false
    }
  2. Run the following command to initialize the Terraform runtime environment:

    terraform init

    If the output indicates that Terraform has been successfully initialized, you can proceed to the next step.

    Initializing the backend...
    Initializing provider plugins...
    - Finding latest version of hashicorp/alicloud...
    - Installing hashicorp/alicloud v1.234.0...
    - Installed hashicorp/alicloud v1.234.0 (signed by HashiCorp)
    Terraform has created a lock file .terraform.lock.hcl to record the provider
    selections it made above. Include this file in your version control repository
    so that Terraform can guarantee to make the same selections by default when
    you run "terraform init" in the future.
    Terraform has been successfully initialized!
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. Run the following command to create an execution plan and preview the changes:

    terraform plan
  4. Run the following command to create the HBase cluster instance:

    terraform apply

    When prompted, enter yes and press Enter. Wait for the command to complete. The 'Apply complete!' message in the output indicates that the HBase cluster was created successfully.

    Plan: 3 to add, 0 to change, 0 to destroy.
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
      Enter a value: yes
    alicloud_vpc.vpc1: Creating...
    alicloud_vpc.vpc1: Creation complete after 6s [id=vpc-m5ecf5pojlcjkolx7u***]
    alicloud_vswitch.default: Creating...
    alicloud_vswitch.default: Creation complete after 4s [id=vsw-m5ehryjeb8rs3bm56j***]
    alicloud_hbase_instance.default: Creating...
    alicloud_hbase_instance.default: Still creating... [10s elapsed]
    alicloud_hbase_instance.default: Still creating... [20s elapsed]
    alicloud_hbase_instance.default: Still creating... [30s elapsed]
    alicloud_hbase_instance.default: Still creating... [40s elapsed]
    alicloud_hbase_instance.default: Still creating... [50s elapsed]
    alicloud_hbase_instance.default: Still creating... [1m0s elapsed]
    alicloud_hbase_instance.default: Still creating... [1m10s elapsed]
    alicloud_hbase_instance.default: Still creating... [1m20s elapsed]
    ...
    alicloud_hbase_instance.default: Still creating... [16m30s elapsed]
    alicloud_hbase_instance.default: Still creating... [16m40s elapsed]
    alicloud_hbase_instance.default: Still creating... [16m50s elapsed]
    alicloud_hbase_instance.default: Creation complete after 16m57s [id=ld-m5ezvi1qi8tkbu***]
    Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
  5. Verify the result.

    Terraform show

    You can run the following command to view the details of the resources created by Terraform:

    terraform show
    shell@Alicloud:~/ens/Hbase$ terraform show
    # alicloud_hbase_instance.default:
    resource "alicloud_hbase_instance" "default" {
        auto_renew              = false
        cold_storage_size       = 0
        core_disk_size          = 400
        core_disk_type          = "cloud_ssd"
        core_instance_quantity  = 2
        core_instance_type      = "hbase.sn2.large"
        deletion_protection     = false
        engine                  = "hbaseue"
        engine_version          = "2.0"
        id                      = "xxx"
        immediate_delete_flag   = true
        ip_white                = "127.0.0.1"
        maintain_end_time       = "22:00Z"
        maintain_start_time     = "18:00Z"
        master_instance_quantity = 2
        master_instance_type    = "hbase.sn2.large"
        name                    = "hbasetest"
        pay_type                = "PostPaid"
        security_groups         = []
        slb_conn_addrs          = []
    }

    Log on to the console

    After the cluster is created, log on to the ApsaraDB for HBase console to check the result. In the cluster list, you can see the hbasetest cluster that was created by Terraform. The service/version is HBase enhanced edition (Lindorm)/2.0, the status is running, the billing method is pay-as-you-go, and the network type is Virtual Private Cloud.

Clean up resources

When you no longer need the resources, run the following command to destroy them. For more information about the terraform destroy command, see Common commands.

terraform destroy

Complete example

Note

The sample code in this topic is ready to run. You can run it directly. Run Now

Sample code

variable "region" {
  default = "cn-qingdao"
}
provider "alicloud" {
  region = var.region
}
data "alicloud_hbase_zones" "default" {}
variable "instance_type" {
  default = "hbase.sn2.large"
}
variable "name" {
  default = "hbasetest"
}
# Create an alicloud_vpc resource.
resource "alicloud_vpc" "vpc1" {
  vpc_name   = var.name
  cidr_block = "172.16.0.0/12"
}
# Create an alicloud_vswitch resource within the VPC and specify a zone.
resource "alicloud_vswitch" "default" {
  vswitch_name = var.name
  cidr_block   = "172.16.20.0/24"
  vpc_id       = alicloud_vpc.vpc1.id
  zone_id      = data.alicloud_hbase_zones.default.zones[0].id
}
resource "alicloud_hbase_instance" "default" {
  timeouts {
    create = "100m" # Set a timeout for the creation.
  }
  # (Required) The name of the HBase instance. The name must be 2 to 128 characters in length and can contain Chinese characters, letters, digits, periods (.), underscores (_), and hyphens (-).
  name        = var.name
  #  (Optional, ForceNew) The zone in which to launch the HBase instance.
  zone_id                = data.alicloud_hbase_zones.default.zones[0].id
  # (Optional, ForceNew) The ID of the VSwitch. 
  vswitch_id             = alicloud_vswitch.default.id
  #  (Optional, ForceNew, Available from v1.185.0) The ID of the VPC.
  vpc_id                 = alicloud_vpc.vpc1.id
  #  (Optional, ForceNew) The engine of the instance. Valid values: "hbase", "hbaseue", "bds". "hbaseue" and "bds" are supported from v1.73.0. A single-node HBase instance requires `engine="hbase"` and `core_instance_quantity=1`.
  engine                 = "hbaseue"
  # (Required, ForceNew) The major engine version. Valid values for different engines: hbase (1.1, 2.0), hbaseue (2.0), bds (1.0).
  engine_version         = "2.0"
  # (Required) The instance type of the master nodes.
  master_instance_type   = var.instance_type
  # (Required) The instance type of the core nodes.
  core_instance_type     = var.instance_type
  # (Optional) The number of core nodes. Default value: 2. Valid values: 1 to 200. An instance with more than one core node is a cluster. An instance with one core node is a single-node instance.
  core_instance_quantity = "2"
  #  (Optional, ForceNew) The disk type of the core nodes. Valid values: cloud_ssd, cloud_essd_pl1, cloud_efficiency, local_hdd_pro, and local_ssd_pro. The `local_disk` has a fixed size. If `engine` is set to `bds`, this parameter is not required.
  core_disk_type         = "cloud_ssd"
  # (Optional) The storage capacity of each core node, in GB. This parameter is valid only when `engine` is "hbase" or "hbaseue". For the `bds` engine, `core_disk_size` is not required. Value ranges: cluster [400, 64000] in 40 GB increments; single-node [20, 500] in 1 GB increments.
  core_disk_size         = 400
  #  (Optional) The billing method. Valid values: PrePaid and PostPaid. Default value: PostPaid. You can change the billing method from PostPaid to PrePaid. From v1.115.0+, you can also change from PrePaid to PostPaid.
  pay_type               = "PostPaid"
  # (Optional) The cold storage capacity in GB. Valid values: 0 or 800 to 1,000,000 in 10 GB increments. A value of 0 indicates that cold storage is disabled. A value greater than 0 indicates that cold storage is enabled.
  cold_storage_size      = 0
  # (Optional, Available from v1.109.0) Specifies whether to delete the instance immediately. If true, the instance is deleted immediately. If false, it is deleted after a retention period.
  immediate_delete_flag  =  true
  # (Optional) The subscription duration in months. This parameter is valid only when `pay_type` is PrePaid. Valid values: 1 to 9, 12, 24, and 36. Values of 12, 24, and 36 correspond to 1, 2, and 3 years, respectively.
  duration               = 1
  # (Optional, Available from v1.73.0) The deletion protection switch. A value of true enables deletion protection. A value of false disables deletion protection. You must set this parameter to false to delete the instance.
  deletion_protection    = false
}

For more complete examples, go to the product-specific folders in More complete examples.