首页 > 解决方案 > terraform resource force creation

问题描述

I would like to know if there is a way to always create a new terraform resource and not destroy any previously deployed resource.

example resource group 1 deployment

provider "azurerm" {
version = "=2.20.0"
features {}
}

resource "azurerm_resource_group" "example" {
name     = "rg-example-resources-1"
}

say now I want to deploy rg-example-resources-2 but I don't want to duplicate the resource block. I'm aware of count but I don't want to use it. if I change the resource group name terraform understand it as a replacement :(

any clue ?

标签: terraforminfrastructure-as-code

解决方案


不知道你为什么需要这个,但你可以为此使用 terraform 工作区(当前工作区是'默认'):'terraform workspace new bar''terraform apply'

新资源将在新工作区中创建。要正确使用它,您需要更改资源组名称,例如:

resource "azurerm_resource_group" "example" {
  name     = "rg-example-resources-${terraform.workspace}"
}

在此处查看详细信息 - https://www.terraform.io/docs/state/workspaces.html

作为一个选项,您可以使用计数器块。如果您不想重新创建第一个资源组,可以使用 count 块生成相同的资源组:

resource "azurerm_resource_group" "example" {
  name     = "rg-example-resources-${count.index + 1}"
}

推荐阅读