anyone know how to go about destroying a specific ...
# terraform
m
anyone know how to go about destroying a specific resource deep in the modules without making a mess...? in this case i would like to destroy
module.service_b.module.ec2
...
Copy code
module "service_a" {
   source = "../modules/stuff"
...
}

module "service_b" {
   source = "../modules/stuff"
...
}

...

# modules/stuff

module "ec2" {
   source = "../modules/ec2"
}
...
... some more stuff
i thought this was pretty trivial until i step thru the tf plan, but i dont think this is doable by messing with hcl itself, instead...
Copy code
terraform destroy --target module.service_b.module.ec2
terraform state rm module.service_b.module.ec2
any other suggestions...?
e
for full context, by destroying that resource, you hope to.... just to make sure we're solving the right thing (xyproblem.info)
l
Have you tried to use the
removed
block, with the lifecycle.destroy option set to
true
(the default)...? https://developer.hashicorp.com/terraform/language/block/removed#lifecycle
m
it was decided to decommission the entire service_b, and to start with is to nuke the compute part of it... thats the gist of it, and u dont wanna look at the entire hcl, it's a giant spaghetti that got dumped on me... i'll look at that
removed
block, seems kinda jank for something trivial, but i'll dig into it...
l
i mean, if you just use the cli with destroy -target, on success the resources are removed from the state. but remember they will still be in the config, so next apply will create them again
one way or another, you gotta change the config to tell it you don't want to the resources anymore
👍 1
m
Indeed, make a resource optional, opt-in via a (boolean) variable, then disable it if you don't need it. Or, remove it from module and release new version of module, but that will affect everyone.
p
Also keep in mind StopInstances , which will be transparent to Terraform and will eliminate compute (but not EBS) costs until you are ready to delete. (Of course, this assumes that ec2 instances are at the core of what you want to delete.)