Import a resource into terraform state file

Terraform is great tool to deal with the infrastructure deployments. Though, it helps you to completely automate the infra deployments, the resources that are deployed by terraform can only be managed by terraform.

Terraform stores the state information in the terraform state file. If you make any changes to the environment manually outside the terraform, then terraform can not track those resources during the plan and apply. If you want add incremental changes to the environment, then add those changes to the terraform configuration file, so that terraform make those changes and updates the state file to track further automatically.

There are the case where we might have deployed the resources manually, but want to manage them through terraform. In such scenarios, terraform help you to track that resources by importing unmanaged resources into the terraform state file.

The following is one such example on Azure.

I have created a resource group in azure through terraform and I have manually created s virtual network which is not being tracked through terraform now. The current code to deploy resource group is:

provider "azurerm" {
    features {}
}
resource "azurerm_resource_group" "rgname" {
    name = "demo-poc"
    location = "eastus"
    tags = {
        Owner = "Admin"
    }
}
output "rgname" {
    value = azurerm_resource_group.rgname.name
}

The state file for the same is:

{
  "version": 4,
  "terraform_version": "0.12.23",
  "serial": 3,
  "lineage": "",
  "outputs": {
    "rgname": {
      "value": "demo-poc",
      "type": "string"
    }
  },
  "resources": [
    {
      "mode": "managed",
      "type": "azurerm_resource_group",
      "name": "rgname",
      "provider": "provider.azurerm",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "id": "/subscriptions/xxxxxxxxx/resourceGroups/demo-poc",
            "location": "eastus",
            "name": "demo-poc",
            "tags": {
              "Owner": "Admin"
            },
            "timeouts": null
          },
          "private": ""
        }
      ]
    }
  ]
}

The about configuration file and state file does not have any information about the VNet that we have created manually. So, now add below empty resource block for VNet in the above configuration.

resource "azurerm_virtual_network" "vnet" {}

Now get the resource Id of the Virtual Network from the Azure portal. Navigate to properties of the Virtual Network and copy the Id and run the command as below:

terraform import azurerm_virtual_network.vnet /subscriptions/xxxxxxxxxx/resourceGroups/demo-poc/providers/Microsoft.Network/virtualNetworks/demo-vnet

Once the import is successful, you should be able to see the resources details as output and in state file as well.

and the state file will have the VNet details as below.

{
      "mode": "managed",
      "type": "azurerm_virtual_network",
      "name": "vnet",
      "provider": "provider.azurerm",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "address_space": [
              "172.16.0.0/16"
            ],
            "ddos_protection_plan": [],
            "dns_servers": [],
            "guid": "",
            "id": "/subscriptions/xxxxxxxx/resourceGroups/demo-poc/providers/Microsoft.Network/virtualNetworks/demo-vnet",
            "location": "eastus",
            "name": "demo-vnet",
            "resource_group_name": "demo-poc",
            "subnet": [
              {
                "address_prefix": "172.16.0.0/24",
                "id": "/subscriptions/xxxxxxxxxxxx/resourceGroups/demo-poc/providers/Microsoft.Network/virtualNetworks/demo-vnet/subnets/default",
                "name": "default",
                "security_group": ""
              }
            ],
            "tags": {},
            "timeouts": {
            }
          },
          "private": ""
        }

Now complete the vnet resource construct with all attributes to manage through terraform from now on.

Hope the article help you.

Share your love

Newsletter Updates

Enter your email address below and subscribe to our newsletter

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *