You need to enable JavaScript to run this app.
导航

使用 Terraform

最近更新时间2024.01.22 17:52:37

首次发布时间2023.04.18 19:06:03

使用 Terraform 的过程是围绕 main.tf 文件进行,通过在 main.tf 文件中追加相应的内容实现资源的管理与编排。本文以创建云数据库 MySQL 版实例为例进行说明。

操作步骤

  1. 以创建云数据库 MySQL 版实例为例,追加以下内容到 main.tf 文件,参数内容需要自行修改,其中 my-instance-0 表示资源名字。

    resource "volcengine_rds_mysql_instance" "my-instance-0" {
      db_engine_version      = "MySQL_5_7"
      node_spec              = "rds.mysql.1c2g"
      primary_zone_id        = data.volcengine_zones.my-instance-0.zones[0].id
      secondary_zone_id      = data.volcengine_zones.my-instance-0.zones[0].id
      storage_space          = 80
      subnet_id              = volcengine_subnet.my-instance-0.id
      instance_name          = "acc-test"
      lower_case_table_names = "1"
    
      charge_info {
        charge_type = "PostPaid"
      }
    
      parameters {
        parameter_name  = "auto_increment_increment"
        parameter_value = "2"
      }
      parameters {
        parameter_name  = "auto_increment_offset"
        parameter_value = "4"
      }
    
      project_name = "default"
      tags {
        key   = "k1"
        value = "v1"
      }
    }
    
  2. 进入 main.tf 所在目录,在终端执行以下命令,查看执行计划。terraform plan 执行后,在终端中会显示待创建实例的相关信息,示例如下。

    # volcengine_rds_mysql_instance.my-instance-0 will be created
    + resource "volcengine_rds_mysql_instance" "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.
    
  3. 执行以下命令,应用执行计划。

    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.
    

相关文档