This is a second part of Importing existing resources into Terraform - Step by Step, in earlier article I imported resource mentioned in tf file, here in this article I will be importing existing module resources and update tfstate file. Below are the module structure of my resources in the terraform tf file.
After running terraform plan it shows plan is successful and it will create a new resource, but resource already exists in Microsoft Azure Cloud. But when applying the configuration shows the correct problem/error as shown below.
terraform apply --auto-approve 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: # module.resorucegroup.azurerm_resource_group.rg will be created + resource "azurerm_resource_group" "rg" { + id = (known after apply) + location = "eastus" + name = "vcloud-lab.com" + tags = { + "orgnization" = "vcloud-lab.com" + "owner" = "vjanvi" } } Plan: 1 to add, 0 to change, 0 to destroy. module.resorucegroup.azurerm_resource_group.rg: Creating... ╷ │ Error: A resource with the ID "/subscriptions/9e22fxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vCloud-lab.com" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_resource_group" for more information. │ │ with module.resorucegroup.azurerm_resource_group.rg, │ on resourcegroup\main.tf line 1, in resource "azurerm_resource_group" "rg": │ 1: resource "azurerm_resource_group" "rg" { │ ╵
To resolve this issue I have to grabbed Resource ID of a resource.
Next command will import existing module resource and update tfstate file. In the import command provide module resource address with resource service id. Import is successful.
terraform import module.resorucegroup.azurerm_resource_group.rg /subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vCloud-lab.com module.resorucegroup.azurerm_resource_group.rg: Importing from ID "/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vCloud-lab.com"... module.resorucegroup.azurerm_resource_group.rg: Import prepared! Prepared azurerm_resource_group for import module.resorucegroup.azurerm_resource_group.rg: Refreshing state... [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vCloud-lab.com] Import successful! The resources that were imported are shown above. These resources are now in your Terraform state and will henceforth be managed by Terraform.
Next I did init again and apply the tf configuration, it did the upgrade in-place successfully.
terraform apply --auto-approve
module.resorucegroup.azurerm_resource_group.rg: Refreshing state... [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vCloud-lab.com]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
~ update in-place
Terraform will perform the following actions:
# module.resorucegroup.azurerm_resource_group.rg will be updated in-place
~ resource "azurerm_resource_group" "rg" {
id = "/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vCloud-lab.com"
name = "vCloud-lab.com"
~ tags = {
~ "owner" = "vjani" -> "vjanvi"
# (1 unchanged element hidden)
}
# (1 unchanged attribute hidden)
# (1 unchanged block hidden)
}
Plan: 0 to add, 1 to change, 0 to destroy.
module.resorucegroup.azurerm_resource_group.rg: Modifying... [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vCloud-lab.com]
module.resorucegroup.azurerm_resource_group.rg: Modifications complete after 4s [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vCloud-lab.com]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
Useful Articles
Create an Azure virtual machine scale set and load balancer using Terraform
Azure Terraform fixed Availibility Zones on Virtual Machine Scale Set
Writing and Using Terraform modules
Terraform Using one module variable in another module
Hashicorp Terraform dynamic block with example
Terraform for_each loop on map example
Terraform for_each loop on resource example
Terraform manage similar resources with for_each loop inside modules