Terraform集成示例
本文介紹RAM用戶如何使用Terraform調(diào)用云原生數(shù)據(jù)倉庫 AnalyticDB PostgreSQL 版的OpenAPI,為指定實例創(chuàng)建一個數(shù)據(jù)庫賬號。
前提條件
執(zhí)行Terraform模板需要RAM用戶權(quán)限,需要您創(chuàng)建RAM用戶,并為RAM用戶創(chuàng)建AccessKey,同時為其添加
AliyunGPDBFullAccess
權(quán)限策略,具體操作請參見RAM用戶概覽。已經(jīng)安裝Terraform。具體操作,請參見在本地安裝和配置Terraform或在Cloud Shell中使用Terraform。
操作步驟
步驟一:編寫模板
創(chuàng)建執(zhí)行目錄并切換到該目錄(本次示例目錄為:D:\Program Files\test
)。在該目錄下新建一個名為main.tf的terraform模板文件。模板內(nèi)容如下:
resource "alicloud_gpdb_account" "default" {
account_name = "testaccount"
db_instance_id = "gp-bp186zl1fu22g****"
account_password = "test123456@"
account_description = "tf_example"
}
參數(shù)說明,請參見alicloud_gpdb_account。
步驟二:運行模板
在執(zhí)行目錄中使用
terraform init
命令加載初始化配置。PS D:\Program Files\test> terraform init
結(jié)果如下:
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.
執(zhí)行
terraform validate
驗證模板語法。PS D:\Program Files\test> terraform validate
結(jié)果如下:
Success! The configuration is valid.
執(zhí)行
terraform plan
創(chuàng)建執(zhí)行計劃。PS D:\Program Files\test> terraform plan
結(jié)果如下:
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.
執(zhí)行
terraform apply
運行模板。PS D:\Program Files\test> terraform apply
在
Enter a value:
提示出來后,輸入yes,等待創(chuàng)建賬號。結(jié)果如下:
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.
創(chuàng)建完成后,您可以通過以下方式查看所創(chuàng)建的數(shù)據(jù)庫賬號。
運行
terraform show
命令查看。