rat*_*ath 6 infrastructure terraform terraform-provider-aws
我有一个Terraform 0.11项目,其中包含30-40种不同的资源。我想删除所有的人,但只有少数人-这些人在逻辑上是相互关联的。
我一直在寻找接近的东西,terraform destroy --except=resource-id但是那当然不存在。
有没有一种方法可以在没有太多脚本的情况下实现这一目标(Terraform管理员拥有各种操作系统)?使用模块会使该过程更容易吗?
terraform destroy当前命令中不存在功能。如果您确实想这样做,并且知道自己在做什么,这是解决方法。
# 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 resource(s)
terraform destroy
Run Code Online (Sandbox Code Playgroud)
Terraform使用状态(* .tfstate)将实际资源映射到您的配置,并跟踪元数据。
terraform state rm 从状态(* .tfstate)中删除项目(资源)。
由于您不运行terraform apply或terraform refresh之后terraform state rm,terraform根本不知道所创建的排除资源。
运行时terraform destroy,它没有有关该资源状态的详细信息,也不会破坏它。它将摧毁其余的。
小智 7
除了您想要的资源之外,针对每个资源(同时跳过数据资源)可能是 atm 的唯一方法:
#! /bin/bash
while read -r resource; do
terraform destroy -target="$resource"
done < <(terraform state list | grep -vE "^data\." | grep -vE "dont_remove|also_important")
Run Code Online (Sandbox Code Playgroud)
小智 7
要删除除某些资源之外的所有堆栈,您需要:
terraform state pull > bkup.jsonterraform state listterraform state rm "aws_myresource"。它不会删除云中的资源,而只是从 terraform 角度删除。terraform destroyterraform state pull > state-to-edit.jsonstate-to-edit.json添加资源对象来编辑。您还需要将值加 1{}bkup.json.resources[].serialterraform state push ./state-to-edit.jsonterraform plan或terraform state list是否仍然拥有所需的资源并删除所有其他资源。| 归档时间: |
|
| 查看次数: |
1990 次 |
| 最近记录: |