[SOLVED] Azure: How can the generic Terraform script be used to automate the creation of the resource group without having to clone them repeatedly?


This Content is from Stack Overflow. Question asked by Mugil Karthikeyan

In order to automate the creation of the resource group, I want to use a generic Terraform script

As an example, the below script creates the resource group in Azure based on the input values provided in the *.tfvars.json file


# Configure the Microsoft Azure provider
provider "azurerm" {
  features {}

# Create a Resource Group if it doesn’t exist
resource "azurerm_resource_group" "example" {
  name     = var.resource_group_name
  location = "West US"


# Input variable: image sku
variable "resource_group_name" {
  description = "Name of the resource group"
  default     = "example-resources"



and executed like

terraform apply –auto-approve -var-file=”testing.tfvars.json”

Before creating the new resource group, it destroys the existing one. I don’t want to destroy the existing resource group, just create a new one.

I don’t want to clone the script. What should I do to use the generic Terraform script repeatedly without destroying the existing one?


You need to create a workspace for each environment.

Workspaces are managed with the terraform workspace set of commands. To create a new workspace and switch to it, you can use terraform workspace new; to switch workspaces you can use terraform workspace select; more info about Managing Workspaces.

Within your Terraform configuration, you may include the name of the current workspace using the ${terraform. Workspace} interpolation sequence.

This Question was asked in StackOverflow by Mugil Karthikeyan and Answered by SoySolisCarlos It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?