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