Greetings, I am very new to this. I am just trying to create one resource group to understand how the variables work. I want all resource group creation to use a specific format. 1.) I am not sure if the code below is correct. 2.) How do I run “apply” with the arguments for variables? Do I need to use -var or can I insert the variables into another file to read?
resourcegroup.tf
resource “azurerm_resource_group” “prod” {
name = var.resource_group_name
location = var.location
}
vars.tf
variable “location” {
type = string
default = “eastus”
}
Variables for different components of the resource group name
variable “application_name” {
description = “The name of the application”
type = string
}
variable “environment” {
description = “The environment (e.g., dev, prod)”
type = string
}
variable “region” {
description = “The region (e.g., eastus, westus)”
type = string
}
variable “instance” {
description = “The instance number or name”
type = string
}
Construct the resource group name
variable “resource_group_name” {
description = “The full name of the resource group”
type = string
default = “rg-{var.application_name}-{var.environment}-{var.region}-{var.instance}”
validation {
condition = can(regex(“^rg-[a-zA-Z0-9]±[a-zA-Z0-9]±[a-zA-Z0-9]±[a-zA-Z0-9]+$”, var.resource_group_name))
error_message = “The resource group name must follow the format: ‘rg-application-environment-region-instance’.”
}
}