Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform: terraform plan returns the Error: Unsupported argument

I have the following three files as below: main.tf, variables.tf and dev.auto.tfvars

Snippet from main.tf

module "sql_vms" {
  source                  = "git::[email protected]:xxxxxxxxxxxx/terraform-modules//azure/"
  rg_name                 = var.resource_group_name
  location                = module.resource_group.external_rg_location
  vnet_name               = var.virtual_network_name
  subnet_name             = var.sql_subnet_name
  app_nsg                 = var.application_nsg
  vm_count                = var.count_vm
  base_hostname           = var.sql_host_basename
  sto_acc_suffix          = var.storage_account_suffix
  vm_size                 = var.virtual_machine_size
  vm_publisher            = var.virtual_machine_image_publisher
  vm_offer                = var.virtual_machine_image_offer
  vm_sku                  = var.virtual_machine_image_sku
  vm_img_version          = var.virtual_machine_image_version
  username                = var.username
  password                = var.password
}

Snippet from variables.tf

variable "app_subnet_name" {
  type    = string
}

variable "sql_subnet_name" {
  type    = string
}

Snippet from dev.auto.tfvars

app_subnet_name = "subnet_1"

sql_subnet_name = "subnet_2"

application_nsg = "test_nsg"

However, I'm getting error like below

Error: Unsupported argument

  on main.tf line 7, in module "sql_vms":
   7:   subnet_name    = var.sql_subnet_name

An argument named "subnet_name" is not expected here.


Error: Unsupported argument

  on main.tf line 8, in module "sql_vms":
   8:   app_nsg        = var.application_nsg

An argument named "app_nsg" is not expected here.

My modules directory structure looks like below

$ ls -R terraform-modules/
terraform-modules/:
aws  azure  gcp

terraform-modules/aws:
alb  ec2-instance-rhel

terraform-modules/aws/alb:

terraform-modules/aws/ec2-instance-rhel:
main.tf

terraform-modules/azure:
compute  resourcegroup  sqlserver

terraform-modules/azure/compute:
main.tf  README.md  variable.tf

terraform-modules/azure/resourcegroup:
data.tf  outputs.tf  variables.tf

terraform-modules/azure/sqlserver:
main.tf  README.md  variables.tf

terraform-modules/gcp:
compute

terraform-modules/gcp/compute:
main.tf

Any idea what is going wrong here?

like image 937
learner Avatar asked Jun 13 '20 14:06

learner


2 Answers

If you are starting out with Terraform, you will get that error message ("An argument named "example" is not expected here") if your module arguments refer to the resource properties and not to variable names, see below for an example:

Example of a Terraform module "example_mod.tf" you want to call from your module:

variable "sg_name" { }   # Usually in a separate file
variable "sg_desc" { }   # called variables.tf

resource "example_resource" "example_name" {
  name        = var.sg_name
  description = var.sg_desc
...
}

CORRECT WAY:

module "my_module" {
  source = "./modules/example_mod.tf"

  sg_name = "whatever"  # NOTE the left hand side "sg_name" is the variable name
  sg_desc = "whatever"    
...
}

INCORRECT WAY: (Gives the error "An argument named "name" is not expected here" )

module "my_module" {
  source = "./modules/example_mod.tf"

  name   = "whatever" # WRONG because the left hand side "name" is a resource property
  description = "whatever" # WRONG for the same reason   
...
}
like image 138
CyclingDave Avatar answered Jan 03 '23 02:01

CyclingDave


I think the issue is that you do not refer to the exact module with the source. I see you have three modules in the source:

source = "git::[email protected]:xxxxxxxxxxxx/terraform-modules//azure/"

They are compute, resourcegroup and sqlserver. But you want to load them in one module. So it cannot find the related variables for the modules. I also don't think it's the right way to load all the modules like that. I would recommend you load the modules one by one like below:

module "compute" {
  source = "git::[email protected]:xxxxxxxxxxxx/terraform-modules//azure/compute"
  ...
}

module "resourcegroup" {
  source = "git::[email protected]:xxxxxxxxxxxx/terraform-modules//azure/resourcegroup"
  ...
}

module "sqlserver" {
  source = "git::[email protected]:xxxxxxxxxxxx/terraform-modules//azure/sqlserver"
  ...
}
like image 43
Charles Xu Avatar answered Jan 03 '23 01:01

Charles Xu