terraform 生命周期防止破坏

Moe*_*Moe 6 terraform terraform-provider-aws

我正在与 Terraform V11 和 AWS 提供商合作;我正在寻找一种方法来防止在销毁阶段销毁少量资源。所以我使用了以下方法。

lifecycle {
    prevent_destroy = true

} 
Run Code Online (Sandbox Code Playgroud)

当我运行“terraform plan”时,出现以下错误。

the plan would destroy this resource, but it currently has 
lifecycle.preven_destroy set to true. to avoid this error and continue with the plan. 
    either disable or adjust the scope. 
Run Code Online (Sandbox Code Playgroud)

我正在寻找的只是一种避免在 destroy 命令期间破坏其中一个资源及其依赖项的方法。

小智 7

AFAIK 尚不支持此功能您需要从状态文件中删除该资源,然后重新导入它

terraform plan | grep <resource> | grep id 
terraform state rm <resource>
terraform destroy
terraform import <resource> <ID>
Run Code Online (Sandbox Code Playgroud)


小智 4

最简单的方法是注释掉您想要销毁的所有资源,然后执行terraform apply.

  • 这实际上是最好的方法吗?这显然无法扩展。 (2认同)
  • @BenLongo 这就是使用生命周期的目的。如果可以的话,建议不要使用生命周期,因为它完全改变了 Terraform 的行为方式。 (2认同)