To view the current state, we can run the following command:
$ terraform show
That will output all resources that Terraform has created and manages, including outputs. Of course, this can be overwhelming for some, and we may want to view the list of resources Terraform manages.
Listing resources in the current state
To list the resources in the Terraform state file, run the following command:
$ terraform state list
azurerm_network_interface.main
azurerm_resource_group.main
azurerm_subnet.internal
azurerm_virtual_machine.main
azurerm_virtual_network.main
And as we see, there are five resources managed by Terraform. You might want to remove a resource from the Terraform state. It might be possible that someone has removed a resource manually as it is no longer required, but it isn’t removed from the Terraform configuration.
Removing a resource from the state
To remove a state manually from the Terraform state file, you must use the terraform state rm command. For example, to remove the Azure VM resource from the Terraform state, run the following command:
$ terraform state rm azurerm_virtual_machine.main Acquiring state lock. This may take a few moments… Removed azurerm_virtual_machine.main Successfully removed 1 resource instance(s). Releasing state lock. This may take a few moments…
Bear in mind that this has merely removed the resource from the state file and has not touched the actual resource sitting on Azure.
There might be instances where someone spun up a VM manually within Azure, and we now want Terraform to manage it. This kind of situation happens mostly in brownfield projects. In that case, we must declare the same configuration within Terraform and then import existing resources in the Terraform state. To do so, we can use the terraform import command.
Importing existing resources into Terraform state
You can use the terraform import command to import existing resources into Terraform state.
The terraform import command is structured as follows:
terraform import
For example, to reimport the httpd VM into the state, run the following command:
$ terraform import azurerm_virtual_machine.main \ “/subscriptions//resourceGroups\ /terraform-ws-dev/providers/Microsoft.Compute/virtualMachines/httpd” Acquiring state lock. This may take a few moments…
azurerm_virtual_machine.main: Importing from ID “/subscriptions/id/resourceGroups/ terraform-ws-dev/providers/Microsoft.Compute/virtualMachines/httpd”… azurerm_virtual_machine.main: Import prepared!
Prepared azurerm_virtual_machine for import
azurerm_virtual_machine.main: Refreshing state… [id=/subscriptions/1de491b5-f572-
459b-a568-c4a35d5ac7a9/resourceGroups/terraform-ws-dev/providers/Microsoft.Compute/
virtualMachines/httpd]
Import successful!
To check whether the resource is imported to the state, we can list the resources again using the following command:
$ terraform state list | grep azurerm_virtual_machine azurerm_virtual_machine.main
As we see, we have the VM within the state file. If we want to dig further into the resources, we can use terraform console.
Leave a Reply