0

I create resource "aws_subnet" using count expression. I'd like to use for_each instead of count, but need help with the correct syntax.

variable "privateSubnetCIDR" {
      type = list(string)
      default = ["10.0.1.0/24","10.0.2.0/24"]
    }
data "aws_availability_zones" "availableAZ" {}
    
resource "aws_subnet" "privatesubnet" {
  count                   = length(var.privateSubnetCIDR)
  cidr_block              = tolist(var.privateSubnetCIDR)[count.index]
  vpc_id                  = aws_vpc.dev_vpc.id
  map_public_ip_on_launch = false
  availability_zone       = data.aws_availability_zones.availableAZ.names[count.index]
  tags = {
    name        = "${var.environment}-privatesubnet-${count.index + 1}"
    AZ          = data.aws_availability_zones.availableAZ.names[count.index]
    Environment = "${var.environment}-privatesubnet"
  }
}
4
  • You forgot to explain what's wrong with your code? Any errors? Commented May 4, 2022 at 6:58
  • No errors, it works. I just want to understand how to use for_each in this case. Commented May 4, 2022 at 7:15
  • You are already using for each in for_each = toset(var.publicSubnetCIDR)? So if it works, what's the problem? Commented May 4, 2022 at 7:16
  • My fault, posted incorrect code. Here is the working example with count Commented May 4, 2022 at 7:21

1 Answer 1

2

One way to use for_each would be as follows:

resource "aws_subnet" "privatesubnet" {
  for_each                = toset(var.privateSubnetCIDR)
  cidr_block              = each.key
  vpc_id                  = aws_vpc.dev_vpc.id
  map_public_ip_on_launch = false
  availability_zone       = element(data.aws_availability_zones.availableAZ.names, index(var.privateSubnetCIDR, each.key))
  tags = {
    name        = "environment-privatesubnet-${index(var.privateSubnetCIDR, each.key) + 1}"
    AZ          = element(data.aws_availability_zones.availableAZ.names, index(var.privateSubnetCIDR, each.key))
    Environment = "environment-privatesubnet"
  }
}

The simpler way would be:

resource "aws_subnet" "privatesubnet" {
  for_each                = {for idx, val in var.privateSubnetCIDR: idx => val}
  cidr_block              = each.value
  vpc_id                  = aws_vpc.dev_vpc.id
  map_public_ip_on_launch = false
  availability_zone       = element(data.aws_availability_zones.availableAZ.names, each.key)
  tags = {
    name        = "environment-privatesubnet-${each.key + 1}"
    AZ          = element(data.aws_availability_zones.availableAZ.names, each.key)
    Environment = "environment-privatesubnet"
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Now I have troubles when trying to get subnet_id in the next block: resource "aws_route_table_association" "nat_routeTableAssociation" { count = length(var.privateSubnetCIDR) route_table_id = aws_route_table.nat_routetable[count.index].id subnet_id = aws_subnet.privatesubnet[count.index].id I can't use for_each here, because route_table uses count. Is there any way to get subnet_id without for_each in this block?
@bohdan If you have new issues, new SO question should be made. My answer correctly addresses your current question.

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.