Az Resource Group Creation

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’.”
}
}

Hello!

I can see that you’re trying to keep the naming of the resource group consistent by utilizing string concatenation of the var.application_name, var.environment, var.region, and var.instance input variables. From your code, it seems you’re trying to assemble them into a different input named var.resource_group_name.

You might find it easier to instead simply set the name argument in the resource (in your resourcegroup.tf file) equal to the expression that you’ve already created:

resource "azurerm_resource_group" "prod" {
  name = "rg-${var.application_name}-${var.environment}-${var.region}-${var.instance}"
  location = var.location
}

Then your vars.tf file would not need the input for var.resource_group_name as you’re constructing it straight within the resource itself. The reason you’d want to do this is due to the fact that Terraform will not allow you to set input defaults using other inputs - you’ll get the Error: Variables not allowed message.

You miss out on the validation; however, you can do something similar within each of the inputs that make up the resource group name:

variable “application_name” {
  description = “The name of the application”
  type = string
  
  validation {
    condition = can(regex("^[[:alnum:]]+$", var.application_name))
    # [[:alnum:]] = The same as [0-9A-Za-z]
    # https://developer.hashicorp.com/terraform/language/functions/regex
    error_message = "The application_name value can only contain a-z, A-Z, and/or 0-9."
  }
}

You’d then provide the values for these inputs using either a -var flag when running the terraform command in the CLI, or you could also assign the values to the inputs using a var file.

I’m partial to a var file, so here’s an example of what you would put in the same directory where you’re running this code:

resource_group_vars.auto.tfvars

application_name = "myapp"
environment = "dev"
region = "eastus"
instance = "001"

This file would then automatically get loaded when running a terraform plan and/or terraform apply. You can change this behavior by removing the .auto from the file name, in which case you would need to call the var file when running the plan or apply with the -var-file flag.

Hopefully this gives you somewhere to start! Please let me know if you have any questions or concerns.

EDIT: You could also make use of locals if you wanted to do so and clean up the resource code. That would look something like this:

locals {
  rg_name = "rg-${var.application_name}-${var.environment}-${var.region}-${var.instance}"
}

resource "azurerm_resource_group" "prod" {
  name = local.rg_name
  location = var.location
}

This might help if you ever decide you want to change the structure of the resource group name and centralizes where you would do so. The documentation linked above does come with the stipulation that locals should be used in moderation to avoid confusion when other code maintainers come to view the code, so take this edit with a “grain of salt”.

Thank you a ton! This worked out great!

1 Like