Skip to content

Instantly share code, notes, and snippets.

@trondhindenes
Last active March 4, 2020 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trondhindenes/d36f08883c752debdc6e021c4f8e8ee5 to your computer and use it in GitHub Desktop.
Save trondhindenes/d36f08883c752debdc6e021c4f8e8ee5 to your computer and use it in GitHub Desktop.
# Lots of other resources are created and all of that stuff works so I'm just skipping the not-so-relevant parts.
resource "aws_route_table" "private" {
for_each = toset(var.public_subnets)
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.natgw[each.key].id #this ressource exists andd is based on a foreach of the public_subnets variable, I just omitted it from the example here.
}
tags = {
Name = "privateroutes-${split(":", each.value)[0]}"
}
}
resource "aws_subnet" "privsubnet" {
for_each = toset(var.private_subnets)
vpc_id = aws_vpc.main.id
cidr_block = split(":", each.value)[1]
availability_zone = split(":", each.value)[0]
tags = {
Name = "${split(":", each.value)[2]}"
}
}
# variable contents:
#public_subnets=[
# "eu-west-1a:10.0.3.0/24:what1",
# "eu-west-1b:10.0.4.0/24:what2",
# "eu-west-1b:10.0.5.0/24:what3"
#]
#private_subnets=[
# "eu-west-1a:10.0.6.0/24:priv1",
# "eu-west-1b:10.0.8.0/24:priv2",
# "eu-west-1c:10.0.9.0/24:priv3",
# "eu-west-1a:10.0.10.0/24:priv4",
#]
# This is where I'm stuck right now. For example:
# For the private subnet "eu-west-1a:10.0.6.0/24:priv1",
# we need to grab the id of the route table called "privateroutes-eu-west-1a",
# and set that as the route_table_id. The "eu-west-1a" substring is accessible by splitting the current loop value: `split(":", each.value)[0]`
#resource "aws_route_table_association" "privateroute_assoc" {
# for_each = toset(var.private_subnets)
# subnet_id = aws_subnet.privsubnet[each.key].id
# route_table_id = aws_route_table.private["privateroutes-${split(":", each.value)[0]}"].id
#}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment