在使用Terraform多次重建Azure API Management时,我尝试添加了一个方便的清除功能
类似Azure API Management等一些Azure资源,在删除资源后会进入一定期限的软删除状态。这可能是出于安全的考虑,当意外删除相关资源时,需要能够恢复原状。如果想要使用相同的资源名创建一个干净的环境,需要先进行Purge再进行创建。因此,我们进行了Terraform配置尝试,使得在删除资源时也会进行Purge操作,然后尝试再次使用相同的资源名进行创建。
验证用的 Terraform 代码
provider "azurerm" {
features {
api_management {
purge_soft_delete_on_destroy = true
}
}
}
resource "azurerm_resource_group" "rg" {
name = "mnrapifn-rg"
location = "japaneast"
}
resource "azurerm_api_management" "apim" {
name = "mnrapifn-apim"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
publisher_name = "test"
publisher_email = "test@example.com"
sku_name = "Consumption_0"
}
执行Terraform以进行操作确认。
$ terraform init
$ terraform plan
$ terraform apply -auto-approve
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
$ terraform destroy -auto-approve
Destroy complete! Resources: 2 destroyed.
$ terraform apply -auto-approve
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
仅需要一个选项,将以下内容用汉语进行本地化改写:参考