最近更新时间:2023.04.18 19:06:03
首次发布时间:2023.04.18 19:06:03
使用 Terraform 的过程是围绕 main.tf
文件进行,通过在 main.tf
文件中追加相应的内容实现资源的管理与编排。本文以创建云数据库 MySQL 版实例为例进行说明。
以创建云数据库 MySQL 版实例为例,追加以下内容到 main.tf
文件,参数内容需要自行修改,其中 my-instance-0 表示资源名字。
resource "volcengine_rds_instance_v2" "my-instance-0" { db_engine_version = "MySQL_5_7" instance_type = "HA" node_info { node_type = "Primary" node_spec = "rds.mysql.1c2g" zone_id = "cn-beijing-a" } node_info { node_type = "Secondary" node_spec = "rds.mysql.1c2g" zone_id = "cn-beijing-a" } storage_type = "LocalSSD" storage_space = 100 vpc_id = "vpc-xxx" subnet_id = "subnet-xxx" charge_info { charge_type = "PostPaid" } }
进入 main.tf
所在目录,在终端执行以下命令,查看执行计划。 terraform plan
执行后,在终端中会显示待创建实例的相关信息,示例如下。
# volcengine_rds_instance_v2.my-instance-0 will be created + resource "volcengine_rds_instance_v2" "my-instance-0" { + connection_info = (known after apply) + db_engine_version = "MySQL_5_7" + id = (known after apply) + instance_type = "HA" + storage_space = 100 + storage_type = "LocalSSD" + subnet_id = "subnet-rrzzs6dzof0gv0x58bh****" + vpc_id = "vpc-13g2qkkzntfy83n6nu4z1****" + charge_info { + auto_renew = (known after apply) + charge_type = "PostPaid" + period = (known after apply) + period_unit = (known after apply) } + node_info { + node_id = (known after apply) + node_spec = "rds.mysql.1c2g" + node_type = "Primary" + zone_id = "cn-beijing-a" } + node_info { + node_id = (known after apply) + node_spec = "rds.mysql.1c2g" + node_type = "Secondary" + zone_id = "cn-beijing-a" } } Plan: 1 to add, 0 to change, 1 to destroy.
执行以下命令,应用执行计划。
terraform apply
执行后,在终端中会显示确认信息。如确认执行,则输入 yes;输入其他内容可取消执行计划。
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 后,开始执行计划。执行后,终端中会有以下提示消息。
$ terraform apply ... 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 ... Apply complete! Resources: 1 added, 0 changed, 0 destroyed.