简体   繁体   中英

How to reference Terraform module output value as a input value to another module

everyone. I'm using Terraform v1.2.6 with aws provider v4.24.0, Unfortunately. I'm stuck in referencing value of module output from another module.

My VPC module is generate output value for subnet's id which provisioned in 3 AZs as list type.

So, I'd like to use this value for count variable to provision number of ENI resources. But when I assigned it to a variable and change it to a count variable using length() function, count variable is always 0. This is my problem.

Below is the some code snippet for my Terraform code.

module "vpc" {

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

  region_id = "${var.region_id}"
  env = "${var.env}"
  service_code = "${var.service_code}"
  
  common_tag = {
      CreatedBy = "${var.common_tag.CreatedBy}",
      PeriodFrom = "${var.common_tag.PeriodFrom}",
      PeriodTo = "${var.common_tag.PeriodTo}",
      CostCenter = "${var.common_tag.CostCenter}",
      Project = "${var.common_tag.Project}"
  }


  vpc = {
      cidr_block = "${var.vpc.cidr_block}", 
      protected_subnet_a_cidr = "${var.vpc.protected_subnet_a_cidr}", 
      protected_subnet_b_cidr = "${var.vpc.protected_subnet_b_cidr}" 
      protected_subnet_c_cidr = "${var.vpc.protected_subnet_c_cidr}",
      tgw_subnet_a_cidr = "${var.vpc.tgw_subnet_a_cidr}",
      tgw_subnet_b_cidr = "${var.vpc.tgw_subnet_b_cidr}",
      tgw_subnet_c_cidr = "${var.vpc.tgw_subnet_c_cidr}",
      workload_subnet_a_cidr = "${var.vpc.workload_subnet_a_cidr}", 
      workload_subnet_b_cidr = "${var.vpc.workload_subnet_b_cidr}",
      workload_subnet_c_cidr = "${var.vpc.workload_subnet_c_cidr}", 
      redis_subnet_a_cidr = "${var.vpc.redis_subnet_a_cidr}",
      redis_subnet_b_cidr = "${var.vpc.redis_subnet_b_cidr}",
      redis_subnet_c_cidr = "${var.vpc.redis_subnet_c_cidr}",
      db_subnet_a_cidr = "${var.vpc.db_subnet_a_cidr}",
      db_subnet_b_cidr = "${var.vpc.db_subnet_b_cidr}",
      db_subnet_c_cidr = "${var.vpc.db_subnet_c_cidr}"
  }

}
 


module "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.vpc.protected_subnet_ids

  use_managed_nat = true

  depends_on = [module.wink_vpc]
  
 
}

Here is the terraform console command result.

❯ terraform console
> module.vpc
{
  "db_subnet_ids" = [
    "subnet-036b19638fd57d03b",
    "subnet-0989e9ecb9ca2a146",
    "subnet-060db32b53559ecfb",
  ]
  "db_subnet_route_table_ids" = [
    "rtb-0b3b9b2726675bc9b",
    "rtb-0bba77cb702e60156",
    "rtb-0cce30f5509eb3505",
  ]
  "igw_id" = "igw-01fd17f6c7c98059a"
  "protected_subnet_ids" = [
    "subnet-0ac643148503ed0b5",
    "subnet-039f527ad5e194b01",
    "subnet-07523fedcb50084a3",
  ]
  "protected_subnet_route_table_ids" = [
    "rtb-0b58c7fc7cd4f8957",
    "rtb-00f85f75400856328",
    "rtb-04663eb73a6a1ff87",
  ]
  "redis_subnet_ids" = [
    "subnet-0dfe56e866f34bc77",
    "subnet-013b60ff183f7192f",
    "subnet-0e51997c9f4548b8a",
  ]
  "redis_subnet_route_table_ids" = [
    "rtb-03ce16be7d7d5538c",
    "rtb-094ed4728b7f4dff7",
    "rtb-0982f508e27559720",
  ]
  "tgw_subnet_ids" = [
    "subnet-0c1508e5e554213e9",
    "subnet-0c0bd659eddac8e0d",
    "subnet-042d5ee2600ba360e",
  ]
  "tgw_subnet_route_table_ids" = [
    "rtb-01b5b1374a1cc161e",
    "rtb-0e2d7cdda12ae23f8",
    "rtb-02291036ba98b4b25",
  ]
  "vpc_id" = "vpc-034f718a9ca96bc95"
  "workload_subnet_ids" = [
    "subnet-087dd412ade2d7b79",
    "subnet-01d0466a843c8249b",
    "subnet-048c4a07d0d1a9fbd",
  ]
  "workload_subnet_route_table_ids" = [
    "rtb-0aafae15877f17c77",
    "rtb-08d65c618047f6007",
    "rtb-05a70c48904e6f7a3",
  ]
}
> module.vpc.protected_subnet_ids
[
  "subnet-0ac643148503ed0b5",
  "subnet-039f527ad5e194b01",
  "subnet-07523fedcb50084a3",
]
>

Code snippets in the NAT module..

...

resource "aws_eip" "nat-gw-eip" {
  count = length(var.target_subnets)
  vpc      = true
}

....

The result of count is always ZERO.

I'm curious why count variable is ZERO. It should be THREE, because module.vpc.protected_subnet_ids had three items in the list.

Please let me know the reason and how to I get the proper result.

Thank you in advance.

Hello, everyone

It's my mistake. Finally, I've found my code snippet which assign empty list [] to a variable target_subnets . Thank you for your time.

Anyway, your comments are very helpful to me.

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM