2

I have already created resource group(not created using my code).

I run terraform apply and my infra was created. But when I run terraform destroy - the console says that my resource group should be deleted too. This should not happen, because not only my infra is in this resource group.

I have try to use terraform import as described here https://stackoverflow.com/a/47446540/10912908 and got the same result as before.

Also, I have tried to define the resource group with only name, but it is not work(. Terraform destroy removes this resource

resource "azurerm_resource_group" "testgroup" {    
name = "Test-Group"    
}
John Doe
  • 65
  • 2
  • 6

2 Answers2

3

you have to not include resource group resource in the configuration for the resource group to not be destroyed (as all the resources in the configuration are to be destroyed). if you rely on outputs from that resource you can use data resource instead.

data "azurerm_resource_group" "test" {
  name = "Test-Group"
}

OP also needed to remove resource group from the state file.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • This answer would be improved by a simple working example of how they can achieve this rather than importing the resource. – ydaetskcoR Jan 31 '19 at 15:32
  • I assume it is what I need? [link](https://www.terraform.io/docs/providers/azurerm/d/resource_group.html) – John Doe Jan 31 '19 at 15:32
  • Just checked it, but it doesn't work. I think it causes because I previously deployed with the previous code, and now I did as you wrote – John Doe Jan 31 '19 at 15:46
  • what doesnt work? if you replaced resource with this, you need to replace references to the resource as well. – 4c74356b41 Jan 31 '19 at 15:48
  • that's right, I did it, but when I run terraform destroy the console says that the resource group will be deleted too – John Doe Jan 31 '19 at 15:54
  • you sure you removed the resource group resource? it doesnt remove data resources 101% – 4c74356b41 Jan 31 '19 at 15:58
  • btw, terraform apply removes resource group too – John Doe Jan 31 '19 at 15:59
  • I did not confirm the destroy command, I just ran it to see what would be removed – John Doe Jan 31 '19 at 16:02
  • I replaced the resource "azurerm_resource_group" ... to data "azurerm_resource_group" ... as you said, and updated links – John Doe Jan 31 '19 at 16:19
  • ok, i'm not really sure what you are doing, unfortunately, but I do know that if you do not have the resource in the configuration it wont get deleted, maybe if you imported it - it might, since its not part of the configuration anymore. I'd suggest removing state files and trying again – 4c74356b41 Jan 31 '19 at 16:21
  • 1
    You are right, I needs some actions with my state file. I only remove resource group from the state file with the following command: terraform state rm testgroup and after that I do not see the resource group in the list for deletion. Thank you very much! – John Doe Jan 31 '19 at 16:49
1

This bash script could work:

terraform state list | while read line
do 
if [[ $line == azurerm_resource_group* ]]; then
echo $line " is a resource group and will not be deleted!"
else
echo "deleting: " $line
terraform destroy -target $line -auto-approve
fi
done

It lists all resources which are managed by terraform and then runs a delete script for every entry except for lines containing "azurerm_resource_group*"

Quisl
  • 11
  • 1