Reputation: 566
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
main.tf
# 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"
}
vars.tf
# Input variable: image sku
variable "resource_group_name" {
description = "Name of the resource group"
default = "example-resources"
}
testing.tfvars.json
{
"resource_group_name":"example-resources-testing"
}
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? Is it just a matter of disabling or removing the state before execution?
The purpose is to automate the resource group creation using ITSM ticketing system, ITSM ticketing tool will provide the input to the terraform script to create the required resource. It should not impact the existing resource groups.
Upvotes: 0
Views: 327
Reputation: 829
Firstly, let's answer why changing variable is removing old resource group (if you're not interested, just skip to last section)
Terraform code isn't "script". It's infrastructure state that you desire.
So if you change variable you change your desire from having resource-group-1
to having resource-group-2
. And your current desire is stored in something called state file. And to fulfil your desire, terraform have to remove first one and create another one. But there are few ways to do what you want to.
To have multiple resource group in single terraform code you either have to:
To have it first way you have two options:
Terraform also allows you to store multiple resource groups in a single state file, so the only thing is to iterate through the list of resource group names. And your iteration operator is for_each
. It should look something like that:
variable "resource_group_names" {
description = "Names of the resource groups"
type = set(string)
}
resource "azurerm_resource_group" "example" {
for_each = var.resource_gruop_names
name = each.value
location = "West US"
}
and variable would be:
{
"resource_group_names":["resource-group-1", "resource-group-2", "resource-group-3"]
}
each
keyword gives you key
and value
but in sets its same. For more about each
, for_each
, etc. you can read here
Upvotes: 4
Reputation: 843
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.
Upvotes: 2