1. 创建RAM
注意,账户下必须有100元,才能创建,如果不足,提示如下
- 访问ram控制
2. 开通AK/SK
复制这个id和secret,下面要用到
- 授权
3.快速构建
3.1 快速编写✅
vi main.tf
yaml
#定义云厂商
provider "alicloud" {
region = "cn-shanghai"
access_key = "xxx" #修改成自己的ak
secret_key = "xxx" #修改成自己的sk
}
#创建vpc
resource "alicloud_vpc" "vpc" {
vpc_name = "vpc_1"
cidr_block = "10.0.0.0/16"
}
# 创建vswitch
# alicloud_vswitch是阿里云的资源字段,vsw_1字段是tf文件中的自定义唯一资源名称,vswitch_name字段是在阿里云上的自定义备注名
resource "alicloud_vswitch" "vsw_1" {
vswitch_name = "vsw_aliyun1"
vpc_id = alicloud_vpc.vpc.id
cidr_block = "10.0.0.0/24"
zone_id = "cn-shanghai-b"
}
#新建安全组
resource "alicloud_security_group" "nsg1" {
security_group_name = "lyc_aliyun_nsg1"
vpc_id = alicloud_vpc.vpc.id
}
#将nsg_rule1、nsg_rule2加入安全组lyc_aliyun_nsg1中
resource "alicloud_security_group_rule" "nsg_rule1" {
type = "ingress"
ip_protocol = "tcp"
nic_type = "intranet"
policy = "accept"
port_range = "1/65535"
priority = 1
security_group_id = alicloud_security_group.nsg1.id
cidr_ip = "0.0.0.0/0"
}
#创建ECS实例
resource "alicloud_instance" "instance" {
# cn-shanghai
availability_zone = "cn-shanghai-b"
security_groups = ["${alicloud_security_group.nsg1.id}"]
instance_type = "ecs.e-c1m1.large" #若实例规格下线,请使用目前售卖中的实例规格
system_disk_category = "cloud_essd" #若磁盘规格下线,请使用目前售卖中的实例规格
image_id = "centos_7_9_x64_20G_alibase_20220824.vhd"
instance_name = "lyc-test"
vswitch_id = alicloud_vswitch.vsw_1.id
internet_max_bandwidth_out = 1
password = "5jejYWzSjZhWQc7G22"
}
#定义云厂商
provider "alicloud" {
region = "cn-shanghai"
access_key = "xxx" #修改成自己的ak
secret_key = "xxx" #修改成自己的sk
}
#创建vpc
resource "alicloud_vpc" "vpc" {
vpc_name = "vpc_1"
cidr_block = "10.0.0.0/16"
}
# 创建vswitch
# alicloud_vswitch是阿里云的资源字段,vsw_1字段是tf文件中的自定义唯一资源名称,vswitch_name字段是在阿里云上的自定义备注名
resource "alicloud_vswitch" "vsw_1" {
vswitch_name = "vsw_aliyun1"
vpc_id = alicloud_vpc.vpc.id
cidr_block = "10.0.0.0/24"
zone_id = "cn-shanghai-b"
}
#新建安全组
resource "alicloud_security_group" "nsg1" {
security_group_name = "lyc_aliyun_nsg1"
vpc_id = alicloud_vpc.vpc.id
}
#将nsg_rule1、nsg_rule2加入安全组lyc_aliyun_nsg1中
resource "alicloud_security_group_rule" "nsg_rule1" {
type = "ingress"
ip_protocol = "tcp"
nic_type = "intranet"
policy = "accept"
port_range = "1/65535"
priority = 1
security_group_id = alicloud_security_group.nsg1.id
cidr_ip = "0.0.0.0/0"
}
#创建ECS实例
resource "alicloud_instance" "instance" {
# cn-shanghai
availability_zone = "cn-shanghai-b"
security_groups = ["${alicloud_security_group.nsg1.id}"]
instance_type = "ecs.e-c1m1.large" #若实例规格下线,请使用目前售卖中的实例规格
system_disk_category = "cloud_essd" #若磁盘规格下线,请使用目前售卖中的实例规格
image_id = "centos_7_9_x64_20G_alibase_20220824.vhd"
instance_name = "lyc-test"
vswitch_id = alicloud_vswitch.vsw_1.id
internet_max_bandwidth_out = 1
password = "5jejYWzSjZhWQc7G22"
}
3.2 快速创建✅
╷
│ Warning: Additional provider information from registry
│
│ The remote registry returned warnings for registry.terraform.io/hashicorp/alicloud:
│ - For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers.
╵
Terraform has been successfully initialized!
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
commands will detect it and remind you to do so if necessary.
╷
│ Warning: Additional provider information from registry
│
│ The remote registry returned warnings for registry.terraform.io/hashicorp/alicloud:
│ - For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers.
╵
Terraform has been successfully initialized!
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
commands will detect it and remind you to do so if necessary.
3.3 查看资源✅
bash
$ 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_instance.instance will be created
+ resource "alicloud_instance" "instance" {
+ availability_zone = "cn-shanghai-b"
+ cpu = (known after apply)
+ create_time = (known after apply)
+ credit_specification = (known after apply)
+ deletion_protection = false
+ deployment_set_group_no = (known after apply)
+ description = (known after apply)
+ dry_run = false
+ enable_jumbo_frame = (known after apply)
+ expired_time = (known after apply)
+ host_name = (known after apply)
+ http_endpoint = (known after apply)
+ http_put_response_hop_limit = (known after apply)
+ http_tokens = (known after apply)
+ id = (known after apply)
+ image_id = "centos_7_9_x64_20G_alibase_20220824.vhd"
+ instance_charge_type = (known after apply)
+ instance_name = "lyc-kevin"
+ instance_type = "ecs.e-c1m1.large"
+ internet_charge_type = (known after apply)
+ internet_max_bandwidth_in = (known after apply)
+ internet_max_bandwidth_out = 1
+ io_optimized = (known after apply)
+ ipv6_address_count = (known after apply)
+ ipv6_addresses = (known after apply)
+ key_name = (known after apply)
+ maintenance_action = (known after apply)
+ memory = (known after apply)
+ network_interface_id = (known after apply)
+ network_interface_traffic_mode = (known after apply)
+ os_name = (known after apply)
+ os_type = (known after apply)
+ password = (sensitive value)
+ period = (known after apply)
+ primary_ip_address = (known after apply)
+ private_ip = (known after apply)
+ private_pool_options_id = (known after apply)
+ private_pool_options_match_criteria = (known after apply)
+ public_ip = (known after apply)
+ resource_group_id = (known after apply)
+ role_name = (known after apply)
+ secondary_private_ip_address_count = (known after apply)
+ secondary_private_ips = (known after apply)
+ security_enhancement_strategy = (known after apply)
+ security_groups = (known after apply)
+ spot_duration = (known after apply)
+ spot_price_limit = (known after apply)
+ spot_strategy = (known after apply)
+ start_time = (known after apply)
+ status = (known after apply)
+ stopped_mode = (known after apply)
+ subnet_id = (known after apply)
+ system_disk_category = "cloud_essd"
+ system_disk_description = (known after apply)
+ system_disk_encrypted = (known after apply)
+ system_disk_id = (known after apply)
+ system_disk_kms_key_id = (known after apply)
+ system_disk_name = (known after apply)
+ system_disk_performance_level = (known after apply)
+ system_disk_size = (known after apply)
+ volume_tags = (known after apply)
+ vpc_id = (known after apply)
+ vswitch_id = (known after apply)
+ image_options (known after apply)
+ network_interfaces (known after apply)
}
# alicloud_security_group.nsg1 will be created
+ resource "alicloud_security_group" "nsg1" {
+ create_time = (known after apply)
+ id = (known after apply)
+ inner_access = (known after apply)
+ inner_access_policy = (known after apply)
+ name = (known after apply)
+ security_group_name = "lyc_aliyun_nsg1"
+ security_group_type = (known after apply)
+ vpc_id = (known after apply)
}
# alicloud_security_group_rule.nsg_rule1 will be created
+ resource "alicloud_security_group_rule" "nsg_rule1" {
+ cidr_ip = "0.0.0.0/0"
+ id = (known after apply)
+ ip_protocol = "tcp"
+ nic_type = "intranet"
+ policy = "accept"
+ port_range = "1/65535"
+ prefix_list_id = (known after apply)
+ priority = 1
+ security_group_id = (known after apply)
+ security_group_rule_id = (known after apply)
+ type = "ingress"
}
# alicloud_vpc.vpc will be created
+ resource "alicloud_vpc" "vpc" {
+ cidr_block = "10.0.0.0/16"
+ 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 = "vpc_1"
}
# alicloud_vswitch.vsw_1 will be created
+ resource "alicloud_vswitch" "vsw_1" {
+ availability_zone = (known after apply)
+ cidr_block = "10.0.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 = "vsw_aliyun1"
+ zone_id = "cn-shanghai-b"
}
Plan: 5 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.
$ 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_instance.instance will be created
+ resource "alicloud_instance" "instance" {
+ availability_zone = "cn-shanghai-b"
+ cpu = (known after apply)
+ create_time = (known after apply)
+ credit_specification = (known after apply)
+ deletion_protection = false
+ deployment_set_group_no = (known after apply)
+ description = (known after apply)
+ dry_run = false
+ enable_jumbo_frame = (known after apply)
+ expired_time = (known after apply)
+ host_name = (known after apply)
+ http_endpoint = (known after apply)
+ http_put_response_hop_limit = (known after apply)
+ http_tokens = (known after apply)
+ id = (known after apply)
+ image_id = "centos_7_9_x64_20G_alibase_20220824.vhd"
+ instance_charge_type = (known after apply)
+ instance_name = "lyc-kevin"
+ instance_type = "ecs.e-c1m1.large"
+ internet_charge_type = (known after apply)
+ internet_max_bandwidth_in = (known after apply)
+ internet_max_bandwidth_out = 1
+ io_optimized = (known after apply)
+ ipv6_address_count = (known after apply)
+ ipv6_addresses = (known after apply)
+ key_name = (known after apply)
+ maintenance_action = (known after apply)
+ memory = (known after apply)
+ network_interface_id = (known after apply)
+ network_interface_traffic_mode = (known after apply)
+ os_name = (known after apply)
+ os_type = (known after apply)
+ password = (sensitive value)
+ period = (known after apply)
+ primary_ip_address = (known after apply)
+ private_ip = (known after apply)
+ private_pool_options_id = (known after apply)
+ private_pool_options_match_criteria = (known after apply)
+ public_ip = (known after apply)
+ resource_group_id = (known after apply)
+ role_name = (known after apply)
+ secondary_private_ip_address_count = (known after apply)
+ secondary_private_ips = (known after apply)
+ security_enhancement_strategy = (known after apply)
+ security_groups = (known after apply)
+ spot_duration = (known after apply)
+ spot_price_limit = (known after apply)
+ spot_strategy = (known after apply)
+ start_time = (known after apply)
+ status = (known after apply)
+ stopped_mode = (known after apply)
+ subnet_id = (known after apply)
+ system_disk_category = "cloud_essd"
+ system_disk_description = (known after apply)
+ system_disk_encrypted = (known after apply)
+ system_disk_id = (known after apply)
+ system_disk_kms_key_id = (known after apply)
+ system_disk_name = (known after apply)
+ system_disk_performance_level = (known after apply)
+ system_disk_size = (known after apply)
+ volume_tags = (known after apply)
+ vpc_id = (known after apply)
+ vswitch_id = (known after apply)
+ image_options (known after apply)
+ network_interfaces (known after apply)
}
# alicloud_security_group.nsg1 will be created
+ resource "alicloud_security_group" "nsg1" {
+ create_time = (known after apply)
+ id = (known after apply)
+ inner_access = (known after apply)
+ inner_access_policy = (known after apply)
+ name = (known after apply)
+ security_group_name = "lyc_aliyun_nsg1"
+ security_group_type = (known after apply)
+ vpc_id = (known after apply)
}
# alicloud_security_group_rule.nsg_rule1 will be created
+ resource "alicloud_security_group_rule" "nsg_rule1" {
+ cidr_ip = "0.0.0.0/0"
+ id = (known after apply)
+ ip_protocol = "tcp"
+ nic_type = "intranet"
+ policy = "accept"
+ port_range = "1/65535"
+ prefix_list_id = (known after apply)
+ priority = 1
+ security_group_id = (known after apply)
+ security_group_rule_id = (known after apply)
+ type = "ingress"
}
# alicloud_vpc.vpc will be created
+ resource "alicloud_vpc" "vpc" {
+ cidr_block = "10.0.0.0/16"
+ 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 = "vpc_1"
}
# alicloud_vswitch.vsw_1 will be created
+ resource "alicloud_vswitch" "vsw_1" {
+ availability_zone = (known after apply)
+ cidr_block = "10.0.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 = "vsw_aliyun1"
+ zone_id = "cn-shanghai-b"
}
Plan: 5 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.
3.4 查看结果✅
bash
$Hsuin: D:/Terraform_project/tf_aliyun ❯ terraform apply
alicloud_vpc.vpc: Refreshing state... [id=vpc-uf6u84s6ut3ffeiingy6j]
alicloud_security_group.nsg1: Refreshing state... [id=sg-uf6db0gazehmzkti4h9y]
alicloud_vswitch.vsw_1: Refreshing state... [id=vsw-uf63o8tj19phndxogjzge]
alicloud_security_group_rule.nsg_rule1: Refreshing state... [id=sg-uf6db0gazehmzkti4h9y:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1]
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_instance.instance will be created
+ resource "alicloud_instance" "instance" {
+ availability_zone = "cn-shanghai-b"
+ cpu = (known after apply)
+ create_time = (known after apply)
+ credit_specification = (known after apply)
+ deletion_protection = false
+ deployment_set_group_no = (known after apply)
+ description = (known after apply)
+ dry_run = false
+ enable_jumbo_frame = (known after apply)
+ expired_time = (known after apply)
+ host_name = (known after apply)
+ http_endpoint = (known after apply)
+ http_put_response_hop_limit = (known after apply)
+ http_tokens = (known after apply)
+ id = (known after apply)
+ image_id = "centos_7_9_x64_20G_alibase_20220824.vhd"
+ instance_charge_type = (known after apply)
+ instance_name = "lyc-kevin"
+ instance_type = "ecs.e-c1m1.large"
+ internet_charge_type = (known after apply)
+ internet_max_bandwidth_in = (known after apply)
+ internet_max_bandwidth_out = 1
+ io_optimized = (known after apply)
+ ipv6_address_count = (known after apply)
+ ipv6_addresses = (known after apply)
+ key_name = (known after apply)
+ maintenance_action = (known after apply)
+ memory = (known after apply)
+ network_interface_id = (known after apply)
+ network_interface_traffic_mode = (known after apply)
+ os_name = (known after apply)
+ os_type = (known after apply)
+ password = (sensitive value)
+ period = (known after apply)
+ primary_ip_address = (known after apply)
+ private_ip = (known after apply)
+ private_pool_options_id = (known after apply)
+ private_pool_options_match_criteria = (known after apply)
+ public_ip = (known after apply)
+ resource_group_id = (known after apply)
+ role_name = (known after apply)
+ secondary_private_ip_address_count = (known after apply)
+ secondary_private_ips = (known after apply)
+ security_enhancement_strategy = (known after apply)
+ security_groups = [
+ "sg-uf6db0gazehmzkti4h9y",
]
+ spot_duration = (known after apply)
+ spot_price_limit = (known after apply)
+ spot_strategy = (known after apply)
+ start_time = (known after apply)
+ status = (known after apply)
+ stopped_mode = (known after apply)
+ subnet_id = (known after apply)
+ system_disk_category = "cloud_essd"
+ system_disk_description = (known after apply)
+ system_disk_encrypted = (known after apply)
+ system_disk_id = (known after apply)
+ system_disk_kms_key_id = (known after apply)
+ system_disk_name = (known after apply)
+ system_disk_performance_level = (known after apply)
+ system_disk_size = (known after apply)
+ volume_tags = (known after apply)
+ vpc_id = (known after apply)
+ vswitch_id = "vsw-uf63o8tj19phndxogjzge"
+ image_options (known after apply)
+ network_interfaces (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 (如果没有问题,输入yes,开始创建)
alicloud_instance.instance: Creating...
alicloud_instance.instance: Still creating... [00m10s elapsed]
alicloud_instance.instance: Creation complete after 16s [id=i-uf69luq0kzpcyg9c60qy]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
$Hsuin: D:/Terraform_project/tf_aliyun ❯ terraform apply
alicloud_vpc.vpc: Refreshing state... [id=vpc-uf6u84s6ut3ffeiingy6j]
alicloud_security_group.nsg1: Refreshing state... [id=sg-uf6db0gazehmzkti4h9y]
alicloud_vswitch.vsw_1: Refreshing state... [id=vsw-uf63o8tj19phndxogjzge]
alicloud_security_group_rule.nsg_rule1: Refreshing state... [id=sg-uf6db0gazehmzkti4h9y:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1]
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_instance.instance will be created
+ resource "alicloud_instance" "instance" {
+ availability_zone = "cn-shanghai-b"
+ cpu = (known after apply)
+ create_time = (known after apply)
+ credit_specification = (known after apply)
+ deletion_protection = false
+ deployment_set_group_no = (known after apply)
+ description = (known after apply)
+ dry_run = false
+ enable_jumbo_frame = (known after apply)
+ expired_time = (known after apply)
+ host_name = (known after apply)
+ http_endpoint = (known after apply)
+ http_put_response_hop_limit = (known after apply)
+ http_tokens = (known after apply)
+ id = (known after apply)
+ image_id = "centos_7_9_x64_20G_alibase_20220824.vhd"
+ instance_charge_type = (known after apply)
+ instance_name = "lyc-kevin"
+ instance_type = "ecs.e-c1m1.large"
+ internet_charge_type = (known after apply)
+ internet_max_bandwidth_in = (known after apply)
+ internet_max_bandwidth_out = 1
+ io_optimized = (known after apply)
+ ipv6_address_count = (known after apply)
+ ipv6_addresses = (known after apply)
+ key_name = (known after apply)
+ maintenance_action = (known after apply)
+ memory = (known after apply)
+ network_interface_id = (known after apply)
+ network_interface_traffic_mode = (known after apply)
+ os_name = (known after apply)
+ os_type = (known after apply)
+ password = (sensitive value)
+ period = (known after apply)
+ primary_ip_address = (known after apply)
+ private_ip = (known after apply)
+ private_pool_options_id = (known after apply)
+ private_pool_options_match_criteria = (known after apply)
+ public_ip = (known after apply)
+ resource_group_id = (known after apply)
+ role_name = (known after apply)
+ secondary_private_ip_address_count = (known after apply)
+ secondary_private_ips = (known after apply)
+ security_enhancement_strategy = (known after apply)
+ security_groups = [
+ "sg-uf6db0gazehmzkti4h9y",
]
+ spot_duration = (known after apply)
+ spot_price_limit = (known after apply)
+ spot_strategy = (known after apply)
+ start_time = (known after apply)
+ status = (known after apply)
+ stopped_mode = (known after apply)
+ subnet_id = (known after apply)
+ system_disk_category = "cloud_essd"
+ system_disk_description = (known after apply)
+ system_disk_encrypted = (known after apply)
+ system_disk_id = (known after apply)
+ system_disk_kms_key_id = (known after apply)
+ system_disk_name = (known after apply)
+ system_disk_performance_level = (known after apply)
+ system_disk_size = (known after apply)
+ volume_tags = (known after apply)
+ vpc_id = (known after apply)
+ vswitch_id = "vsw-uf63o8tj19phndxogjzge"
+ image_options (known after apply)
+ network_interfaces (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 (如果没有问题,输入yes,开始创建)
alicloud_instance.instance: Creating...
alicloud_instance.instance: Still creating... [00m10s elapsed]
alicloud_instance.instance: Creation complete after 16s [id=i-uf69luq0kzpcyg9c60qy]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
3.5 删除资源✅
bash
$Hsuin: D:/Terraform_project/tf_aliyun ❯ terraform destroy
alicloud_vpc.vpc: Refreshing state... [id=vpc-uf6u84s6ut3ffeiingy6j]
alicloud_security_group.nsg1: Refreshing state... [id=sg-uf6db0gazehmzkti4h9y]
alicloud_vswitch.vsw_1: Refreshing state... [id=vsw-uf63o8tj19phndxogjzge]
alicloud_security_group_rule.nsg_rule1: Refreshing state... [id=sg-uf6db0gazehmzkti4h9y:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1]
alicloud_instance.instance: Refreshing state... [id=i-uf69luq0kzpcyg9c60qy]
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_instance.instance will be destroyed
- resource "alicloud_instance" "instance" {
- availability_zone = "cn-shanghai-b" -> null
- cpu = 2 -> null
- create_time = "2025-07-09T06:42Z" -> null
- deletion_protection = false -> null
- dry_run = false -> null
- enable_jumbo_frame = false -> null
- expired_time = "2099-12-31T15:59Z" -> null
- host_name = "iZuf69luq0kzpcyg9c60qyZ" -> null
- http_put_response_hop_limit = 0 -> null
- id = "i-uf69luq0kzpcyg9c60qy" -> null
- image_id = "centos_7_9_x64_20G_alibase_20220824.vhd" -> null
- instance_charge_type = "PostPaid" -> null
- instance_name = "lyc-test" -> null
- instance_type = "ecs.e-c1m1.large" -> null
- internet_charge_type = "PayByTraffic" -> null
- internet_max_bandwidth_in = 200 -> null
- internet_max_bandwidth_out = 1 -> null
- ipv6_address_count = 0 -> null
- ipv6_addresses = [] -> null
- maintenance_action = "AutoRecover" -> null
- maintenance_notify = false -> null
- memory = 2048 -> null
- network_interface_id = "eni-uf69luq0kzpcyg964tw6" -> null
- network_interface_traffic_mode = "Standard" -> null
- os_name = "CentOS 7.9 64位" -> null
- os_type = "linux" -> null
- password = (sensitive value) -> null
- primary_ip_address = "10.0.0.119" -> null
- private_ip = "10.0.0.119" -> null
- private_pool_options_match_criteria = "None" -> null
- public_ip = "139.224.5.37" -> null
- queue_pair_number = 0 -> null
- secondary_private_ip_address_count = 0 -> null
- secondary_private_ips = [] -> null
- security_groups = [
- "sg-uf6db0gazehmzkti4h9y",
] -> null
- spot_duration = 0 -> null
- spot_price_limit = 0 -> null
- spot_strategy = "NoSpot" -> null
- start_time = "2025-07-09T06:42Z" -> null
- status = "Running" -> null
- stopped_mode = "Not-applicable" -> null
- subnet_id = "vsw-uf63o8tj19phndxogjzge" -> null
- system_disk_bursting_enabled = false -> null
- system_disk_category = "cloud_essd" -> null
- system_disk_encrypted = false -> null
- system_disk_id = "d-uf69luq0kzpcyg9b10dj" -> null
- system_disk_performance_level = "PL1" -> null
- system_disk_provisioned_iops = 0 -> null
- system_disk_size = 40 -> null
- tags = {} -> null
- volume_tags = {} -> null
- vpc_id = "vpc-uf6u84s6ut3ffeiingy6j" -> null
- vswitch_id = "vsw-uf63o8tj19phndxogjzge" -> null
# (18 unchanged attributes hidden)
- image_options {
- login_as_non_root = false -> null
}
}
# alicloud_security_group.nsg1 will be destroyed
- resource "alicloud_security_group" "nsg1" {
- create_time = "2025-07-09T06:38:17Z" -> null
- id = "sg-uf6db0gazehmzkti4h9y" -> null
- inner_access = true -> null
- inner_access_policy = "Accept" -> null
- name = "lyc_aliyun_nsg1" -> null
- security_group_name = "lyc_aliyun_nsg1" -> null
- security_group_type = "normal" -> null
- tags = {} -> null
- vpc_id = "vpc-uf6u84s6ut3ffeiingy6j" -> null
# (2 unchanged attributes hidden)
}
# alicloud_security_group_rule.nsg_rule1 will be destroyed
- resource "alicloud_security_group_rule" "nsg_rule1" {
- cidr_ip = "0.0.0.0/0" -> null
- id = "sg-uf6db0gazehmzkti4h9y:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1" -> null
- ip_protocol = "tcp" -> null
- nic_type = "intranet" -> null
- policy = "accept" -> null
- port_range = "1/65535" -> null
- priority = 1 -> null
- security_group_id = "sg-uf6db0gazehmzkti4h9y" -> null
- security_group_rule_id = "sgr-uf60cgbi6u4q585j0ldq" -> null
- type = "ingress" -> null
# (5 unchanged attributes hidden)
}
# alicloud_vpc.vpc will be destroyed
- resource "alicloud_vpc" "vpc" {
- cidr_block = "10.0.0.0/16" -> null
- classic_link_enabled = false -> null
- create_time = "2025-07-09T06:38:11Z" -> null
- dns_hostname_status = "DISABLED" -> null
- enable_ipv6 = false -> null
- id = "vpc-uf6u84s6ut3ffeiingy6j" -> null
- ipv6_cidr_blocks = [] -> null
- name = "vpc_1" -> null
- region_id = "cn-shanghai" -> null
- resource_group_id = "rg-acfm2qdjwer4h7y" -> null
- route_table_id = "vtb-uf6wczszr08ua5i1z9cst" -> null
- router_id = "vrt-uf6vav8bgfwoz624uc4jw" -> null
- router_table_id = "vtb-uf6wczszr08ua5i1z9cst" -> null
- secondary_cidr_blocks = [] -> null
- status = "Available" -> null
- system_route_table_route_propagation_enable = true -> null
- tags = {} -> null
- user_cidrs = [] -> null
- vpc_name = "vpc_1" -> null
# (4 unchanged attributes hidden)
}
# alicloud_vswitch.vsw_1 will be destroyed
- resource "alicloud_vswitch" "vsw_1" {
- availability_zone = "cn-shanghai-b" -> null
- cidr_block = "10.0.0.0/24" -> null
- create_time = "2025-07-09T06:38:17Z" -> null
- id = "vsw-uf63o8tj19phndxogjzge" -> null
- name = "vsw_aliyun1" -> null
- status = "Available" -> null
- tags = {} -> null
- vpc_id = "vpc-uf6u84s6ut3ffeiingy6j" -> null
- vswitch_name = "vsw_aliyun1" -> null
- zone_id = "cn-shanghai-b" -> null
# (2 unchanged attributes hidden)
}
Plan: 0 to add, 0 to change, 5 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_security_group_rule.nsg_rule1: Destroying... [id=sg-uf6db0gazehmzkti4h9y:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1]
alicloud_instance.instance: Destroying... [id=i-uf69luq0kzpcyg9c60qy]
alicloud_security_group_rule.nsg_rule1: Destruction complete after 1s
alicloud_instance.instance: Still destroying... [id=i-uf69luq0kzpcyg9c60qy, 00m10s elapsed]
alicloud_instance.instance: Destruction complete after 12s
alicloud_security_group.nsg1: Destroying... [id=sg-uf6db0gazehmzkti4h9y]
alicloud_vswitch.vsw_1: Destroying... [id=vsw-uf63o8tj19phndxogjzge]
alicloud_security_group.nsg1: Destruction complete after 0s
alicloud_vswitch.vsw_1: Destruction complete after 4s
alicloud_vpc.vpc: Destroying... [id=vpc-uf6u84s6ut3ffeiingy6j]
alicloud_vpc.vpc: Destruction complete after 5s
Destroy complete! Resources: 5 destroyed.
$Hsuin: D:/Terraform_project/tf_aliyun ❯ terraform destroy
alicloud_vpc.vpc: Refreshing state... [id=vpc-uf6u84s6ut3ffeiingy6j]
alicloud_security_group.nsg1: Refreshing state... [id=sg-uf6db0gazehmzkti4h9y]
alicloud_vswitch.vsw_1: Refreshing state... [id=vsw-uf63o8tj19phndxogjzge]
alicloud_security_group_rule.nsg_rule1: Refreshing state... [id=sg-uf6db0gazehmzkti4h9y:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1]
alicloud_instance.instance: Refreshing state... [id=i-uf69luq0kzpcyg9c60qy]
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_instance.instance will be destroyed
- resource "alicloud_instance" "instance" {
- availability_zone = "cn-shanghai-b" -> null
- cpu = 2 -> null
- create_time = "2025-07-09T06:42Z" -> null
- deletion_protection = false -> null
- dry_run = false -> null
- enable_jumbo_frame = false -> null
- expired_time = "2099-12-31T15:59Z" -> null
- host_name = "iZuf69luq0kzpcyg9c60qyZ" -> null
- http_put_response_hop_limit = 0 -> null
- id = "i-uf69luq0kzpcyg9c60qy" -> null
- image_id = "centos_7_9_x64_20G_alibase_20220824.vhd" -> null
- instance_charge_type = "PostPaid" -> null
- instance_name = "lyc-test" -> null
- instance_type = "ecs.e-c1m1.large" -> null
- internet_charge_type = "PayByTraffic" -> null
- internet_max_bandwidth_in = 200 -> null
- internet_max_bandwidth_out = 1 -> null
- ipv6_address_count = 0 -> null
- ipv6_addresses = [] -> null
- maintenance_action = "AutoRecover" -> null
- maintenance_notify = false -> null
- memory = 2048 -> null
- network_interface_id = "eni-uf69luq0kzpcyg964tw6" -> null
- network_interface_traffic_mode = "Standard" -> null
- os_name = "CentOS 7.9 64位" -> null
- os_type = "linux" -> null
- password = (sensitive value) -> null
- primary_ip_address = "10.0.0.119" -> null
- private_ip = "10.0.0.119" -> null
- private_pool_options_match_criteria = "None" -> null
- public_ip = "139.224.5.37" -> null
- queue_pair_number = 0 -> null
- secondary_private_ip_address_count = 0 -> null
- secondary_private_ips = [] -> null
- security_groups = [
- "sg-uf6db0gazehmzkti4h9y",
] -> null
- spot_duration = 0 -> null
- spot_price_limit = 0 -> null
- spot_strategy = "NoSpot" -> null
- start_time = "2025-07-09T06:42Z" -> null
- status = "Running" -> null
- stopped_mode = "Not-applicable" -> null
- subnet_id = "vsw-uf63o8tj19phndxogjzge" -> null
- system_disk_bursting_enabled = false -> null
- system_disk_category = "cloud_essd" -> null
- system_disk_encrypted = false -> null
- system_disk_id = "d-uf69luq0kzpcyg9b10dj" -> null
- system_disk_performance_level = "PL1" -> null
- system_disk_provisioned_iops = 0 -> null
- system_disk_size = 40 -> null
- tags = {} -> null
- volume_tags = {} -> null
- vpc_id = "vpc-uf6u84s6ut3ffeiingy6j" -> null
- vswitch_id = "vsw-uf63o8tj19phndxogjzge" -> null
# (18 unchanged attributes hidden)
- image_options {
- login_as_non_root = false -> null
}
}
# alicloud_security_group.nsg1 will be destroyed
- resource "alicloud_security_group" "nsg1" {
- create_time = "2025-07-09T06:38:17Z" -> null
- id = "sg-uf6db0gazehmzkti4h9y" -> null
- inner_access = true -> null
- inner_access_policy = "Accept" -> null
- name = "lyc_aliyun_nsg1" -> null
- security_group_name = "lyc_aliyun_nsg1" -> null
- security_group_type = "normal" -> null
- tags = {} -> null
- vpc_id = "vpc-uf6u84s6ut3ffeiingy6j" -> null
# (2 unchanged attributes hidden)
}
# alicloud_security_group_rule.nsg_rule1 will be destroyed
- resource "alicloud_security_group_rule" "nsg_rule1" {
- cidr_ip = "0.0.0.0/0" -> null
- id = "sg-uf6db0gazehmzkti4h9y:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1" -> null
- ip_protocol = "tcp" -> null
- nic_type = "intranet" -> null
- policy = "accept" -> null
- port_range = "1/65535" -> null
- priority = 1 -> null
- security_group_id = "sg-uf6db0gazehmzkti4h9y" -> null
- security_group_rule_id = "sgr-uf60cgbi6u4q585j0ldq" -> null
- type = "ingress" -> null
# (5 unchanged attributes hidden)
}
# alicloud_vpc.vpc will be destroyed
- resource "alicloud_vpc" "vpc" {
- cidr_block = "10.0.0.0/16" -> null
- classic_link_enabled = false -> null
- create_time = "2025-07-09T06:38:11Z" -> null
- dns_hostname_status = "DISABLED" -> null
- enable_ipv6 = false -> null
- id = "vpc-uf6u84s6ut3ffeiingy6j" -> null
- ipv6_cidr_blocks = [] -> null
- name = "vpc_1" -> null
- region_id = "cn-shanghai" -> null
- resource_group_id = "rg-acfm2qdjwer4h7y" -> null
- route_table_id = "vtb-uf6wczszr08ua5i1z9cst" -> null
- router_id = "vrt-uf6vav8bgfwoz624uc4jw" -> null
- router_table_id = "vtb-uf6wczszr08ua5i1z9cst" -> null
- secondary_cidr_blocks = [] -> null
- status = "Available" -> null
- system_route_table_route_propagation_enable = true -> null
- tags = {} -> null
- user_cidrs = [] -> null
- vpc_name = "vpc_1" -> null
# (4 unchanged attributes hidden)
}
# alicloud_vswitch.vsw_1 will be destroyed
- resource "alicloud_vswitch" "vsw_1" {
- availability_zone = "cn-shanghai-b" -> null
- cidr_block = "10.0.0.0/24" -> null
- create_time = "2025-07-09T06:38:17Z" -> null
- id = "vsw-uf63o8tj19phndxogjzge" -> null
- name = "vsw_aliyun1" -> null
- status = "Available" -> null
- tags = {} -> null
- vpc_id = "vpc-uf6u84s6ut3ffeiingy6j" -> null
- vswitch_name = "vsw_aliyun1" -> null
- zone_id = "cn-shanghai-b" -> null
# (2 unchanged attributes hidden)
}
Plan: 0 to add, 0 to change, 5 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_security_group_rule.nsg_rule1: Destroying... [id=sg-uf6db0gazehmzkti4h9y:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1]
alicloud_instance.instance: Destroying... [id=i-uf69luq0kzpcyg9c60qy]
alicloud_security_group_rule.nsg_rule1: Destruction complete after 1s
alicloud_instance.instance: Still destroying... [id=i-uf69luq0kzpcyg9c60qy, 00m10s elapsed]
alicloud_instance.instance: Destruction complete after 12s
alicloud_security_group.nsg1: Destroying... [id=sg-uf6db0gazehmzkti4h9y]
alicloud_vswitch.vsw_1: Destroying... [id=vsw-uf63o8tj19phndxogjzge]
alicloud_security_group.nsg1: Destruction complete after 0s
alicloud_vswitch.vsw_1: Destruction complete after 4s
alicloud_vpc.vpc: Destroying... [id=vpc-uf6u84s6ut3ffeiingy6j]
alicloud_vpc.vpc: Destruction complete after 5s
Destroy complete! Resources: 5 destroyed.
- 验证TF是否为空
bash
$Hsuin: D:/Terraform_project/tf_aliyun ❯ terraform show
The state file is empty. No resources are represented.
#至此删除干净
$Hsuin: D:/Terraform_project/tf_aliyun ❯ terraform show
The state file is empty. No resources are represented.
#至此删除干净
❌ 注意
- 资源destroy过程中,由于时间及依赖等的先后顺序等,可能会存在无法一次性删除干净。如果仅是对整个Terraform进行destroy,可多次执行直到资源完全为0。
- 使用完成后,请删除资源,避免产生不必要的大量费用。
参考官方文档:
https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources
https://www.ethanzhang.xyz/2023/10/18/IaC基础设施即代码思想下的Terraform工具实战/