Search code examples
terraformdatadoghcl

Why am I getting this error for reference to undeclared input variable


I'm trying to create a basic dashboard using terraform in my front end project.

Here is my folder structure:

├── infrastructure
│   ├── environments
│   │  ├── production
│   │     ├── backend.tvars
│   │     ├── variable.tvars
│   │   
│   │    
│   ├── modules
│       ├── datadog-dashboard
│       │   ├── main.tf
│       │   ├── variables.tf
│       │  
│       └── datadog-monitors
│   
├── backend.tf
├── main.tf
├── provider.tf
├── terraform.auto.tvars
├── variable.tfvars

When I run my pipeline, I get errors for variables in my main.tf at the root

for example:

 Error: Reference to undeclared input variable
  │ 
  │   on main.tf line 10, in module "rna_webapp_service_account":
  │   10:   service      = var.service
  │ 
  │ An input variable with the name "service" has not been declared. 
    This variable can be declared with a variable "service" {} 
    block.

Looking at my main.tf module:

module "rna_webapp_service_account" {
    source = "git::git..."

  service_account_name_override = "website ${var.environment_short}"
  display_name = "website ${var.environment_short}"
  description  = "Service account for website ${var.environment_short}"

  service      = var.service
  environment  = var.environment_short
  location     = var.location

From my understanding the variables.tf in the root define these variables kind of like a schema. For service it looks like this:

variable "service" {
  description = "Name of the service the infrastructure is for"
}

so if service has been declared within a variable service {} block why am I getting this error?


Solution

  • From my understanding the variables.tf in the root define these variables kind of like a schema. For service it looks like this:

    That just declares the variable for use at the root level. You also need to declare it in the rna_webapp_service_account module if you want to pass it into that module.

    The error is telling you that the module you are using has no input named service.