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
1 comment:
Are you interested in doing Data Science Training in Chennai with a Certification Exam? Catch the best features of Data Science training courses with Infycle Technologies, the best Data Science Training & Placement institutes in and around Chennai. Infycle offers the best hands-on training to the students with the revised curriculum to enhance their knowledge. In addition to the Certification & Training, Infycle offers placement classes for personality tests, interview preparation, and mock interviews for clearing the interviews with the best records. To have all it in your hands, dial 7504633633 for a free demo from the experts.
Post a Comment