All Products
Search
Document Center

Tablestore:Integrate Tablestore by using Terraform

Last Updated:Nov 05, 2024

This topic describes how to install and configure Terraform. This topic also describes how to use Terraform to create a Tablestore instance.

Prerequisites

The following operations are performed in the Resource Access Management (RAM) console:

  • A RAM user is created and the AliyunOTSFullAccess policy is attached to the RAM user to grant the RAM user the permissions to manage Tablestore. For more information, see Create a RAM user and Grant permissions to a RAM user.

    Note
    • In this example, the AliyunOTSFullAccess policy is used for demonstration purposes. Grant the required permissions to the RAM user in a fine-grained manner based on your business requirements. For more information, see Create a custom policy.Tablestore

    • If you connect to Terraform by using Cloud Shell, you must also attach the AliyunCloudShellFullAccess policy to the RAM user.

  • An AccessKey pair is created for the RAM user. For more information, see Create an AccessKey pair.

    Warning

    If the AccessKey pair of your Alibaba Cloud account is leaked, your resources are exposed to potential risks. We recommend that you use the AccessKey pair of a RAM user to perform operations. This prevents leakage of the AccessKey pair of your Alibaba Cloud account.

Procedure

Note

In this example, Linux is used to describe how to integrate Tablestore by using Terraform.

1. Install and configure Terraform

  1. Download a software package suitable for your operating system from the official Terraform website.

  2. Decompress the package to the /usr/local/bin directory.

    If you decompress the executable file to another directory, you must add the path to global variables.

  3. Run the following command to check whether Terraform is installed:

    terraform

    If the following output is returned, Terraform is installed.

    image

  4. Create a working directory for your Terraform project.

    Important

    You must create a working directory for each Terraform project.

    mkdir terraform-test
  5. Go to the working directory named terraform-test.

    cd terraform-test

    Terraform reads all *.tf and *.tfvars files in the directory when Terraform is running. You can write configurations to different files based on your business requirements. The following table describes the common configuration files.

    File

    Description

    provider.tf

    The provider configuration file.

    terraform.tfvars

    The configuration file that contains variables required to configure the provider.

    variable.tf

    The configuration file that contains common variables.

    resource.tf

    The resource configuration file.

    data.tf

    The package configuration file.

    output.tf

    The output file.

    1. Create a configuration file name provider.tf for authentication.

      vim provider.tf

      The following sample code provides an example on how to specify the configuration file:

      provider "alicloud" {
        region = "cn-hangzhou"
        access_key = "LTA**********NO2"
        secret_key = "MOk8x0*********************wwff"
      }

      The following table describes the parameters in the configuration file.

      Parameter

      Example

      Description

      region

      cn-hangzhou

      The ID of the region. For more information, see Regions.

      access_key

      LTA**********NO2

      The AccessKey ID and AccessKey secret of your Alibaba Cloud account or RAM user.

      secret_key

      MOk8x0*********************wwff

    2. Initialize the terraform-test working directory.

      Important

      After you create a working directory and configuration files for a Terraform project, you must initialize the working directory.

      terraform init

      If the following output is returned, the working directory is loaded.

      image

2. Write a template

Create a file named main.tf in the terraform-test working directory and enter the following code.

For more information about the syntax, description, and examples of how to create a Tablestore instance, see alicloud_ots_instance.

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

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

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"
  }
}

3. Run the template

  1. Check whether the template syntax is valid.

    terraform validate

    If the following output is returned, the template syntax is valid.

    image

  2. Create an execution plan and preview the operations that you want to perform.

    terraform plan

    If the following output is returned, the execution plan is created.

    image

  3. Run the template to apply the execution plan.

    terraform apply

    Enter yes to change resources.

    image

    If the following output is returned, the resources are changed.

    image

    In the output, id indicates the name of the instance.

  4. You can perform the following steps to view the instance that you created:

    1. Log on to the Tablestore console.

    2. In the top navigation bar, select a resource group and a region.

    3. In the instance list of the Overview page, search for the instance name.

      image

References

For more information about how to install and configure Terraform, see Use Terraform in Cloud Shell and Install and configure Terraform in the local PC.