Use Terraform to manage Tair (Redis OSS-compatible) resources

Updated at:

This topic describes how to use Terraform as a Resource Access Management (RAM) user to create a Tair (Enterprise Edition) instance and create a database account on the instance.

Create a RAM user and grant permissions to the RAM user

If you have created a RAM user and granted permissions to the RAM user, you can skip this step.

  1. To create a RAM user, perform the following steps:

    1. Go to the Users page of the RAM console, and then click Create User.

    2. Set Logon Name to redis-terraform-test and Access Mode to Using permanent AccessKey to access.

    3. Click OK. After the RAM user is created, save the AccessKey ID and AccessKey secret of the RAM user.

  2. To grant permissions to the RAM user, perform the following steps:

    1. On the Users page, find the RAM user and click Add Permissions in the Actions column.

    2. Search for AliyunKvstore in the Policy search box and select the AliyunKvstoreFullAccess policy.

      Note

      In this example, the AliyunKvstoreFullAccess policy is used for demonstration purposes. This policy grants full permissions to query and modify Tair instances.

      In actual projects, you can select policies as needed or customize more fine-grained policies. For more information, see Identity management.

    3. In this example, you must also create a virtual private cloud (VPC) and a vSwitch. To do so, search for AliyunVPCFullAccess in the Policy search box and select the AliyunVPCFullAccess policy.

    4. Click Grant permissions.

Procedure

Install Terraform

  • For information about how to install and configure Terraform on your PC, see Install and configure Terraform.

    After Terraform is installed, you can open the CLI and then run the terraform version command. If version information is returned, Terraform is installed.

  • If you do not want to install Terraform, use Cloud Shell provided by Alibaba Cloud. Cloud Shell provides a built-in runtime environment for Terraform.

Create a template

Create a file named main.tf and enter the following content to create a Tair (Enterprise Edition) instance and create a database account on the instance.

# Load the alicloud plug-in and specify China (Shanghai) as the region. 
provider "alicloud" {
  region = "cn-shanghai"
}

# Define a function that is used as the instance name. 
variable "name" {
  default = "tf-example"
}

# Query the zones in the China (Shanghai) region where you can create a Tair DRAM-based instance. 
data "alicloud_kvstore_zones" "default" {
  product_type = "Tair_rdb"
}

# Create a VPC. 
resource "alicloud_vpc" "default" {
  vpc_name   = var.name
  cidr_block = "10.4.0.0/16"
}

# Create a vSwitch in the specified VPC and zone. 
resource "alicloud_vswitch" "default" {
  vswitch_name = var.name
  cidr_block   = "10.4.0.0/24"
  vpc_id       = alicloud_vpc.default.id
  zone_id      = data.alicloud_kvstore_zones.default.zones.0.id
}

# Create a Tair DRAM-based instance. 
resource "alicloud_redis_tair_instance" "default" {
  tair_instance_name = var.name
  payment_type       = "Subscription"
  instance_class     = "tair.rdb.with.proxy.2g"
  instance_type      = "tair_rdb"
  period             = "1"
  shard_count        = "2"
  zone_id            = data.alicloud_kvstore_zones.default.zones.0.id
  vpc_id             = alicloud_vpc.default.id
  vswitch_id         = alicloud_vswitch.default.id 
}

# Create a database account on the instance. 
resource "alicloud_kvstore_account" "default" {
  account_name     = "tfexamplename"
  account_password = "YourPassword_123"
  instance_id      = alicloud_redis_tair_instance.default.id
}

For more information about the configurations of the alicloud_redis_tair_instance resource type, see Tair (Redis OSS-Compatible) And Memcache (KVStore).

Execute the template

In this example, Cloud Shell is used for demonstration purposes. The specific way to run commands may vary on other operating systems.

  1. In the directory where the main.tf file is located, run the terraform init command to initialize and load modules.

    Expected output:

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.227.0...
    
    The following providers do not have any version constraints in configuration,
    so the latest version was installed.
    
    To prevent automatic upgrades to new major versions that may contain breaking
    changes, it is recommended to add version = "..." constraints to the
    corresponding provider blocks in configuration, with the constraint strings
    suggested below.
    
    * provider.alicloud: version = "~> 1.227"
    
    
    Warning: registry.terraform.io: For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers.
    
    
    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.
  2. Run the terraform validate command to check whether the template syntax is correct.

    Expected output:

    Success!  The configuration is valid.
  3. Run the terraform plan command to create an execution plan.

    Expected output:

    Refreshing Terraform state in-memory prior to plan...
    The refreshed state will be used to calculate this plan, but will not be
    persisted to local or remote state storage.
    
    data.alicloud_kvstore_zones.default: Refreshing state...
    
    ------------------------------------------------------------------------
    
    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # alicloud_kvstore_account.default will be created
      + resource "alicloud_kvstore_account" "default" {
          + account_name      = "tfexamplename"
          + account_password  = (sensitive value)
          + account_privilege = "RoleReadWrite"
          + account_type      = "Normal"
          + id                = (known after apply)
          + instance_id       = (known after apply)
          + status            = (known after apply)
        }
    
      # alicloud_redis_tair_instance.default will be created
      + resource "alicloud_redis_tair_instance" "default" {
          + create_time        = (known after apply)
          + engine_version     = (known after apply)
          + id                 = (known after apply)
          + instance_class     = "tair.rdb.with.proxy.2g"
          + instance_type      = "tair_rdb"
          + node_type          = (known after apply)
          + payment_type       = "Subscription"
          + period             = 1
          + port               = (known after apply)
          + resource_group_id  = (known after apply)
          + shard_count        = 2
          + ssl_enabled        = (known after apply)
          + status             = (known after apply)
          + storage_size_gb    = (known after apply)
          + tair_instance_name = "tf-example"
          + vpc_id             = (known after apply)
          + vswitch_id         = (known after apply)
          + zone_id            = "cn-shanghai-b"
        }
    
      # alicloud_vpc.default will be created
      + resource "alicloud_vpc" "default" {
          + cidr_block            = "10.4.0.0/16"
          + create_time           = (known after apply)
          + id                    = (known after apply)
          + ipv6_cidr_block       = (known after apply)
          + ipv6_cidr_blocks      = (known after apply)
          + name                  = (known after apply)
          + resource_group_id     = (known after apply)
          + route_table_id        = (known after apply)
          + router_id             = (known after apply)
          + router_table_id       = (known after apply)
          + secondary_cidr_blocks = (known after apply)
          + status                = (known after apply)
          + user_cidrs            = (known after apply)
          + vpc_name              = "tf-example"
        }
    
      # alicloud_vswitch.default will be created
      + resource "alicloud_vswitch" "default" {
          + availability_zone    = (known after apply)
          + cidr_block           = "10.4.0.0/24"
          + create_time          = (known after apply)
          + id                   = (known after apply)
          + ipv6_cidr_block      = (known after apply)
          + ipv6_cidr_block_mask = (known after apply)
          + name                 = (known after apply)
          + status               = (known after apply)
          + vpc_id               = (known after apply)
          + vswitch_name         = "tf-example"
          + zone_id              = "cn-shanghai-b"
        }
    
    Plan: 4 to add, 0 to change, 0 to destroy.
    
    ------------------------------------------------------------------------
    
    Note: You didn't specify an "-out" parameter to save this plan, so Terraform
    can't guarantee that exactly these actions will be performed if
    "terraform apply" is subsequently run.
  4. Run the terraform apply command to deploy the template.

    Partial output:

    Plan: 4 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: 

    In this case, you need to enter yes to change resources. The following information is returned:

    alicloud_vpc.default: Creating...
    alicloud_vpc.default: Creation complete after 6s [id=vpc-uf6****1k4o4fqifukqfc]
    alicloud_vswitch.default: Creating...
    alicloud_vswitch.default: Creation complete after 4s [id=vsw-uf6****cajhh00xznkwqm]
    alicloud_redis_tair_instance.default: Creating...
    alicloud_redis_tair_instance.default: Still creating... [10s elapsed]
    alicloud_redis_tair_instance.default: Still creating... [20s elapsed]
    ...
    alicloud_redis_tair_instance.default: Still creating... [7m0s elapsed]
    alicloud_redis_tair_instance.default: Still creating... [7m10s elapsed]
    alicloud_redis_tair_instance.default: Creation complete after 7m18s [id=r-uf6****4c367b1q6ty]
    alicloud_kvstore_account.default: Creating...
    alicloud_kvstore_account.default: Still creating... [10s elapsed]
    ...
    alicloud_kvstore_account.default: Still creating... [1m0s elapsed]
    alicloud_kvstore_account.default: Creation complete after 1m1s [id=r-uf6****4c367b1q6ty:tfexamplename]
    
    Apply complete!  Resources: 4 added, 0 changed, 0 destroyed.
  5. After the instance is created, you can call an API operation, use an SDK, or log on to the Tair console to view the instance.

References