Search code examples
azureterraformterraform-provider-azure

terraform reports name argument not defined for resource_group


Aim: To create an Azure resource group using variables and enforce naming constraints.

Terraform Azure resource group module definition is as below.

cat locals.tf

locals {
  tags = {
    "Environment" = var.environment_name
    "Department"  = var.department
    "Managed By"  = "Terraform Open Source"
  }
}

cat main.tf

resource "azurerm_resource_group" "resource_group" {
  name     = regex("^[-\\w\\._\\(\\)]+$",substr("rg-${local.environment_name}-${local.application_name}-${var.location}", 90))
  location = var.location
  tags = local. Tags
}output "resource_group_id" {
  value       = azurerm_resource_group.resource_group.id
  description = "Resource Group id"
}

cat outputs.tf

output "resource_group_name" {
  value       = azurerm_resource_group.resource_group.name
  description = "The name of the resource group"
}

output "resource_group_location" {
  value       = azurerm_resource_group.resource_group.location
  description = "The location of the resource group"
}

cat providers.tf

terraform {
  required_version = ">= 1.4.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 3.55.0"
    }
  }
}
provider "azurerm" {
  features {}
}

cat variables.tf

variable "name" {
  type        = string
  description = "Name of the resource group"
}

variable "location" {
  type        = string
  description = "Location of the resource group"
}

variable "environment_name" {
  type        = string
  description = <<EOT
    (Optional)  The name of the environment where the resources will be deployed.

    Options:
      - dev
      - uat
      - test
      - prod

    Default: dev
  EOT

  default = "dev"

  validation {
    condition     = can(regex("dev|uat|test|prod"), var.environment)
    error_message = "Err: environment name is not valid."
  }
}
variable "department" {
  type        = string
  description = "Name of the department"
}
variable "cost_centre" {
  type        = string
  description = "Cost Centre for the Resource or Resource Group"

}
variable "application_name" {
  type = string
  description = "Name of the application"
  
}

Now, I have created an example like the one below.

cat main.tf

module "rg" {
  source           = "./.."
  location         = "australiaeast"
  environment_name = "Dev"
  department       = "Data Services"
  cost_centre      = "ABC101"
  application_name = "demo"
}        

cat provider.tf

terraform {
  required_version = ">= 1.4.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 3.55.0"
    }
  }
}
provider "azurerm" {
  features {}
}

Unfortunately, when I run terraform plan, I get an error like the one below.

Error:

│ Error: Missing required argument
│ 
│   on main.tf line 1, in module "rg":
│    1: module "rg" {
│ 
│ The argument "name" is required, but no definition was found.
╵

Solution

  • If you look at your code, it clearly defines name variable without a default value:

    variable "name" {
      type        = string
      description = "Name of the resource group"
    }
    

    Since there is no default value given, you have to explicitly provide name when you instantiate your rg module. If you don't want to do this, you have to define a default name, e.g.:

    variable "name" {
      type        = string
      default     = "some-default-rg-name"
      description = "Name of the resource group"
    }