0
votes

I know we can destroy the resources created by terraform using its terraform destory command but let's say I don't want to use that functionality and I only have the apply option available.

As we can pass the count parameter to create multiple resource instances, I saw that by setting the count value to 0 terminate those instances. example,

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
  count         = "0"

  tags = {
    Name = "HelloWorld"
  }
}

Can we destroy all the types of resources using the count parameter by setting it 0? will it also handle the dependency of destroying other resources? (destroying the aws_security_group_rule by destroying aws_security_group)

Is this a good practice to terminate the resources considering I don't have the terraform destroy option available with me? or is there any better approach to perform a similar operation?

1
Latest version for Terraform allows count at the module level: hashicorp.com/resources/… you can set it once and everything under that module will do what you wantHelder Sepulveda
yeah! I didn't use the latest 0.13 version. I am using 0.12.7. I didn't yet plan to migrate to the newer 0.13. But, yes we could easily do it by your way.Rushikesh Gaidhani

1 Answers

3
votes

Using Apply in the way you suggest is the usual way of destroying resources when modifying your infrastructure. The Destroy option is more for when you want a full cleanup.

Setting count=0 is a supported way to destroy all resources created in that block, as well as their dependencies though that can run into complications if other resource blocks depend indirectly on that count.

However simply removing the block completely (as well as dependent resource blocks) will also destroy them on the next apply run. This would be the usual practice unless it's only a temporary destruction, there are code reuse considerations, or you need destroy provisioners to execute.