Quantcast
Channel: Terraform - HashiCorp Discuss
Viewing all articles
Browse latest Browse all 11363

0.13-rc1 - Reference module that uses for_each

$
0
0

I’m testing out TF 13, and am just getting my head around what is possible or not. The idea here is to create a subnet (in 3 AZ’s) PER module that is being created. Is that technically possible to do from just one subnet resource ? FYI the code I have the the aws_subnet resource is just pseudocode somewhat :slight_smile:. If using for_each is easier to achieve that, then of course that’s possible as well.

locals {
  vpc_cidrs = {
    egress = cidrsubnet(local.root_cidr, 8, 0)
    shared = cidrsubnet(local.root_cidr, 8, 1)
    # starting from the last cidr to keep special cidrs and team account cidrs grouped
    datascience = cidrsubnet(local.root_cidr, 8, 255)
  }
}

module "vpc" {
  source = "terraform-aws-modules/vpc/aws"
  for_each = local.vpc_cidrs
  name                        = each.key
  cidr                        = each.value
  azs                         = local.azs
  enable_flow_log             = true
  tags = {
    Name = each.key
  }
}

resource "aws_subnet" "private" {

  count = length(module.vpc)
  cidr_block        = cidrsubnet(module.vpc.*.vpc_cidr_block[count.index) ,3 , count.index )
  availability_zone = element(local.azs, count.index)
  vpc_id            = element(module.vpc.*.vpc_id[count.index], count.index)
  tags = {
    Name = "${module.vpc.*.name[count.index)}-private"
  }
}

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 11363

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>