gpt4 book ai didi

azure - 通过 Terraform 管理 Azure 资源锁

转载 作者:行者123 更新时间:2023-12-05 04:46:27 26 4
gpt4 key购买 nike

我计划通过 Terraform 管理 Azure 资源锁。我的想法是在资源级别创建一个只读锁。根据 Terraform 文档,以下代码可用于此目的。

resource "azurerm_management_lock" "resource-group-level" {
name = "resource-group-level"
scope = azurerm_resource_group.example.id
lock_level = "ReadOnly"
notes = "This Resource Group is Read-Only"
}

现在我担心对该资源的任何后续修改。在下一个执行周期中,对资源的任何更改都将失败,因为资源上存在只读锁。我希望删除锁,进行修改并重新添加锁。

如何通过 Terraform 处理这样的场景?

最佳答案

如果您想删除资源组锁定,然后在对资源组进行更改后应用它,那么最好将锁定脚本保留在不同的文件中,并将资源保留在不同的文件中。

我们将使用数据源并为资源创建锁,然后销毁它,您可以来回移动而不影响资源。

示例:我使用不同的 .tf 文件创建了一个资源组,现在我想对其应用只读锁定。

用于锁定的.tf文件

provider "azurerm" {
features {}
}
data "azurerm_resource_group" "example" {
name = "your resource-group name"
}

resource "azurerm_management_lock" "rglock" {
name = "resource-group-level"
scope = data.azurerm_resource_group.example.id
lock_level = "ReadOnly"
notes = "This Resource Group is Read-Only"
}

所以,我们只需要将 terraform 应用于此锁定文件,就会创建锁定,当我们需要删除它时,我们可以执行 terraform destroy 并来回执行。

terraform-apply 的输出

enter image description here

enter image description here

terraform-destroy 的输出

enter image description here

enter image description hereterraform destroy 命令仅销毁 Lock,因为它是 terraform 脚本中的资源 block ,而我们的资源组是数据 block ,因此不会对其进行任何更改。

关于azure - 通过 Terraform 管理 Azure 资源锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68794456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com