Sungshik Jou
Sungshik Jou

Reputation: 85

Terraform "Unsupported attribute" error when access output variable of sub-module

Gurus!

I'm under developing Terraform modules to provision NAT resource for production and non-production environment. There are two repositories one for Terraform modules another for the live environment for each account (ex: dev, stage, prod..)

I have an problem when access output variable of network/nat module. It makes me very tired. Please refer below.

❯ tree sre-iac-modules/network/nat/
sre-iac-modules/network/nat/
├── main.tf
├── non_production
│   └── main.tf
├── outputs.tf
├── production
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
└── variables.tf

❯ tree sre-iac-modules/network/nat/
sre-iac-modules/network/nat/
├── main.tf
├── non_production
│   └── main.tf
├── outputs.tf
├── production
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
└── variables.tf

In the main code snippet, sre-iac-live/dev/services/wink/network/main.tf I cannot access output variable named module.wink_nat.eip_ids. When I run terraform plan or terraform console, always I reached following error.

│ Error: Unsupported attribute
│
│   on ../../../../../sre-iac-modules/network/nat/outputs.tf line 2, in output "eip_ids":
│    2:     value = module.production.eip_ids
│     ├────────────────
│     │ module.production is tuple with 1 element
│
│ This value does not have any attributes.
╵

Here is the ../../../../../sre-iac-modules/network/nat/outputs.tf and main.tf

output "eip_ids" {
    value = module.production.eip_ids
#    value = ["a", "b", "c"]
}

----
main.tf


module "production" {
  source = "./production"
  count = var.is_production ? 1 : 0
  env    = ""
  region_id = ""
  service_code = ""
  target_route_tables = []
  target_subnets = var.target_subnets
}


module "non_production" {
  source = "./non_production"

  count = var.is_production ? 0 : 1
}

However, if I use value = ["a", "b", "c"] then it works well! I couldn't re what is the problem.

Below is the code snippet of ./sre-iac-modules/network/nat/production/outputs.tf

output "eip_ids" {
    value = aws_eip.for_nat[*].id
#    value = [aws_eip.nat-gw-eip.*.id]
#    value = aws_eip.for_nat.id
#    value = ["a", "b", "c"]
}

Below is the code snippet of ./sre-iac-modules/network/nat/production/main.tf

resource "aws_eip" "for_nat" {
  count = length(var.target_subnets)
  vpc      = true
}


And finally, here is the main.tf code snippet. (sre-iac-live/dev/services/wink/network/main.tf)


module "wink_vpc" {
  .... skip ....
}



module "wink_nat" {

  # Relative path references
  source = "../../../../../sre-iac-modules/network/nat"

  region_id = "${var.region_id}"
  env = "${var.env}"
  service_code = "${var.service_code}"
  
  target_subnets = module.wink_vpc.protected_subnet_ids

  is_production = true

  depends_on = [module.wink_vpc]
  
 
}

I'm stuck this issue for one day. I needs Terraform Guru's help.

Please give me your great advice. Thank you so much in advance.

Cheers!

Upvotes: 2

Views: 3398

Answers (1)

Mark B
Mark B

Reputation: 201048

Your production module has a count meta attribute. To reference the module you have to use an index, like:

value = module.production[0].eip_ids

Upvotes: 3

Related Questions