Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Importing already created module Infrastructure into Terraform and update state file

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.

hashicorp hcl language terraform devops azurerm_resource_group resource module tf tfstate file location terraform import export already resource exist.png

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.

Microsoft Powershell terraform plan apply --auto-approve azurerm_resource_group module importing existing infrastructure resources already exists imported into the state.png

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.

Microsoft Azure terraform resource ID subscription id resource group terraform import hcl hashicorp resource already exist Error parsing contained more segments than required.png

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.

terraform import module resource group subscription id import prepared successful refresh state A resource with the ID already exists - to be managed via Terraform this resource needs to be imported into the State.png

Next I did init again and apply the tf configuration, it did the upgrade in-place successfully.

Terrafrom import apply --auto-approve unchanged attribute element hidden block subscription resource id add change update in-place modify already exists infrastructure error.png

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

Go Back

Comment

Blog Search

Page Views

7699791

Follow me on Blogarama