Appearance
Aliyun 创建 VPC
https://registry.terraform.io/providers/aliyun/alicloud/latest
编写配置文件
使用 HCL 编写配置文件
配置Terraform身份认证
- windows 为例
- 在桌面右键单击此电脑,选择属性 -> 高级系统设置 -> 环境变量 -> 系统变量/用户变量。
- 在系统变量/用户变量中,单击新建,创建以下环境变量。
创建 main.tf
D:\project\IaC\terraform-demo
(base) λ cat main.tf
resource "alicloud_vpc" "main" {
# VPC名称
vpc_name = "alicloud"
# VPC地址块
cidr_block = "10.1.0.0/21"
}
resource "alicloud_vswitch" "main" {
# VPC ID
vpc_id = alicloud_vpc.main.id
# 交换机地址块
cidr_block = "10.1.0.0/24"
# 可用区
zone_id = "cn-hangzhou-b"
# 资源依赖,会优先创建该依赖资源
depends_on = [alicloud_vpc.main]
}
创建 provider.tf
(base) λ cat provider.tf
terraform {
required_providers {
alicloud = {
source = "aliyun/alicloud"
version = "1.255.0"
}
}
}
provider "alicloud" {
region = "cn-hangzhou"
}
部署
初始化
terraform init
自动安装aliyun provider
查看变更计划
terraform plan
(base) λ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:
# alicloud_vpc.main will be created
+ resource "alicloud_vpc" "main" {
+ cidr_block = "10.1.0.0/21"
+ create_time = (known after apply)
+ dns_hostname_status = (known after apply)
+ id = (known after apply)
+ ipv6_cidr_block = (known after apply)
+ ipv6_cidr_blocks = (known after apply)
+ name = (known after apply)
+ region_id = (known after apply)
+ resource_group_id = (known after apply)
+ route_table_id = (known after apply)
+ router_id = (known after apply)
+ router_table_id = (known after apply)
+ secondary_cidr_blocks = (known after apply)
+ status = (known after apply)
+ system_route_table_route_propagation_enable = (known after apply)
+ user_cidrs = (known after apply)
+ vpc_name = "alicloud"
}
# alicloud_vswitch.main will be created
+ resource "alicloud_vswitch" "main" {
+ availability_zone = (known after apply)
+ cidr_block = "10.1.0.0/24"
+ create_time = (known after apply)
+ id = (known after apply)
+ ipv6_cidr_block = (known after apply)
+ ipv6_cidr_block_mask = (known after apply)
+ name = (known after apply)
+ status = (known after apply)
+ vpc_id = (known after apply)
+ vswitch_name = (known after apply)
+ zone_id = "cn-hangzhou-b"
}
Plan: 2 to add, 0 to change, 0 to destroy.
- terraform apply 应用更改(按 y 确认)
(base) λterraform apply
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_vpc.main will be created
+ resource "alicloud_vpc" "main" {
+ cidr_block = "10.1.0.0/21"
+ create_time = (known after apply)
+ dns_hostname_status = (known after apply)
+ id = (known after apply)
+ ipv6_cidr_block = (known after apply)
+ ipv6_cidr_blocks = (known after apply)
+ name = (known after apply)
+ region_id = (known after apply)
+ resource_group_id = (known after apply)
+ route_table_id = (known after apply)
+ router_id = (known after apply)
+ router_table_id = (known after apply)
+ secondary_cidr_blocks = (known after apply)
+ status = (known after apply)
+ system_route_table_route_propagation_enable = (known after apply)
+ user_cidrs = (known after apply)
+ vpc_name = "alicloud"
}
# alicloud_vswitch.main will be created
+ resource "alicloud_vswitch" "main" {
+ availability_zone = (known after apply)
+ cidr_block = "10.1.0.0/24"
+ create_time = (known after apply)
+ id = (known after apply)
+ ipv6_cidr_block = (known after apply)
+ ipv6_cidr_block_mask = (known after apply)
+ name = (known after apply)
+ status = (known after apply)
+ vpc_id = (known after apply)
+ vswitch_name = (known after apply)
+ zone_id = "cn-hangzhou-b"
}
Plan: 2 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_vpc.main: Creating...
alicloud_vpc.main: Creation complete after 8s [id=vpc-bp1rrcwl3kfm0xs0riivu]
alicloud_vswitch.main: Creating...
alicloud_vswitch.main: Creation complete after 5s [id=vsw-bp1kqpb1q3sx8sshi2bik]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
- 查看配置
(base) λterraform show
# alicloud_vpc.main:
resource "alicloud_vpc" "main" {
cidr_block = "10.1.0.0/21"
classic_link_enabled = false
create_time = "2025-08-01T05:07:55Z"
description = null
dns_hostname_status = "DISABLED"
enable_ipv6 = false
id = "vpc-bp1rrcwl3kfm0xs0riivu"
ipv6_cidr_block = null
ipv6_cidr_blocks = []
name = "alicloud"
region_id = "cn-hangzhou"
resource_group_id = "rg-acfmwwni352kcma"
route_table_id = "vtb-bp1t9fnwgor3qewykfvvg"
router_id = "vrt-bp1byhnq8fqzo2nozdtsr"
router_table_id = "vtb-bp1t9fnwgor3qewykfvvg"
secondary_cidr_blocks = []
status = "Available"
system_route_table_description = null
system_route_table_name = null
system_route_table_route_propagation_enable = true
user_cidrs = []
vpc_name = "alicloud"
}
# alicloud_vswitch.main:
resource "alicloud_vswitch" "main" {
availability_zone = "cn-hangzhou-b"
cidr_block = "10.1.0.0/24"
create_time = "2025-08-01T05:08:02Z"
description = null
id = "vsw-bp1kqpb1q3sx8sshi2bik"
ipv6_cidr_block = null
name = null
status = "Available"
vpc_id = "vpc-bp1rrcwl3kfm0xs0riivu"
vswitch_name = null
zone_id = "cn-hangzhou-b"
}
控制台
- 阿里云控制台展示
销毁 terraform destroy
(base) λterraform destroy
alicloud_vpc.main: Refreshing state... [id=vpc-bp1rrcwl3kfm0xs0riivu]
alicloud_vswitch.main: Refreshing state... [id=vsw-bp1kqpb1q3sx8sshi2bik]
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:
# alicloud_vpc.main will be destroyed
- resource "alicloud_vpc" "main" {
- cidr_block = "10.1.0.0/21" -> null
- classic_link_enabled = false -> null
- create_time = "2025-08-01T05:07:55Z" -> null
- dns_hostname_status = "DISABLED" -> null
- enable_ipv6 = false -> null
- id = "vpc-bp1rrcwl3kfm0xs0riivu" -> null
- ipv6_cidr_blocks = [] -> null
- name = "alicloud" -> null
- region_id = "cn-hangzhou" -> null
- resource_group_id = "rg-acfmwwni352kcma" -> null
- route_table_id = "vtb-bp1t9fnwgor3qewykfvvg" -> null
- router_id = "vrt-bp1byhnq8fqzo2nozdtsr" -> null
- router_table_id = "vtb-bp1t9fnwgor3qewykfvvg" -> null
- secondary_cidr_blocks = [] -> null
- status = "Available" -> null
- system_route_table_route_propagation_enable = true -> null
- tags = {} -> null
- user_cidrs = [] -> null
- vpc_name = "alicloud" -> null
# (4 unchanged attributes hidden)
}
# alicloud_vswitch.main will be destroyed
- resource "alicloud_vswitch" "main" {
- availability_zone = "cn-hangzhou-b" -> null
- cidr_block = "10.1.0.0/24" -> null
- create_time = "2025-08-01T05:08:02Z" -> null
- id = "vsw-bp1kqpb1q3sx8sshi2bik" -> null
name = null
- status = "Available" -> null
- tags = {} -> null
- vpc_id = "vpc-bp1rrcwl3kfm0xs0riivu" -> null
- zone_id = "cn-hangzhou-b" -> null
# (3 unchanged attributes hidden)
}
Plan: 0 to add, 0 to change, 2 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
alicloud_vswitch.main: Destroying... [id=vsw-bp1kqpb1q3sx8sshi2bik]
alicloud_vswitch.main: Destruction complete after 5s
alicloud_vpc.main: Destroying... [id=vpc-bp1rrcwl3kfm0xs0riivu]
alicloud_vpc.main: Destruction complete after 6s
Destroy complete! Resources: 2 destroyed.