All Products
Search
Document Center

Tablestore:Use Terraform to create a Tablestore instance

Last Updated:Dec 04, 2024

Terraform is an open source tool that is used to preview, configure, and manage cloud infrastructure and resources in a secure and efficient manner. Tablestore is connected to Terraform. This topic describes how to create a Tablestore instance by using Terraform.

Note

You can run the sample code in this topic with a few clicks. For more information, visit Terraform Explorer.

Prerequisites

  • We recommend that you use a RAM user that has the minimum required permissions to perform the operations in this topic. This reduces the risk of leaking the AccessKey pair of your Alibaba Cloud account. For information about how to attach the policy that contains the minimum required permissions to the RAM user, see Create a RAM user and Grant permissions to a RAM user. In this example, the following policy is used:

    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "ots:GetInstance",
            "ots:BindInstance2Vpc",
            "ots:ListVpcInfoByInstance",
            "ots:UnbindInstance2Vpc",
            "ots:DeleteInstance",
            "ots:InsertInstance",
            "ots:UpdateInstance",
            "ots:ListInstance"
          ],
          "Resource": "*"
        }
      ]
    }
  • The runtime environment for Terraform is prepared by using one of the following methods:

    • Explorer: Alibaba Cloud provides Terraform Explorer, an online runtime environment for Terraform. You can use Terraform after you log on to Terraform Explorer without the need to install Terraform. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at no additional costs.

    • Cloud Shell: Terraform is preinstalled in Cloud Shell and identity credentials are configured. You can directly run Terraform commands in Cloud Shell. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at low costs.

    • Install and configure Terraform on your on-premises machine: This method is suitable for scenarios in which network conditions are poor or a custom development environment is used. For more information, see Install and configure Terraform.

Required resources

Create a Tablestore instance

  1. Create a working directory and a configuration file named main.tf in the directory. Copy the following code to the main.tf configuration file:

    variable "name" {
      default = "tf-example"
    }
    
    variable "region" {
      default = "cn-hangzhou"
    }
    
    provider "alicloud" {
      region = var.region
    }
    
    resource "random_integer" "default" {
      min = 10000
      max = 99999
    }
    
    # Tablestore instance
    resource "alicloud_ots_instance" "default" {
      name        = "${var.name}-${random_integer.default.result}"  #The name of the instance.
      description = var.name  #The description of the instance.
      accessed_by = "Vpc"  #The networks over which you can access the instance.
      tags = {  #The tags of the instance.
        Created = "TF"
        For     = "Building table"
      }
    }
  2. Run the following command to initialize the Terraform runtime environment:

    terraform init

    If the following information is returned, Terraform is initialized:

    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 start code execution:

    terraform apply

    During the code execution, enter yes as prompted and press the Enter key. Wait until the command is complete. If the following command output is returned, the code is successfully run.

    You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
    
    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
    
    
    Apply complete!  Resources: 2 added, 0 changed, 0 destroyed.
  4. Verify the result.

    Run the terraform show command

    Run the following command in the working directory to query the details of the instance that is created by using Terraform:

    terraform show

    image

    Log on to the Tablestore console

    Log on to the Tablestore console. On the All Instances page, view the created instance.

    image

Release resources

If you no longer require the preceding resources that are created or managed by using Terraform, run the following command to release the resources. For more information about the terraform destroy command, see Common commands.

terraform destroy

During the command execution, preview the resources that you want to release. To confirm the deletion, enter yes as prompted and press the Enter key. Wait until the command is executed. If the following information appears, the resources are released.

Plan: 0 to add, 0 to change, 2 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes
  ...
  Destroy complete!  Resources: 2 destroyed.

Example

Note

You can run the sample code in this topic with a few clicks. For more information, visit Terraform Explorer.

Sample code

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

variable "region" {
  default = "cn-hangzhou"
}

provider "alicloud" {
  region = var.region
}

resource "random_integer" "default" {
  min = 10000
  max = 99999
}

# Tablestore instance
resource "alicloud_ots_instance" "default" {
  name        = "${var.name}-${random_integer.default.result}"  #The name of the instance.
  description = var.name  #The description of the instance.
  accessed_by = "Vpc"  #The networks over which you can access the instance.
  tags = {  #The tags of the instance.
    Created = "TF"
    For     = "Building table"
  }
}

If you want to view more complete examples, visit the directory of the corresponding service on the More Complete Examples page.

FAQ

  1. When I use Terraform to create instances of multiple cloud services, how do I increase the efficiency of creating and deleting instances?

  • Example: Customer A uses Terraform to continuously create instances of a series of cloud services. Tablestore instances are created but instances of other cloud services fail to be created in the subsequent process. In this case, Customer A rolls back the workflow, deletes the created instances, and prepares to re-run the workflow. A long period of time is required to delete Tablestore instances and each instance name is unique within the same region. Therefore, Customer A cannot create the same Tablestore instances before the instances are deleted. In this case, Customer A has to wait until the instances are deleted.

  • Solution: Add a random number suffix or an auto-increment ID to the name of each Tablestore instance to ensure that instances that have different names are created each time the workflow is executed. This way, when a node of a workflow fails and the workflow needs to be rolled back and re-executed, the workflow can be re-executed without the need to wait for the Tablestore instances of the previous node to be deleted.