Terraform集成示例
本文为您介绍RAM用户如何使用Terraform调用云原生数据仓库 AnalyticDB MySQL 版的OpenAPI为企业版、基础版或湖仓版集群创建数据库高权限账号。
本教程所含示例代码支持一键运行,您可以直接运行代码。一键运行
前提条件
由于阿里云账号(主账号)具有资源的所有权限,一旦发生泄露将面临重大风险。建议您使用RAM用户,并为该RAM用户创建AccessKey,具体操作方式请参见创建RAM用户和创建AccessKey。
通过RAM授权,阿里云用户可以有效地管理其云资源访问权限,适应多用户协同工作的需求,并且能够按需为用户分配最小权限,避免权限过大导致的安全漏洞。具体操作方式请参见为RAM用户授权。
准备Terraform运行环境,您可以选择以下任一方式来使用Terraform。
在Terraform Explorer中使用Terraform:阿里云提供了Terraform的在线运行环境,您无需安装Terraform,登录后即可在线使用和体验Terraform。适用于零成本、快速、便捷地体验和调试Terraform的场景。
Cloud Shell:阿里云Cloud Shell中预装了Terraform的组件,并已配置好身份凭证,您可直接在Cloud Shell中运行Terraform的命令。适用于低成本、快速、便捷地访问和使用Terraform的场景。
在本地安装和配置Terraform:适用于网络连接较差或需要自定义开发环境的场景。
使用的资源
本教程示例包含的部分资源会产生一定费用,完成教程后,若无需使用该资源,请及时释放或退订。
alicloud_vpc:创建专有网络VPC。
alicloud_vswitch:创建专有网络交换机。
alicloud_adb_db_cluster_lake_version:创建云原生数据仓库 AnalyticDB MySQL 版湖仓版集群。
alicloud_adb_lake_account:创建云原生数据仓库 AnalyticDB MySQL 版湖仓版集群账号。
创建数据库账号
本示例以创建云原生数据仓库 AnalyticDB MySQL 版湖仓版集群高权限账号为例。
创建一个工作目录,并在该工作目录中创建名为main.tf的配置文件,然后将以下代码复制到main.tf中。
创建前置资源。
variable "name" { default = "terraform-example" } variable "region" { default = "cn-shenzhen" } variable "zone_id" { default = "cn-shenzhen-e" } provider "alicloud" { region = var.region } resource "alicloud_vpc" "default" { vpc_name = "alicloud" cidr_block = "172.16.0.0/16" } resource "alicloud_vswitch" "default" { vpc_id = alicloud_vpc.default.id cidr_block = "172.16.192.0/20" zone_id = var.zone_id } resource "alicloud_adb_db_cluster_lake_version" "CreateInstance" { storage_resource = "0ACU" zone_id = var.zone_id vpc_id = alicloud_vpc.default.id vswitch_id = alicloud_vswitch.default.id db_cluster_description = var.name compute_resource = "32ACU" db_cluster_version = "5.0" payment_type = "PayAsYouGo" security_ips = "127.0.0.1" enable_default_resource_group = false }
在main.tf文件中增加
resource "alicloud_adb_lake_account" "default"
配置项。resource "alicloud_adb_lake_account" "default" { db_cluster_id = alicloud_adb_db_cluster_lake_version.CreateInstance.id account_type = "Super" account_name = "tfnormal" account_password = "normal@2023" account_privileges { privilege_type = "Database" privilege_object { database = "MYSQL" } privileges = [ "select", "update" ] } account_privileges { privilege_type = "Table" privilege_object { database = "INFORMATION_SCHEMA" table = "ENGINES" } privileges = [ "update" ] } account_privileges { privilege_type = "Column" privilege_object { table = "COLUMNS" column = "PRIVILEGES" database = "INFORMATION_SCHEMA" } privileges = [ "update" ] } account_description = var.name }
执行以下命令,初始化Terraform运行环境。
terraform init
返回如下信息,表示Terraform初始化成功。
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... 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
创建执行计划,并预览变更。
terraform plan
执行以下命令,创建资源。
terraform apply
在执行过程中,根据提示输入
yes
并按下Enter键,等待命令执行完成,若出现以下信息,则表示运行成功。alicloud_vpc.default: Refreshing state... [id=vpc-****] alicloud_vswitch.default: Refreshing state... [id=vsw-****] alicloud_adb_db_cluster_lake_version.CreateInstance: Refreshing state... [id=amv-****] 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_adb_lake_account.default will be created + resource "alicloud_adb_lake_account" "default" { + account_description = "terraform-example" + account_name = "tfnormal" + account_password = (sensitive value) + account_type = "Super" + db_cluster_id = "amv-****" + id = (known after apply) + status = (known after apply) + account_privileges { + privilege_type = "Column" + privileges = [ + "update", ] + privilege_object { + column = "PRIVILEGES" + database = "INFORMATION_SCHEMA" + table = "COLUMNS" } } + account_privileges { + privilege_type = "Table" + privileges = [ + "update", ] + privilege_object { + column = (known after apply) + database = "INFORMATION_SCHEMA" + table = "ENGINES" } } + account_privileges { + privilege_type = "Database" + privileges = [ + "select", + "update", ] + privilege_object { + column = (known after apply) + database = "MYSQL" + table = (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_adb_lake_account.default: Creating... alicloud_adb_lake_account.default: Creation complete after 4s [id=amv-****:tfnormal] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
验证结果。
执行terraform show命令
执行
terraform show
查看创建的账号。terraform show
# alicloud_adb_lake_account.default: resource "alicloud_adb_lake_account" "default" { account_description = "terraform-example" account_name = "tfnormal" account_password = (sensitive value) account_type = "Super" db_cluster_id = "amv-****" id = "amv-****:tfnormal" status = "Available" account_privileges { privilege_type = "Table" privileges = [ "update", ] privilege_object { column = null database = "INFORMATION_SCHEMA" table = "ENGINES" } } account_privileges { privilege_type = "Database" privileges = [ "select", "update", ] privilege_object { column = null database = "MYSQL" table = null } } account_privileges { privilege_type = "Column" privileges = [ "update", ] privilege_object { column = "PRIVILEGES" database = "INFORMATION_SCHEMA" table = "COLUMNS" } } }
登录云原生数据仓库AnalyticDB MySQL控制台
登录云原生数据仓库AnalyticDB MySQL控制台查看创建的账号。
清理资源
若您已无需使用上述已创建的资源,请执行以下命令释放资源。关于terraform destroy
的更多信息,请参见Terraform常用命令。
terraform destroy
完整示例
当前示例代码支持一键运行,您可以直接运行代码。一键运行
如果您想体验更多完整示例,请前往更多完整示例中对应产品的文件夹查看。