租户端 云资源管理 资源编排(TIC) 操作指南 使用 Terraform 创建云平台资源(以 VPC 为例)

使用 Terraform 创建云平台资源(以 VPC 为例)

最近更新时间: 2026-06-30 15:06:00

创建 provider.tf 文件

指定 provider 配置信息。文件内容如下:

terraform {
  required_providers {
    cloud = {
      source = "myorg/cloud"
    }
  }
}

创建 main.tf 文件,配置 Provider 并创建私有网络 VPC

文件内容如下:

main.tf
 
resource "cloud_vpc" "cloud_vpc-bppk" {
  cidr_block = "10.0.0.0/16"
  name      = "ci-temp-test-updated"
  tags = {
"test" = "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:
 
  # cloud_vpc.cloud_vpc-bppk will be created
  + resource "cloud_vpc" "cloud_vpc-bppk" {
      + assistant_cidrs        = (known after apply)
      + cidr_block             = "10.0.0.0/16"
      + create_time            = (known after apply)
      + default_route_table_id = (known after apply)
      + dns_servers            = (known after apply)
      + docker_assistant_cidrs = (known after apply)
      + id                     = (known after apply)
      + is_default             = (known after apply)
      + is_multicast           = false
      + name                   = "ci-temp-test-updated"
      + tags                   = {
          + "test" = "test"
        }
      + vpc_id                 = (known after apply)
    }
 
Plan: 1 to add, 0 to change, 0 to destroy.
Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.

执行以下命令,创建资源。

terrform apply

根据提示输入yes 创建资源,返回信息如下所示:

zzzzy.l@zzzzys-MacBook-Pro demo % terraform apply
╷
│ Warning: Provider development overrides are in effect
│ 
│ The following provider development overrides are set in the CLI
│ configuration:
│  - myorg/cloud in /Users/zzzzy.l/project/tce/terraform-provider-tencent
│ 
│ The behavior may therefore not match any released version of the provider and
│ applying changes may cause the state to become incompatible with published
│ releases.
╵
 
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:
 
  # cloud_vpc.cloud_vpc-bppk will be created
  + resource "cloud_vpc" "cloud_vpc-bppk" {
      + assistant_cidrs        = (known after apply)
      + cidr_block             = "10.0.0.0/16"
      + create_time            = (known after apply)
      + default_route_table_id = (known after apply)
      + dns_servers            = (known after apply)
      + docker_assistant_cidrs = (known after apply)
      + id                     = (known after apply)
      + is_default             = (known after apply)
      + is_multicast           = false
      + name                   = "ci-temp-test-updated"
      + tags                   = {
          + "test" = "test"
        }
      + vpc_id                 = (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
 
cloud_vpc.cloud_vpc-bppk: Creating...
cloud_vpc.cloud_vpc-bppk: Creation complete after 3s [id=vpc-4lpij1lx]
 
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

执行完毕后,您可以在云平台控制台查看创建的资源。

销毁资源

执行以下命令,销毁资源。

terraform destroy

返回信息如下所示:

zzzzy.l@zzzzys-MacBook-Pro demo % terraform destroy
╷
│ Warning: Provider development overrides are in effect
│ 
│ The following provider development overrides are set in the CLI
│ configuration:
│  - myorg/cloud in /Users/zzzzy.l/project/tce/terraform-provider-tencent
│ 
│ The behavior may therefore not match any released version of the provider and
│ applying changes may cause the state to become incompatible with published
│ releases.
╵
cloud_vpc.cloud_vpc-bppk: Refreshing state... [id=vpc-4lpij1lx]
 
Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
  - destroy
 
Terraform will perform the following actions:
 
  # cloud_vpc.cloud_vpc-bppk will be destroyed
  - resource "cloud_vpc" "cloud_vpc-bppk" {
      - assistant_cidrs        = [] -> null
      - cidr_block             = "10.0.0.0/16" -> null
      - create_time            = "2024-02-01 14:52:08" -> null
      - default_route_table_id = "rtb-0xbqrr6u" -> null
      - dns_servers            = [
          - "183.60.82.98",
          - "183.60.83.19",
        ] -> null
      - docker_assistant_cidrs = [] -> null
      - id                     = "vpc-4lpij1lx" -> null
      - is_default             = false -> null
      - is_multicast           = false -> null
      - name                   = "ci-temp-test-updated" -> null
      - tags                   = {
          - "test" = "test"
        } -> null
      - vpc_id                 = "vpc-4lpij1lx" -> null
    }
 
Plan: 0 to add, 0 to change, 1 to destroy.
 
Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.
 
  Enter a value: yes
 
cloud_vpc.cloud_vpc-bppk: Destroying... [id=vpc-4lpij1lx]
cloud_vpc.cloud_vpc-bppk: Destruction complete after 3s
 
Destroy complete! Resources: 1 destroyed.