Terraform keep saying variable not defined though its defined - terraform

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 Bastion/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 Bastion/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 ?

In your Bastion module in the variables.tf you specify the arguments
variable "bastion_allocation_method" {
...
}
variable "bastion_sku_type" {
...
}
In your case your module expects the vars bastion_allocation_method and bastion_sku_type
But when you call the module, you dont pass the variable names properly
module "bastion" {
source = "../modules/bastion"
...
allocation_method = var.bastion_allocation_method
sku = var.bastion_sku_type
...
}
Use instead
module "bastion" {
source = "../modules/bastion"
...
bastion_allocation_method = var.bastion_allocation_method
bastion_sku_type = var.bastion_sku_type
...
}
The variable assignments that you pass in module clause should match the definitions in the module's variables.tf

Related

Dynamic block of lifecycle_rule not working in terraform 1.0.6

The following block within a google_cloud_storage resource
dynamic "lifecycle_rule" {
for_each = var.bucket_lifecycle_rule
content {
condition {
age = lifecycle_rule.age
with_state = lifecycle_rule.with_state
}
action {
type = lifecycle_rule.type
storage_class = lifecycle_rule.storage_class
}
}
}
with the following variable declaration
variable "bucket_lifecycle_rule" {
description = "Lifecycle rules"
type = list(object({
age = string
with_state = string
type = string
storage_class = string
}))
default = [
{
age = 30
with_state = "ANY"
type = "SetStorageClass"
storage_class = "COLDLINE"
},
{
age = 120
with_state = "ANY"
type = "Delete"
storage_class = ""
},
]
}
errors out as follows:
│ Error: Unsupported attribute
│
│ on main.tf line 18, in resource "google_storage_bucket" "my_bucket":
│ 18: age = lifecycle_rule.age
│
│ This object does not have an attribute named "age".
╵
╷
│ Error: Unsupported attribute
│
│ on main.tf line 19, in resource "google_storage_bucket" "my_bucket":
│ 19: with_state = lifecycle_rule.with_state
│
│ This object does not have an attribute named "with_state".
╵
╷
│ Error: Unsupported attribute
│
│ on main.tf line 22, in resource "google_storage_bucket" "my_bucket":
│ 22: type = lifecycle_rule.type
│
│ This object does not have an attribute named "type".
╵
╷
│ Error: Unsupported attribute
│
│ on main.tf line 23, in resource "google_storage_bucket" "my_bucket":
│ 23: storage_class = lifecycle_rule.storage_class
│
│ This object does not have an attribute named "storage_class".
why is that?
You have to reference the properties from your objects with value:
dynamic "lifecycle_rule" {
for_each = var.bucket_lifecycle_rule
content {
condition {
age = lifecycle_rule.value.age
with_state = lifecycle_rule.value.with_state
}
action {
type = lifecycle_rule.value.type
storage_class = lifecycle_rule.value.storage_class
}
}
}
In the Terraform docs there is a note about this:
The iterator object has two attributes:
key is the map key or list element index for the current element. If
the for_each expression produces a set value then key is identical to value and should not be used.
value is the value of the current element.

Error Reference to undeclared resource - Trying to get terraform modules to work

New terraform learner here, trying to learn by example and by coding. I was looking for terraform modules that would allow one to create a VM, found the example here https://faun.pub/creating-a-windows-vm-in-azure-using-terraform-which-way-is-best-13aff3ed9b74 which I thought would take my very basic terraform knowledge up a notch, since creating a VM is a process I am familiar with and can build upon. The experience has been nothing but frustrating. This is my 3rd week on it, and it still feels like miles away.
I read up the first solution and my files are structured in exactly thesame way as described.Mind, I have tried the first method, and it was riddled with errors, so I decided to try the second method which was supposed to be simpler.
I created a folder called simple_vm and inside it, another folder called vm.
Here are the contents.
vm_module_local_example.tf
# Create an Azure VM cluster with Terraform calling a Module. Creates 1 for Windows 10 desktop and 1 for Windows 2019 Server.
module windows_desktop_vm_using_local_module {
source = "./vm"
resource_group_name = azurerm_resource_group.rg.name
location = "uksouth"
sloc = "uks"
vm_subnet_id = module.network.vnet_subnets[0]
vm_name = "tfdtlocmod"
vm_size = var.desktop_vm_size
publisher = var.desktop_vm_image_publisher
offer = var.desktop_vm_image_offer
sku = var.desktop_vm_image_sku
static_ip_address = "10.0.1.15"
activity_tag = "Windows Desktop"
admin_password = module.vmpassword.secretvalue
}
module windows_server_vm_using_local_module {
source = "./vm"
resource_group_name = azurerm_resource_group.rg.name
location = "uksouth"
sloc = "uks"
vm_subnet_id = module.network.vnet_subnets[1]
vm_name = "tfsvlocmod"
vm_size = var.server_vm_size
publisher = var.server_vm_image_publisher
offer = var.server_vm_image_offer
sku = var.server_vm_image_sku
static_ip_address = "10.0.2.15"
activity_tag = "Windows Server"
admin_password = module.vmpassword.secretvalue
}
Within the folder called VM. I have the following files.
main.tf
resource "random_string" "nic_prefix" {
length = 4
special = false
}
resource "azurerm_network_interface" "vm_nic" {
name = "${var.vm_name}-nic1"
location = var.location
resource_group_name = var.resource_group_name
ip_configuration {
name = "${var.vm_name}_nic_${random_string.nic_prefix.result}"
subnet_id = var.vm_subnet_id
private_ip_address_allocation = "Static"
private_ip_address = var.static_ip_address
}
tags = var.tags
}
resource "azurerm_network_interface_security_group_association" "vm_nic_sg" {
network_interface_id = azurerm_network_interface.vm_nic.id
network_security_group_id = var.network_security_group_id
count = var.network_security_group_id == "" ? 0 : 1
}
resource "azurerm_virtual_machine" "windows_vm" {
name = var.vm_name
vm_size = var.vm_size
location = var.location
resource_group_name = var.resource_group_name
tags = merge(var.tags, { activityName = "${var.activity_tag} " })
network_interface_ids = [
"${azurerm_network_interface.vm_nic.id}",
]
storage_image_reference {
publisher = var.publisher
offer = var.offer
sku = var.sku
version = "latest"
}
identity {
type = "SystemAssigned"
}
storage_os_disk {
name = "${var.vm_name}-os-disk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
admin_password = module.vmpassword.secretvalue
admin_username = "azureuser"
computer_name = var.vm_name
}
os_profile_windows_config {
provision_vm_agent = true
}
delete_os_disk_on_termination = var.vm_os_disk_delete_flag
delete_data_disks_on_termination = var.vm_data_disk_delete_flag
}
outputs.tf
output "vm_id" {
value = "${azurerm_virtual_machine.windows_vm.id}"
}
output "vm_name" {
value = "${azurerm_virtual_machine.windows_vm.name}"
}
output "vm_location" {
value = "${azurerm_virtual_machine.windows_vm.location}"
}
output "vm_resource_group_name" {
value = "${azurerm_virtual_machine.windows_vm.resource_group_name}"
}
variables.tf
variable "resource_group_name" {
}
variable "location" {
}
variable "sloc" {
}
variable "vm_size" {
default = "Standard_B1s"
}
variable "vm_subnet_id" {
}
variable "vm_name" {
}
variable "vm_os_disk_delete_flag" {
default = true
}
variable "vm_data_disk_delete_flag" {
default = true
}
variable "network_security_group_id" {
default = ""
}
variable "static_ip_address" {
}
variable "publisher" {
}
variable "offer" {
}
variable "sku" {
}
variable "tags" {
type = map
description = "All mandatory tags to use on all assets"
default = {
activityName = "AzureVMWindowsDemo"
automation = "Terraform"
costCenter1 = "A00000"
dataClassification = "Demo"
managedBy = "example#test.com"
solutionOwner = "example#test.com"
}
}
variable "activity_tag" {
}
variable "admin_password" {
}
Taking what is on the website, I get the following errors.
Error: Reference to undeclared resource
│
│ on vm_module_local_example.tf line 4, in module "windows_desktop_vm_using_local_module":
│ 4: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 7, in module "windows_desktop_vm_using_local_module":
│ 7: vm_subnet_id = module.network.vnet_subnets[0]
│
│ No module call named "network" is declared in the root module.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 9, in module "windows_desktop_vm_using_local_module":
│ 9: vm_size = var.desktop_vm_size
│
│ An input variable with the name "desktop_vm_size" has not been declared. This variable can be declared with a variable "desktop_vm_size" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 10, in module "windows_desktop_vm_using_local_module":
│ 10: publisher = var.desktop_vm_image_publisher
│
│ An input variable with the name "desktop_vm_image_publisher" has not been declared. This variable can be declared with a variable "desktop_vm_image_publisher" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 11, in module "windows_desktop_vm_using_local_module":
│ 11: offer = var.desktop_vm_image_offer
│
│ An input variable with the name "desktop_vm_image_offer" has not been declared. This variable can be declared with a variable "desktop_vm_image_offer" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 12, in module "windows_desktop_vm_using_local_module":
│ 12: sku = var.desktop_vm_image_sku
│
│ An input variable with the name "desktop_vm_image_sku" has not been declared. This variable can be declared with a variable "desktop_vm_image_sku" {} block.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 15, in module "windows_desktop_vm_using_local_module":
│ 15: admin_password = module.vmpassword.secretvalue
│
│ No module call named "vmpassword" is declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│ on vm_module_local_example.tf line 19, in module "windows_server_vm_using_local_module":
│ 19: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 22, in module "windows_server_vm_using_local_module":
│ 22: vm_subnet_id = module.network.vnet_subnets[1]
│
│ No module call named "network" is declared in the root module.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 24, in module "windows_server_vm_using_local_module":
│ 24: vm_size = var.server_vm_size
│
│ An input variable with the name "server_vm_size" has not been declared. This variable can be declared with a variable "server_vm_size" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 25, in module "windows_server_vm_using_local_module":
│ 25: publisher = var.server_vm_image_publisher
│
│ An input variable with the name "server_vm_image_publisher" has not been declared. This variable can be declared with a variable "server_vm_image_publisher" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 26, in module "windows_server_vm_using_local_module":
│ 26: offer = var.server_vm_image_offer
│
│ An input variable with the name "server_vm_image_offer" has not been declared. This variable can be declared with a variable "server_vm_image_offer" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 27, in module "windows_server_vm_using_local_module":
│ 27: sku = var.server_vm_image_sku
│
│ An input variable with the name "server_vm_image_sku" has not been declared. This variable can be declared with a variable "server_vm_image_sku" {} block.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 30, in module "windows_server_vm_using_local_module":
│ 30: admin_password = module.vmpassword.secretvalue
│
│ No module call named "vmpassword" is declared in the root module.
I can see that a lot of the errors relate to undeclared variables. I then create a variable file as follows (not within the vm folder).
variable "subscription_id" {
}
variable "client_id" {
}
variable "client_secret" {
}
variable "tenant_id" {
}
variable "global_settings" {
}
variable "desktop_vm_image_publisher" {
}
variable "desktop_vm_image_offer" {
}
variable "desktop_vm_image_sku" {
}
variable "desktop_vm_image_version" {
}
variable "desktop_vm_size" {
}
variable "server_vm_image_publisher" {
}
variable "server_vm_image_offer" {
}
variable "server_vm_image_sku" {
}
variable "server_vm_image_version" {
}
variable "server_vm_size" {
}
created a file called terraform.auto.tfvars.
# This file should not be checked into source control (add to .gitignore)
subscription_id = "xxxxxxxxxxxxxx"
client_id = "xxxxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxx"
tenant_id = "xxxxxxxxxxxxxx"
## globalsettings
global_settings = {
#Set of tags
tags = {
applicationName = "Windows VM Demo"
businessUnit = "Technical Solutions"
costCenter = "MPN Sponsorship"
DR = "NON-DR-ENABLED"
deploymentType = "Terraform"
environment = "Dev"
owner = "Jack Roper"
version = "0.1"
}
}
# Desktop VM variables
desktop_vm_image_publisher = "MicrosoftWindowsDesktop"
desktop_vm_image_offer = "Windows-10"
desktop_vm_image_sku = "20h1-pro"
desktop_vm_image_version = "latest"
desktop_vm_size = "Standard_B1s"
# Server VM Variables
server_vm_image_publisher = "MicrosoftWindowsServer"
server_vm_image_offer = "WindowsServer"
server_vm_image_sku = "2019-Datacenter"
server_vm_image_version = "latest"
server_vm_size = "Standard_B1s"
Running terraform plan this time, I get the following errors.
Error: Reference to undeclared resource
│
│ on vm_module_local_example.tf line 4, in module "windows_desktop_vm_using_local_module":
│ 4: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 7, in module "windows_desktop_vm_using_local_module":
│ 7: vm_subnet_id = module.network.vnet_subnets[0]
│
│ No module call named "network" is declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 15, in module "windows_desktop_vm_using_local_module":
│ 15: admin_password = module.vmpassword.secretvalue
│
│ No module call named "vmpassword" is declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│ on vm_module_local_example.tf line 19, in module "windows_server_vm_using_local_module":
│ 19: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 22, in module "windows_server_vm_using_local_module":
│ 22: vm_subnet_id = module.network.vnet_subnets[1]
│
│ No module call named "network" is declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 30, in module "windows_server_vm_using_local_module":
│ 30: admin_password = module.vmpassword.secretvalue
│
│ No module call named "vmpassword" is declared in the root module.
╵
Now, It appears I have been extremely unlucky with examples online, in the above there are a lot of missing variables, the author doesnt specify if certain files should be used from a different example (there is a lot of code on the link apparently for different techniques for achieving the same goal). At the same time, I am new to this and wasn't sure if I can truly say there are missing files/details in the solution posted. I have had no luck with contacting the author, no github repo to check that I have the right setup, hence why I have turned here for help. All I want to do is improve my knowledge on terraform modules, I prefer to have explanations and examples so that I can also practice. I could look at git repos, but that would only be giving me solutions without explanation or learning opportunities.
As people in the comments have mentioned it looks like you are following an incomplete or out of date tutorial. I thought it might help you if I explain how to understand the errors produced so you can tackle this hurdle in future.
Error: Reference to undeclared resource
│
│ on vm_module_local_example.tf line 4, in module "windows_desktop_vm_using_local_module":
│ 4: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
In the above message Terraform is saying "On line 4 in the file vm_module_local_example.tf, you've set the resource_group_name to be the value of azurerm_resource_group.rg.name but when I tried to find a resource "azure_resource_group" with the name "rg" I couldn't find anything. To fix it you'd need to code up a resource like:
resource "azurerm_resource_group" "rg" {
name = "example"
location = "West Europe"
}
Once that's done your line 4 will set resource_group_name to be "example".
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 7, in module "windows_desktop_vm_using_local_module":
│ 7: vm_subnet_id = module.network.vnet_subnets[0]
│
│ No module call named "network" is declared in the root module.
This is a good one, on line 7 in the file named vm_module_local_example.tf there is a call to module.network.vnet_subnets[0]. So Terraform will look for a module called network before proceeding further. You'd need code like this:
module "network" {
# pass in whatever the module needs...
}
Based on the error it looks like the module is expected to contain a resource called vnet_subnets generated with count as it's relying on a zero based index (that's the [0] part).
Anyways, hope this helps!

Separate structure for variables.tf gets "Object, known only after apply"

I have recently separated terraform files from it's variable file as per below structure
(root)
| main.tf
| users.tf
| roles.tf
├── Configuration (folder)
├──── azure-pipelines.yml
├──── .gitignore
├──── variables.tf
and since then I am getting bellow error messages
│ Error: Unsupported attribute
│
│ on main.tf line 77, in resource "azurerm_key_vault_secret" "primary_account_storage_access_key":
│ 77: name = "${module.variables.storage-account_name}-access-key"
│ ├────────────────
│ │ module.variables is a object, known only after apply
│
│ This object does not have an attribute named "storage-account_name".
╵
╷
│ Error: Unsupported attribute
│
│ on main.tf line 78, in resource "azurerm_key_vault_secret" "primary_account_storage_access_key":
│ 78: value = module.variables.storage-access_key
│ ├────────────────
│ │ module.variables is a object, known only after apply
│
│ This object does not have an attribute named "storage-access_key".
╵
This is how problematic resource "primary_account_storage_access_key" is defined
resource "azurerm_key_vault_secret" "primary_account_storage_access_key" {
depends_on = [azurerm_key_vault_access_policy.terraform_sp_access]
key_vault_id = data.azurerm_key_vault.azvault.id
name = "${module.variables.storage-account_name}-access-key"
value = module.variables.storage-access_key
}
Module is defined as below:
module "variables" {
source = "./Configuration"
}
I have no issue with utilizing the same module in other resources placed in the same file (main.tf)
terraform {
backend "azurerm" {
resource_group_name = module.variables.storage-resource_group_name
storage_account_name = module.variables.storage-storage_account_name
container_name = module.variables.storage-container_name
key = module.variables.storage-key
}
}
Found articles here on the site focusing on the error suggesting
"splat" operator with "toset" / "one" function, however that did not help:
name = "${toset(module.variables[*].storage-account_name)}-access-key"
value = toset(module.variables[*].storage-access_key)
name = "${one(module.variables[*].storage-account_name)}-access-key"
value = one(module.variables[*].storage-access_key)
Content of variables.tf what child module variables uses:
variable "storage-resource_group_name" {
type = string
default = "Reporting-HFM-integration-rg"
}
variable "storage-account_name" {
type = string
default = "reportinghfmintegration"
}
variable "storage-container_name" {
type = string
default = "tfstate-blob"
}
variable "storage-key" {
type = string
default = "terraform.tfstate"
}
variable "storage-access_key" {
type = string
default = "u3K..."
}
variable "keyVault-name" {
type = string
default = "se-dataplat-dwvault-prod"
}
variable "keyVault-resource_group_name" {
type = string
default = "AzureDataPlatform-dwtools-prod-rg"
}
variable "keyVault-id" {
type = string
default = "/subscriptions/23a89ca1-9743-4b3b-b5ff-41cea9985deb/resourceGroups/..."
}

Output CSV file using terraform

I'm trying to use terraform variable data (CSV file) to create a resource group and the name of the resource group are added into the CSV file.
I'm currently experiencing the below error.
provider "azurerm" {
features{}
}
locals {
resource_groupname = csvdecode(file("./test.csv"))
}
resource "azurerm_resource_group" "Main" {
count = length(locals.resource_groupname)
name = locals.resource_groupname[count.index].groupname
location = "North europe"
}
Error Message
Error: Reference to undeclared resource
│
│ on testvariable.tf line 10, in resource "azurerm_resource_group" "Customer11":
│ 10: count = length(locals.groupname)
│
│ A managed resource "locals" "groupname" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│ on testvariable.tf line 11, in resource "azurerm_resource_group" "Customer11":
│ 11: name = data.locals.groupname[count.index].groupname
│
│ A data resource "locals" "groupname" has not been declared in the root module.
╵
Updated Error Messgae
╷
│ Error: Reference to undeclared resource
│
│ on testtf.tf line 10, in resource "azurerm_resource_group" "Main":
│ 10: count = length(locals.resource_groupname)
│
│ A managed resource "locals" "resource_groupname" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│ on testtf.tf line 11, in resource "azurerm_resource_group" "Main":
│ 11: name = locals.resource_groupname[count.index].groupname
│
│ A managed resource "locals" "resource_groupname" has not been declared in the root module.
Your code should be (assuming this time you posted correct code):
resource "azurerm_resource_group" "Main" {
count = length(local.resource_groupname)
name = local.resource_groupname[count.index].groupname
location = "North europe"
}
Since ./test.csv is not shown its difficult to speculate on its content and the use in your code.
Suppose your CSV file has headers like
name, location, ABC, XXY, CDF, your
then you use for_each as well here
locals {
resource_groupname = csvdecode(file("./test.csv"))
}
resource "azurerm_resource_group" "Main" {
for_each = { for inst in locals.resource_groupname) : inst.location=> inst
}
name = each.value.name
location = each.value.location
}

How can I reference a variable in `main.tf`?

I have two terraform files main.tf and var.tfvars:
main.tf:
provider "google" {
project = var.project_id
region = var.gcp_region
}
provider "aws" {
region = var.aws_region
alias = "aws"
}
vars.tfvars:
variable "gcp_region" {
type = string
default = "asia-southeast1"
}
variable "aws_region" {
type = string
default = "ap-southeast-2"
}
variable "project_id" {
type = string
default = "test-oidc-arosha"
}
when I run terraform apply, I got below error:
Error: Reference to undeclared input variable
│
│ on local.tf line 6, in locals:
│ 6: state_backet = "${local.component_name}-${local.part_name}-deployment-${var.aws_region}-${data.aws_caller_identity.current.account_id}"
│
│ An input variable with the name "aws_region" has not been declared. This variable can be declared
│ with a variable "aws_region" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on main.tf line 2, in provider "google":
│ 2: project = var.project_id
│
│ An input variable with the name "project_id" has not been declared. This variable can be declared
│ with a variable "project_id" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on main.tf line 3, in provider "google":
│ 3: region = var.gcp_region
│
│ An input variable with the name "gcp_region" has not been declared. This variable can be declared
│ with a variable "gcp_region" {} block.
I don't understand why I got this error even I already specified the default values for each variable.
My terraform version is:
$ terraform --version
Terraform v1.0.0
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v3.45.0
+ provider registry.terraform.io/hashicorp/google v3.72.0
Change the name of you vars.tfvars file to variables.tf and it should pick up the default value. .tfvars files are for inputs to variables which are in turn defined in the variables.tf file (or any .tf file actually, doesn't matter what you call it.)
you need to add variable definition to your main.tf and then use your variable in var.tf approapriately
example
variable "aws_region" {
type = string
default = "ap-southeast-2"
}
var.tfvars
example
aws_region = {
Name = "created by jatin/terraform",
instance_type="t2.micro"
}

Resources