0
votes

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 ?

1
How would that be declarative? Terraform is a declarative language. It sounds like you want an imperative command here?Jonas
it would be declarative if the input are being read from an external fileuser3291059

1 Answers

0
votes

Not sure why do you need this, but you can use terraform workspace for this (current workspace is 'default'): 'terraform workspace new bar' 'terraform apply'

New resource will be created in the new workspace. To use it without errors, you need to change resource group name, e.g.:

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

See the details here - https://www.terraform.io/docs/state/workspaces.html

As an option, you can use counter block. If you don't want to re-create first resource group, you can generate the same resource group with count block:

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