2

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.

  • for Terraform module (sre-iac-module repo)
❯ 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

  • for live environment (sre-iac-modules repo)
❯ 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!

3
  • 1
    Your production module has a count attribute. Please try: value = module.production[0].eip_ids Commented Aug 18, 2022 at 15:46
  • @MarkB, Oh... Thank you for teaching me. I'm not familiar with Terraform yet. Your comments have been a great help to me. It works well now! Commented Aug 18, 2022 at 22:59
  • @MarkB Please leave your comment as a answer. I will choose your answer :) Thank you so much again. Commented Aug 19, 2022 at 1:11

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.