Terraform - 删除除一个以外的所有资源 [英] Terraform - Delete all resources except one

查看:17
本文介绍了Terraform - 删除除一个以外的所有资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Terraform 0.11 项目,其中包含 30-40 种不同的资源.除了少数之外,我想删除所有这些 - 这些少数在逻辑上是相互关联的.

I have a Terraform 0.11 project with 30-40 different resources. I would like to delete all of them except a few - and those few are logically related to each other.

我一直在寻找类似于 terraform destroy --except=resource-id 的东西,但那当然不存在.

I was looking for something close to terraform destroy --except=resource-id but that of course doesn't exist.

有没有办法在没有太多脚本的情况下实现这一点(Terraform 管理员有各种操作系统)?使用模块可能会使这个过程更容易吗?

Is there a way to achieve that without too much scripting (Terraform admins have various OSs)? Would using modules make that process easier perhaps?

推荐答案

terraform destroy 命令目前不存在功能.如果您真的想这样做,并且您知道自己在做什么,那么这里是解决方法.

There is no exist feature in terraform destroy command currently. If you really want to do that, and you know what you do, here is the workaround.

# list all resources
terraform state list

# remove that resource you don't want to destroy
# you can add more to be excluded if required
terraform state rm <resource_to_be_deleted> 

# destroy the whole stack except above excluded resource(s)
terraform destroy 

那么为什么这些命令对你的想法有效?

状态 (*.tfstate) 被 Terraform 用来将现实世界的资源映射到您的配置,跟踪元数据.

So why do these commands work for your idea?

The state (*.tfstate) is used by Terraform to map real world resources to your configuration, keep track of metadata.

terraform state rm 仅从状态文件 (*.tfstate) 中清除记录(资源).它不会破坏真正的资源.

terraform state rm cleans a record (resource) from the state file (*.tfstate) only. It doesn't destroy the real resource.

由于您没有运行 terraform applyterraform refresh,在 terraform state rm 之后,terraform 不知道排除的资源是完全创建.

Since you don't run terraform apply or terraform refresh, after terraform state rm, terraform doesn't know the excluded resource was created at all.

当您运行 terraform destroy 时,它没有关于排除资源状态的详细信息,也不会销毁它.它会摧毁其余的.

When you run terraform destroy, it has no detail about that excluded resource’s state and will not destroy it. It will destroy the rest.

顺便说一句,如果你愿意,以后你仍然有机会使用 terraform import 命令将资源导入回来.

By the way, later you still have chance to import the resource back with terraform import command if you want.

这篇关于Terraform - 删除除一个以外的所有资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆