Reputation: 379
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?
Upvotes: 13
Views: 40883
Reputation: 949
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:
Upvotes: 20