3
votes

I have the following directory/files structure:

.
├── main.tf
├── modules
│ └── Resource_group
│ ├── main.tf
│ └── vars.tf

Configuration files

./main.tf

module "app-rg" {
  source = "./modules/Resource_Group"
}

./modules/resource_group/main.tf

provider "azurerm" {
  version = "=2.11.0"
  features { }
}

resource "azurerm_resource_group" "rg" {
  name = "${lookup(var.resource_group, var.env)}"
  location = "${lookup(var.location, var.env)}"
}

./modules/resource_group/vars.tf

variable "env" {
  description = "env : dev or prod"
}

variable "resource_group" {
  type = "map"
  default = {
    dev = "rg-dev"
    prod = "rg-prod"
  }
}

variable "location" {
  type = "map"
  default = {
    dev = "westindia"
    prod = "westeurope"
  }
}

When I run a "terraform plan" I will get a below error.

Error: Missing required argument

on main.tf line 6, in module "app-rg": 6: module "app-rg" {

The argument "env" is required, but no definition was found.

Why do I get The argument "env" is required, but no definition was found?

1
The error is telling you that your module has env as a required, non defaulted argument and you haven't provided it when calling the module (you only pass the source argument to link to the module and no other arguments).ydaetskcoR

1 Answers

7
votes

You have not provided the env input variable.

As there is no default value present, terraform doesn't know what env you want to use. You have a couple of options to fix that. First, you can include the variable value in the main.tf when calling a module. It would look like that:

module "app-rg" {
source = "./modules/Resource_Group"
env = "dev"
}

You can also include this input variable in a external variable definition file like terraform.tfvars, or pass it as an environmental variable, or set a default value in /modules/resource_group/vars.tf

Here are some resources on variables and modules by HasiCorp: