Terraform keep saying variable not defined though its defined. Please help

I am trying to create a resource in azure via Terraform. Even though I have declared all required variables, its erroring out. I am using modules for the same…

Here is the below code that I tried:

My Module’s main.tf that used to create resource:

resource "azurerm_public_ip" "syn_pip" {
  name                = "pip-${var.prefix}-${var.postfix}"
  location            = var.location
  resource_group_name = var.rg_name
  allocation_method = var.bastion_allocation_method
  sku = var.bastion_sku_type
}

My modules’s variables.tf :

variable "bastion_allocation_method" {
  type = string
  description = "allocation method for bastion"
}

variable "bastion_sku_type" {
  type = string
  description = "sku to be used for bastion"
}

My root module bastion.tf :

module "bastion" {
  source = "../modules/bastion"
  rg_name  = module.resource_group.name
  location = module.resource_group.location
  allocation_method = var.bastion_allocation_method
  sku = var.bastion_sku_type
  prefix  = var.prefix
  postfix = random_string.postfix.result
  subnet_id = azurerm_subnet.bastion_subnet.id
}

root module’s variable.tf:

variable "bastion_allocation_method" {
  type = string
}

variable "bastion_sku_type" {
  type = string
}

my terraform.tfvars which passed to terraform plan :

"bastion_allocation_method": "Static",
"bastion_sku_type": "Standard"

Error that I get:

 Error: Missing required argument
│ 
│   on bastion.tf line 1, in module "bastion":
│    1: module "bastion" {
│ 
│ The argument "bastion_allocation_method" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│ 
│   on bastion.tf line 1, in module "bastion":
│    1: module "bastion" {
│ 
│ The argument "bastion_sku_type" is required, but no definition was found.
╵
╷
│ Error: Unsupported argument
│ 
│   on bastion.tf line 7, in module "bastion":
│    7:   allocation_method = var.bastion_allocation_method
│ 
│ An argument named "allocation_method" is not expected here.
╵
╷
│ Error: Unsupported argument
│ 
│   on bastion.tf line 8, in module "bastion":
│    8:   sku = var.bastion_sku_type
│ 
│ An argument named "sku" is not expected here.
╵

Exited with code exit status 1

Can someone suggest, what is the mistake I am doing ?

You are getting your root module variables and your child module variables confused.

You need variables for all of these in your bastion module

  rg_name  
  location 
  allocation_method 
  sku <= sku not bastion_sku_type
  prefix  
  postfix 
  subnet_id 

For example in your variables.tf you have a variable called bastion_sku_type, but when you pass it into your module you called it sku

I recommend just hard coding the module call and getting it working, and then add in any root module variables you require last.

1 Like