Skip to content

Commit

Permalink
make subnet cidr block assignment reproducible
Browse files Browse the repository at this point in the history
  • Loading branch information
dfry committed Mar 17, 2022
1 parent f17d7eb commit 654205a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
locals {
all_pub_subnets = merge({"management" = data.aws_availability_zones.available.names[0]}, {for pair in setproduct(var.environments, data.aws_availability_zones.available.names) : "${pair[0]}-${pair[1]}" => pair[1]})
all_priv_subnets = {for pair in setproduct(var.environments, data.aws_availability_zones.available.names) : "${pair[0]}-${pair[1]}" => pair[1]}
all_pub_subnets = merge({"management" = {env = "management", az = data.aws_availability_zones.available.names[0], cidr_block_index = 0}}, {for pair in setproduct(var.environments, data.aws_availability_zones.available.names) : "${pair[0]}-${pair[1]}" => {env = pair[0], az = pair[1], cidr_block_index = 1 + index(data.aws_availability_zones.available.names, pair[1]) + (var.cidr_block_index[pair[0]]) * length(data.aws_availability_zones.available.names)}})
all_priv_subnets = {for pair in setproduct(var.environments, data.aws_availability_zones.available.names) : "${pair[0]}-${pair[1]}" => {env = pair[0], az = pair[1], cidr_block_index = index(data.aws_availability_zones.available.names, pair[1]) + (var.cidr_block_index[pair[0]]) * length(data.aws_availability_zones.available.names)}}
}

module "ubuntu-bionic-ami" {
Expand Down
4 changes: 2 additions & 2 deletions modules/named-subnets/private.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module "private_label" {
resource "aws_subnet" "private" {
for_each = local.private_subnet_az_map
vpc_id = var.vpc_id
availability_zone = each.value
cidr_block = cidrsubnet(var.cidr_block, ceil(log(var.max_subnets, 2)), index(keys(local.private_subnet_az_map), each.key))
availability_zone = each.value.az
cidr_block = cidrsubnet(var.cidr_block, ceil(log(var.max_subnets, 2)), each.value.cidr_block_index)

tags = {
"Name" = "${module.private_label.id}${var.delimiter}${each.key}"
Expand Down
4 changes: 2 additions & 2 deletions modules/named-subnets/public.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module "public_label" {
resource "aws_subnet" "public" {
for_each = local.public_subnet_az_map
vpc_id = var.vpc_id
availability_zone = each.value
cidr_block = cidrsubnet(var.cidr_block, ceil(log(var.max_subnets, 2)), index(keys(local.public_subnet_az_map), each.key))
availability_zone = each.value.az
cidr_block = cidrsubnet(var.cidr_block, ceil(log(var.max_subnets, 2)), each.value.cidr_block_index)

tags = {
"Name" = "${module.public_label.id}${var.delimiter}${each.key}"
Expand Down
8 changes: 8 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ variable "environments" {
default = ["dev"]
}

variable "cidr_block_index" {
description = "map of environments for the tenatn. Each environment will contain an instance of Mojaloop and associated services."
type = map(number)
default = {
"dev" = 0
}
}

variable "public_subnets" {
default = ["gateway", "management", "simulators"]
type = list(string)
Expand Down

0 comments on commit 654205a

Please sign in to comment.