使用 terraform 删除特定资源,即 vm、nic、nsg
delete specific resource i.e, vm,nic,nsg using terraform
我在防火墙内创建了 azure vm、nic、nsg。现在我需要删除在防火墙内创建的特定 vm、nic、nsg。这个我会一直做下去的。
当我尝试用下面的特定 vm、ns、nic 删除时,它正在删除整个资源组。
terraform init
terraform apply -no-color -auto-approve
terraform destroy -force
我的代码:
# Configure the Microsoft Azure Provider
provider "azurerm" {
subscription_id = "xxxxx"
client_id = "xxxxx"
client_secret = "xxxxx"
tenant_id = "xxxxx"
}
# Locate the existing custom/golden image
data "azurerm_image" "search" {
name = "AZLXSPTDEVOPS01_Image"
resource_group_name = "RG-EASTUS-SPT-PLATFORM"
}
output "image_id" {
value = "/subscriptions/xxxxxxx/resourceGroups/RG-EASTUS-SPT-PLATFORM/providers/Microsoft.Compute/images/AZLXSPTDEVOPS01_Image"
}
# Create a Resource Group for the new Virtual Machine.
resource "azurerm_resource_group" "main" {
name = "RG-PF-TEST"
location = "eastus"
}
# Create a Subnet within the Virtual Network
resource "azurerm_subnet" "internal" {
name = "SNET-IN"
virtual_network_name = "VNET-PFSENSE-TEST"
resource_group_name = "${azurerm_resource_group.main.name}"
address_prefix = "192.168.2.0/24"
}
# Create a Network Security Group with some rules
resource "azurerm_network_security_group" "main" {
name = "RG-Dev-NSG"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
security_rule {
name = "allow_SSH"
description = "Allow SSH access"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
# Create a network interface for VMs and attach the PIP and the NSG
resource "azurerm_network_interface" "main" {
name = "NIC-Dev"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_security_group_id = "${azurerm_network_security_group.main.id}"
ip_configuration {
name = "primary"
subnet_id = "${azurerm_subnet.internal.id}"
private_ip_address_allocation = "static"
private_ip_address = "192.168.2.6"
}
}
# Create a new Virtual Machine based on the Golden Image
resource "azurerm_virtual_machine" "vm" {
name = "AZLXSPTDEVOPS01"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_interface_ids = ["${azurerm_network_interface.main.id}"]
vm_size = "Standard_DS12_v2"
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
storage_image_reference {
id = "${data.azurerm_image.search.id}"
}
storage_os_disk {
name = "AZLXSPTDEVOPS01-OS"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "APPVM"
admin_username = "devopsadmin"
admin_password = "admin#2019"
}
os_profile_linux_config {
disable_password_authentication = false
}
}
我只需要删除特定的 vm、nic 和 nsg。有人可以帮我吗
是的,现在我可以使用以下命令删除特定资源。
terraform init
terraform apply -no-color -auto-approve
terraform destroy -target azurerm_network_interface.main -no-color -auto-approve
terraform destroy -target azurerm_network_security_group.main -no-color -auto-approve
terraform destroy -target azurerm_virtual_machine.vm -no-color -auto-approve
我在防火墙内创建了 azure vm、nic、nsg。现在我需要删除在防火墙内创建的特定 vm、nic、nsg。这个我会一直做下去的。
当我尝试用下面的特定 vm、ns、nic 删除时,它正在删除整个资源组。
terraform init
terraform apply -no-color -auto-approve
terraform destroy -force
我的代码:
# Configure the Microsoft Azure Provider
provider "azurerm" {
subscription_id = "xxxxx"
client_id = "xxxxx"
client_secret = "xxxxx"
tenant_id = "xxxxx"
}
# Locate the existing custom/golden image
data "azurerm_image" "search" {
name = "AZLXSPTDEVOPS01_Image"
resource_group_name = "RG-EASTUS-SPT-PLATFORM"
}
output "image_id" {
value = "/subscriptions/xxxxxxx/resourceGroups/RG-EASTUS-SPT-PLATFORM/providers/Microsoft.Compute/images/AZLXSPTDEVOPS01_Image"
}
# Create a Resource Group for the new Virtual Machine.
resource "azurerm_resource_group" "main" {
name = "RG-PF-TEST"
location = "eastus"
}
# Create a Subnet within the Virtual Network
resource "azurerm_subnet" "internal" {
name = "SNET-IN"
virtual_network_name = "VNET-PFSENSE-TEST"
resource_group_name = "${azurerm_resource_group.main.name}"
address_prefix = "192.168.2.0/24"
}
# Create a Network Security Group with some rules
resource "azurerm_network_security_group" "main" {
name = "RG-Dev-NSG"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
security_rule {
name = "allow_SSH"
description = "Allow SSH access"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
# Create a network interface for VMs and attach the PIP and the NSG
resource "azurerm_network_interface" "main" {
name = "NIC-Dev"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_security_group_id = "${azurerm_network_security_group.main.id}"
ip_configuration {
name = "primary"
subnet_id = "${azurerm_subnet.internal.id}"
private_ip_address_allocation = "static"
private_ip_address = "192.168.2.6"
}
}
# Create a new Virtual Machine based on the Golden Image
resource "azurerm_virtual_machine" "vm" {
name = "AZLXSPTDEVOPS01"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_interface_ids = ["${azurerm_network_interface.main.id}"]
vm_size = "Standard_DS12_v2"
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
storage_image_reference {
id = "${data.azurerm_image.search.id}"
}
storage_os_disk {
name = "AZLXSPTDEVOPS01-OS"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "APPVM"
admin_username = "devopsadmin"
admin_password = "admin#2019"
}
os_profile_linux_config {
disable_password_authentication = false
}
}
我只需要删除特定的 vm、nic 和 nsg。有人可以帮我吗
是的,现在我可以使用以下命令删除特定资源。
terraform init
terraform apply -no-color -auto-approve
terraform destroy -target azurerm_network_interface.main -no-color -auto-approve
terraform destroy -target azurerm_network_security_group.main -no-color -auto-approve
terraform destroy -target azurerm_virtual_machine.vm -no-color -auto-approve