Experience: is what you get soon after you need it.

Experience: is what you get soon after you need it.



My Cloud Certifications:

Certified Kubernetes Administrator (CKA)

Cloud Certified Security Professional (ISC2)

CyberSecurity Certified Professional (ISC2)

AWS Certified Solutions Architect Associate

Azure Certified Architect Expert

Azure Certified Architect

Azure Certified Administrator

Oracle Cloud Infrastructure 2018 Certified Architect Associate.

Oracle Cloud Infrastructure Classic 2018 Certified Architect Associate.

Oracle Database Cloud Administrator Certified Professional.

Oracle Database Cloud Service Operations Certified Associate.

Search This Blog

Wednesday, May 19, 2021

How to import a resource in terraform if not is not being managed already

 If a cloud resource is not being managed by your terraform scripts then in the below post I will show you how you can make terraform mange it.

I have a resource already created by ARM templates but now I want it managed by terraform.

Ex:-

azurerm_subnet.app-subnet: Creating...


azurerm_subnet.app-subnet: Creating...

 Error: A resource with the ID "/subscriptions/12XXXXXXX058f0/resourceGroups/XXXcare-rg/providers/Microsoft.Network/virtualNetworks/datacare-vnet/subnets/xxxcare-appsubnet" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_subnet" for more information.

  on main.tf line 29, in resource "azurerm_subnet" "app-subnet":

   29: resource "azurerm_subnet" "app-subnet" {


Now if we want terraform to manage this resource we need to add (import) this resource into the state file. 

First get the resource id of this resource from properties blade.


Then use the below command to import the resource into the state file.


terraform import azurerm_subnet.app-subnet /subscriptions/12XXX58f0/resourceGroups/care-rg/providers/Microsoft.Network/virtualNetworks/my-vnet


Ex:-

samshaik@shaikprod:~/terraform/azure/confluence$ terraform import azurerm_subnet.app-subnet /subscriptions/12XXX58f0/resourceGroups/care-rg/providers/Microsoft.Network/virtualNetworks/my-vnet

azurerm_subnet.app-subnet: Importing from ID "/subscriptions/12XXX58f0/resourceGroups/care-rg/providers/Microsoft.Network/virtualNetworks/my-vnet"...

azurerm_subnet.app-subnet: Import prepared!

  Prepared azurerm_subnet for import

azurerm_subnet.app-subnet: Refreshing state... [id=/ssubscriptions/12XXX58f0/resourceGroups/care-rg/providers/Microsoft.Network/virtualNetworks/my-vnet]


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.


Now create the plan again:

samshaik@shaikprod:~/terraform/azure/confluence$ terraform plan -out confluencedev.plan

Plan: 3 to add, 0 to change, 1 to destroy.


Saved the plan to: confluencedev.plan

To perform exactly these actions, run the following command to apply:

    terraform apply "confluencedev.plan"


Now apply the plan to create the resources:

Plan: 3 to add, 0 to change, 0 to destroy.


Do you want to perform these actions?

  Terraform will perform the actions described above.

  Only 'yes' will be accepted to approve.


  Enter a value: yes