Terraform集成示例

本文介绍RAM用户如何使用Terraform调用云原生数据仓库 AnalyticDB PostgreSQL 版的OpenAPI,为指定实例创建一个数据库账号。

前提条件

操作步骤

步骤一:编写模板

创建执行目录并切换到该目录(本次示例目录为:D:\Program Files\test)。在该目录下新建一个名为main.tf的terraform模板文件。模板内容如下:

resource "alicloud_gpdb_account" "default" {
  account_name        = "testaccount"
  db_instance_id      = "gp-bp186zl1fu22g****"
  account_password    = "test123456@"
  account_description = "tf_example"
}

参数说明,请参见alicloud_gpdb_account

步骤二:运行模板

  1. 在执行目录中使用terraform init命令加载初始化配置。

    PS D:\Program Files\test> terraform init

    结果如下:

    Initializing the backend...
    Initializing provider plugins...
    - Finding latest version of hashicorp/alicloud...
    - Installing hashicorp/alicloud v1.228.0...
    - Installed hashicorp/alicloud v1.228.0 (signed by HashiCorp)
    Terraform has created a lock file .terraform.lock.hcl to record the provider
    selections it made above. Include this file in your version control repository
    so that Terraform can guarantee to make the same selections by default when
    you run "terraform init" in the future.
    
    ╷
    │ Warning: Additional provider information from registry
    │
    │ The remote registry returned warnings for registry.terraform.io/hashicorp/alicloud:
    │ - 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验证模板语法。

    PS D:\Program Files\test> terraform validate

    结果如下:

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

    PS D:\Program Files\test> terraform plan

    结果如下:

    Terraform used the selected providers to generate the following execution plan. Resource
    actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # alicloud_gpdb_account.default will be created
      + resource "alicloud_gpdb_account" "default" {
          + account_description = "tf_example"
          + account_name        = "testaccount"
          + account_password    = "test123456@"
          + db_instance_id      = "gp-bp186zl1fu22g****"
          + id                  = (known after apply)
          + status              = (known after apply)
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
  4. 执行terraform apply运行模板。

    PS D:\Program Files\test> terraform apply

    Enter a value:提示出来后,输入yes,等待创建账号。

    结果如下:

    Terraform used the selected providers to generate the following execution plan. Resource
    actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # alicloud_gpdb_account.default will be created
      + resource "alicloud_gpdb_account" "default" {
          + account_description = "tf_example"
          + account_name        = "testaccount"
          + account_password    = "test123456@"
          + db_instance_id      = "gp-bp186zl1fu22g****"
          + id                  = (known after apply)
          + status              = (known after apply)
        }
    
    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_gpdb_account.default: Creating...
    alicloud_gpdb_account.default: Still creating... [10s elapsed]
    alicloud_gpdb_account.default: Still creating... [25s elapsed]
    alicloud_gpdb_account.default: Still creating... [35s elapsed]
    alicloud_gpdb_account.default: Still creating... [45s elapsed]
    alicloud_gpdb_account.default: Still creating... [55s elapsed]
    alicloud_gpdb_account.default: Creation complete after 1m2s [id=gp-bp186zl1fu22g****:testaccount]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  5. 创建完成后,您可以通过以下方式查看所创建的数据库账号。