文档

Terraform集成示例

更新时间:

本文为您介绍RAM用户如何通过Terraform创建Tair(Redis企业版)实例,并在新实例中创建一个数据库账号。

创建RAM用户并完成授权

如您已创建RAM用户且已完成授权,可跳过此步骤。

  1. 创建RAM用户:

    1. 访问RAM用户列表,单击创建用户

    2. 设置登录名称redis-terraform-test,选择访问方式OpenAPI 调用访问

    3. 单击确定,创建RAM用户并保存AccessKey ID与AccessKey Secret信息。

  2. 完成授权:

    1. 访问RAM用户列表,单击目标RAM用户操作列的添加权限

    2. 在文本框中搜索AliyunKvstore,选择AliyunKvstoreFullAccess策略。

      说明

      本示例为了便于演示,使用AliyunKvstoreFullAccess策略,该策略具有查询、修改Redis实例的完全控制权限

      在实际项目中,您也可以按需选择权限或定制更加精细化的自定义策略,更多信息请参见身份管理

    3. 本示例还需新建VPC和交换机,请在文本框中搜索AliyunVPCFullAccess,选择AliyunVPCFullAccess策略。

    4. 单击确认新增授权,完成授权操作。

操作步骤

安装Terraform

  • 在本地安装和配置Terraform,请参见在本地安装和配置Terraform

    安装完成后,您可以打开命令行终端,输入terraform version,看到返回版本信息表示已成功安装。

  • 如果您不想安装Terraform,可以使用阿里云提供的在线服务Cloud Shell,其中内置了Terraform的运行环境。

编写模板

创建一个名为main.tf文件,填入以下内容,用于创建一个Tair(Redis企业版)实例,并在新实例中创建一个数据库账号。

# 载入alicloud插件,并指定地域为华东2(上海)。
provider "alicloud" {
  region = "cn-shanghai"
}

# 定义一个函数,用于作为实例名称。
variable "name" {
  default = "tf-example"
}

# 查询华东2(上海)地域中可创建Tair内存型的可用区。
data "alicloud_kvstore_zones" "default" {
  product_type = "Tair_rdb"
}

# 创建一个VPC实例。
resource "alicloud_vpc" "default" {
  vpc_name   = var.name
  cidr_block = "10.4.0.0/16"
}

# 在指定VPC、可用区中创建一个交换机实例。
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
}

# 创建一个Tair内存型实例。
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 
}

# 在新实例中创建一个数据库账号。
resource "alicloud_kvstore_account" "default" {
  account_name     = "tfexamplename"
  account_password = "YourPassword_123"
  instance_id      = alicloud_redis_tair_instance.default.id
}

更多关于alicloud_redis_tair_instance资源类型的配置信息,请参见Redis And Memcache (KVStore)

运行模板

本示例以Cloud Shell操作为例,在其他操作系统中,运行命令的具体方式可能会有所不同。

  1. 在main.tf文件所在的目录下,执行terraform init命令,初始化、加载模块。

    预计返回:

    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. 执行terraform validate命令,验证模板语法是否正确。

    预计返回:

    Success! The configuration is valid.
  3. 执行terraform plan命令,创建执行计划。

    预计返回:

    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. 执行terraform apply命令,部署模板。

    部分返回信息:

    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: 

    此时,您需要输入yes即可变更资源。输入后的返回信息如下:

    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. 创建完成后,您可以通过OpenAPI、SDK或者在Redis控制台,查看新创建的实例。

相关文档