Skip to content
Snippets Groups Projects
Commit 9634c069 authored by nimrod's avatar nimrod
Browse files

Allow deploying the VPC from scratch.

Terraform can't deploy resource with for_each if some of the information
is missing when building the plan. A workaround is to avoid having a
resource with for_each depend on a resource witha a count.
parent 2638eac3
No related branches found
No related tags found
No related merge requests found
...@@ -88,6 +88,7 @@ locals { ...@@ -88,6 +88,7 @@ locals {
8 = "h" 8 = "h"
9 = "i" 9 = "i"
} }
subnet_mapping = { for i in range(var.subnet_count) : i => local.az_mapping[i + 1] }
} }
resource "aws_subnet" "private" { resource "aws_subnet" "private" {
...@@ -124,21 +125,21 @@ output "private_subnet_names" { ...@@ -124,21 +125,21 @@ output "private_subnet_names" {
} }
resource "aws_subnet" "public" { resource "aws_subnet" "public" {
count = var.subnet_count for_each = local.subnet_mapping
availability_zone = "${var.region}${local.az_mapping[count.index + 1]}" availability_zone = "${var.region}${each.value}"
cidr_block = cidrsubnet(var.cidr_block, 8, var.subnet_count + count.index) cidr_block = cidrsubnet(var.cidr_block, 8, var.subnet_count + each.key)
map_public_ip_on_launch = true map_public_ip_on_launch = true
vpc_id = local.vpc_id vpc_id = local.vpc_id
tags = { tags = {
Name = "${local.env}-public-${local.az_mapping[count.index + 1]}" Name = "${local.env}-public-${each.value}"
Type = "public" Type = "public"
} }
} }
locals { locals {
public_subnet_arns = aws_subnet.public.*.arn public_subnet_arns = [for i in aws_subnet.public : i.arn]
public_subnet_ids = aws_subnet.public.*.id public_subnet_ids = [for i in aws_subnet.public : i.id]
public_subnet_names = [for i in aws_subnet.public.*.tags : i["Name"]] public_subnet_names = [for i in aws_subnet.public : i.tags["Name"]]
} }
output "public_subnet_arns" { output "public_subnet_arns" {
...@@ -183,17 +184,23 @@ output "nat_gateway_eip_names" { ...@@ -183,17 +184,23 @@ output "nat_gateway_eip_names" {
} }
resource "aws_nat_gateway" "gateway" { resource "aws_nat_gateway" "gateway" {
count = var.subnet_count for_each = {
allocation_id = local.nat_gateway_eip_ids[count.index] for i in range(var.subnet_count) : i => {
subnet_id = local.public_subnet_ids[count.index] nat_eip_id = local.nat_gateway_eip_ids[i]
subnet_id = local.public_subnet_ids[i]
az = local.az_mapping[i + 1]
}
}
allocation_id = each.value.nat_eip_id
subnet_id = each.value.subnet_id
tags = { tags = {
Name = "${local.env}-${local.az_mapping[count.index + 1]}" Name = "${local.env}-${each.value.az}"
} }
} }
locals { locals {
nat_gateway_ids = aws_nat_gateway.gateway.*.id nat_gateway_ids = [for i in aws_nat_gateway.gateway : i.id]
nat_gateway_names = [for i in aws_nat_gateway.gateway.*.tags : i["Name"]] nat_gateway_names = [for i in aws_nat_gateway.gateway : i.tags["Name"]]
} }
output "nat_gateway_ids" { output "nat_gateway_ids" {
...@@ -240,9 +247,9 @@ output "public_route_table_name" { ...@@ -240,9 +247,9 @@ output "public_route_table_name" {
} }
resource "aws_route_table_association" "public" { resource "aws_route_table_association" "public" {
for_each = toset(local.public_subnet_ids) for_each = aws_subnet.public
route_table_id = local.public_route_table_id route_table_id = local.public_route_table_id
subnet_id = each.key subnet_id = each.value.id
} }
locals { locals {
...@@ -255,10 +262,10 @@ output "public_route_table_association_ids" { ...@@ -255,10 +262,10 @@ output "public_route_table_association_ids" {
} }
resource "aws_route_table" "private" { resource "aws_route_table" "private" {
for_each = toset(local.nat_gateway_ids) for_each = aws_nat_gateway.gateway
vpc_id = local.vpc_id vpc_id = local.vpc_id
tags = { tags = {
Name = "${local.env}-private-${local.az_mapping[index(local.nat_gateway_ids, each.key) + 1]}" Name = "${local.env}-private-${local.az_mapping[index(local.nat_gateway_ids, each.value.id) + 1]}"
} }
route { route {
...@@ -289,9 +296,9 @@ output "private_route_table_names" { ...@@ -289,9 +296,9 @@ output "private_route_table_names" {
} }
resource "aws_route_table_association" "private" { resource "aws_route_table_association" "private" {
for_each = zipmap(local.private_subnet_ids, local.private_route_table_ids) for_each = aws_route_table.private
route_table_id = each.value route_table_id = each.value.id
subnet_id = each.key subnet_id = local.private_subnet_ids[index(local.private_route_table_ids, each.value.id)]
} }
locals { locals {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment