Skip to content

Commit

Permalink
add subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed May 3, 2021
1 parent 9b6ab94 commit de94326
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 29 deletions.
1 change: 1 addition & 0 deletions subnets/availabilty_zones.tf
1 change: 1 addition & 0 deletions subnets/data.tf
1 change: 1 addition & 0 deletions subnets/defaults.tf
109 changes: 109 additions & 0 deletions subnets/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* # About aws-vpc-setup :: subnets
*
* This submodule creates public and private subnets.
*
* # Usage
*
* ```hcl
* module "subnets" {
* source = "git@github.e.it.census.gov:terraform-modules/aws-vpc-setup.git//subnets"
* availability_zones = var.availability_zones
* public_subnets = [ { base_cidr = "10.188.16.0/24", label = "public", bits = 2, private = false } ]
* private_subnets = [
* { base_cidr = "10.188.18.0/23", label = "private-lb", bits = 2, private = true },
* { base_cidr = "10.188.20.0/23", label = "db", bits = 2, private = true },
* { base_cidr = "10.188.22.0/23", label = "apps", bits = 2, private = true } ]
*
* vpc_name = var.vpc_name
* vpc_cidr_block = var.vpc_cidr_block
* vpc_index = var.vpc_index
* vpc_short_name = var.vpc_short_name
* vpc_full_name = var.vpc_full_name
* vpc_environment = var.vpc_environment
*
* tags = {}
* }
*/

locals {
account_id = var.account_id != "" ? var.account_id : data.aws_caller_identity.current.account_id
account_environment = data.aws_arn.current.partition == "aws-us-gov" ? "gov" : "ew"

base_tags = {
"boc:tf_module_version" = local._module_version
"boc:created_by" = "terraform"
}

availability_zones = length(var.availability_zones) != 0 ? var.availability_zones : data.aws_availability_zones.zones.names
az_count = length(local.availability_zones)
az_count_list = range(local.az_count)
az_list = toset(local.availability_zones)
empty = toset([])
}

#---
# public subnets
#---
locals {
public_subnets = { for v in var.public_subnets : v.label =>
{
base_cidr = v.base_cidr
label = v.label
bits = v.bits
private = v.private
subnets = [for i in local.az_count_list : cidrsubnet(v.base_cidr, v.bits, i)]
labels = [for az in local.availability_zones : format("%s-%s", v.label, az)]
availability_zones = local.availability_zones
}
}
public_map = flatten([for k, v in local.public_subnets :
[for i in local.az_count_list : tomap({ "subnet" = v.subnets[i], "label" = v.labels[i], "availability_zone" = v.availability_zones[i] })]])
}


resource "aws_subnet" "public" {
for_each = { for subnet in local.public_map : subnet.label => subnet }
vpc_id = var.vpc_id
cidr_block = each.value.subnet
availability_zone = each.value.availability_zone

tags = merge(
local.base_tags,
var.tags,
map("Name", format("%v%v-%v", local._prefixes["subnet"], var.vpc_full_name, each.value.label))
)
}


#---
# private subnets
#---
locals {
private_subnets = { for v in var.private_subnets : v.label =>
{
base_cidr = v.base_cidr
label = v.label
bits = v.bits
private = v.private
subnets = [for i in local.az_count_list : cidrsubnet(v.base_cidr, v.bits, i)]
labels = [for az in local.availability_zones : format("%s-%s", v.label, az)]
availability_zones = local.availability_zones
}
}
private_map = flatten([for k, v in local.private_subnets :
[for i in local.az_count_list : tomap({ "subnet" = v.subnets[i], "label" = v.labels[i], "availability_zone" = v.availability_zones[i] })]])
}

resource "aws_subnet" "private" {
for_each = { for subnet in local.private_map : subnet.label => subnet }
vpc_id = var.vpc_id
cidr_block = each.value.subnet
availability_zone = each.value.availability_zone

tags = merge(
local.base_tags,
var.tags,
map("Name", format("%v%v-%v", local._prefixes["subnet"], var.vpc_full_name, each.value.label))
)
}
1 change: 1 addition & 0 deletions subnets/prefixes.tf
1 change: 1 addition & 0 deletions subnets/variables.common.tf
1 change: 1 addition & 0 deletions subnets/variables.common.vpc.tf
33 changes: 33 additions & 0 deletions subnets/variables.subnets.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
variable "availability_zones" {
description = "AWS Availability Zones to use (by default will use all available)"
type = list(string)
default = []
}

variable "public_subnets" {
description = "List of objects with public subnet information to be created"
type = list(object({
base_cidr = string
label = string
bits = number
private = bool
# subnets = list(string)
# labels = list(string)
# availability_zones = list(string)
}))
default = []
}

variable "private_subnets" {
description = "List of objects with private subnet information to be created"
type = list(object({
base_cidr = string
label = string
bits = number
private = bool
# subnets = list(string)
# labels = list(string)
# availability_zones = list(string)
}))
default = []
}
1 change: 1 addition & 0 deletions subnets/version.tf
30 changes: 1 addition & 29 deletions vpc/locals.tf
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
locals {
az_list = data.aws_availability_zones.zones.names
az_count = length(local.az_list)
az_count_list = range(local.az_count)
region = data.aws_region.current.name
}

data "aws_availability_zones" "zones" {
state = "available"
}

data "aws_availability_zone" "zone" {
count = length(data.aws_availability_zones.zones.names)
state = "available"
name = data.aws_availability_zones.zones.names[count.index]
}

output "availability_zone_names" {
description = "VPC Availability zone name list (3)"
value = data.aws_availability_zones.zones.names
}

output "availability_zone_ids" {
description = "VPC Availability zone id list (3)"
value = data.aws_availability_zones.zones.zone_ids
}

output "availability_zone_suffixes" {
description = "VPC Availability zone suffix list (3)"
value = data.aws_availability_zone.zone[*].name_suffix
region = data.aws_region.current.name
}

0 comments on commit de94326

Please sign in to comment.