文档

Terraform集成示例

更新时间:

本文为您演示RAM用户如何使用Terraform在DMS创建一个权限模板。

前置概念

如果您还不了解Terraform,请参见什么是Terraform

创建RAM用户并完成授权

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

  1. 创建RAM用户。

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

    2. 设置登录名称

      例如,填入dms_testuser

    3. 选择访问方式OpenAPI 调用访问

    4. 单击确定。保存RAM用户的UID。

  2. 完成授权。

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

    2. 在文本框中搜索AliyunDMSFullAccess策略。

      说明
      • AliyunDMSFullAccess策略具有管理DMS资源的权限。

      • 如果您需要使用阿里云提供的在线服务Cloud Shell,还需要添加AliyunCloudShellFullAccess(管理云命令行的权限)。

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

操作步骤

安装Terraform

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

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

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

编写模板

创建一个名为main.tf文件,填入以下内容,用于在DMS新建一个权限模板。

模板中的tid、authority_template_namedescription参数需要根据您的实际情况自行填写。

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

provider "alicloud" {
  region = "cn-hangzhou"
}

data "alicloud_dms_user_tenants" "default" {
  status = "ACTIVE"
}

resource "alicloud_dms_enterprise_authority_template" "default" {
  tid                     = 233***
  authority_template_name = "jichengtest"
  description             = "test"
}

关于该模板的更多信息,请参见alicloud_dms_enterprise_authority_template

运行模板

该示例以在Cloud Shell操作为例,本地Terraform运行命令的具体方式可能会有所不同。

  1. 初始化加载模块,包括Provider,Provisioner,Module等模板。

    terraform init

    返回结果:

    Terraform has been successfully initialized!
  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_dms_user_tenants.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_dms_enterprise_authority_template.default will be created
      + resource "alicloud_dms_enterprise_authority_template" "default" {
          + authority_template_id   = (known after apply)
          + authority_template_name = "jichengtest"
          + create_time             = (known after apply)
          + description             = "test"
          + id                      = (known after apply)
          + tid                     = 233***
        }
    
    Plan: 1 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

    返回结果:

    data.alicloud_dms_user_tenants.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_dms_enterprise_authority_template.default will be created
      + resource "alicloud_dms_enterprise_authority_template" "default" {
          + authority_template_id   = (known after apply)
          + authority_template_name = "jichengtest"
          + create_time             = (known after apply)
          + description             = "test"
          + id                      = (known after apply)
          + tid                     = 233***
        }
    
    Plan: 1 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_dms_enterprise_authority_template.default: Creating...
    alicloud_dms_enterprise_authority_template.default: Creation complete after 0s [id=233***:5049]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  5. 创建完成后,您可以通过OpenAPI、SDK或者在云备份控制台,可以查看到创建的权限模板。查看权限模板的具体操作,请参见创建权限模板

    image