diff --git a/README.md b/README.md index 91c543d..fceaca5 100644 --- a/README.md +++ b/README.md @@ -95,11 +95,7 @@ No modules. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| [availability\_zones](#input\_availability\_zones) | Target availability zones. | `list(string)` | `null` | no | -| [cidr\_block](#input\_cidr\_block) | The IPv4 CIDR block for the VPC. | `string` | `null` | no | | [network\_acl\_disabled](#input\_network\_acl\_disabled) | If disabled, default Network ACLs allow all inbound and outbound protocols. | `bool` | `true` | no | -| [prefix\_name](#input\_prefix\_name) | Value used to prefix resources name. | `string` | `null` | no | -| [subnet\_groups](#input\_subnet\_groups) | One subnet in each availablity zone is created per subnet group. |
map(object({
cidr_block = string
default_route = optional(string, "")
}))
| `null` | no | ## Outputs diff --git a/config/default.yaml b/config/default.yaml new file mode 100644 index 0000000..a5ba21b --- /dev/null +++ b/config/default.yaml @@ -0,0 +1,8 @@ +cidr_block: 10.1.0.0/16 +max_azs: 2 + +subnet_groups: + public: + default_route: igw + private: + default_route: nat/public diff --git a/config/eks.yaml b/config/eks.yaml new file mode 100644 index 0000000..5bad076 --- /dev/null +++ b/config/eks.yaml @@ -0,0 +1,13 @@ + +cidr_block: 10.0.0.0/16 +max_azs: 2 + +subnet_groups: + public: + default_route: igw + tags: + kubernetes.io/role/elb: 1 + private: + default_route: nat/public + tags: + kubernetes.io/role/internal-elb: 1 diff --git a/main.tf b/main.tf index ee8f51b..44dce0c 100644 --- a/main.tf +++ b/main.tf @@ -5,24 +5,22 @@ data "aws_availability_zones" "current" { } locals { + config_file = "${path.module}/config/default.yaml" + config = yamldecode(file(local.config_file)) # Configure defaults when attributes are not defined - prefix_name = var.prefix_name != null ? var.prefix_name : basename(path.cwd) - cidr_block = var.cidr_block != null ? var.cidr_block : "10.0.0.0/16" - azs = var.availability_zones != null ? var.availability_zones : slice(data.aws_availability_zones.current.names, 0, 2) - - nets = var.subnet_groups != null ? var.subnet_groups : { - public = { - cidr_block = cidrsubnet(local.cidr_block, 1, 0) - default_route = "igw" - } - private = { - cidr_block = cidrsubnet(local.cidr_block, 1, 1) - default_route = "ngw/public" - } - } + prefix_name = try(local.config.prefix_name, basename(path.cwd)) + cidr_block = try(local.config.cidr_block, "10.0.0.0/16") + azs_count = min(try(local.config.max_azs, 5), length(data.aws_availability_zones.current.names)) + azs = try(local.config.availability_zones, slice(data.aws_availability_zones.current.names, 0, local.azs_count)) + nets = try(local.config.subnet_groups, {}) # subnets - subnet_newbits = ceil(log(length(local.azs), 2)) + nets_newbits = ceil(log(length(local.nets), 2)) + azs_newbits = ceil(log(length(local.azs), 2)) + + net_cidr_blocks = { + for i, v in keys(local.nets) : v => try(local.nets[v].cidr_block, cidrsubnet(local.cidr_block, local.nets_newbits, i)) + } subnets_attributes = merge(flatten([ for k, v in local.nets : [ @@ -30,18 +28,19 @@ locals { join("-", [k, z]) = { availability_zone = z subnet_group = k - default_route = v.default_route - cidr_block = cidrsubnet(v.cidr_block, local.subnet_newbits, i) + default_route = try(v.default_route, "") + cidr_block = cidrsubnet(local.net_cidr_blocks[k], local.azs_newbits, i) + tags = try(v.tags, { test : "none" }) } } ] ])...) # internet gateway - igw_enabled = contains([for g in local.nets : g.default_route == "igw"], true) + igw_enabled = contains([for g in local.nets : try(g.default_route == "igw", false)], true) # NAT gateways - nat_subnet_groups = distinct([for k, v in local.nets : basename(v.default_route) if startswith(v.default_route, "ngw/")]) + nat_subnet_groups = distinct([for g in local.nets : basename(g.default_route) if try(startswith(g.default_route, "ngw/"), false)]) nat_gateways = toset(flatten([ for k in local.nat_subnet_groups : [ for i, z in local.azs : join("-", [k, z]) diff --git a/subnets.tf b/subnets.tf index a3e26ea..48c38aa 100644 --- a/subnets.tf +++ b/subnets.tf @@ -6,9 +6,9 @@ resource "aws_subnet" "self" { cidr_block = each.value.cidr_block availability_zone = each.value.availability_zone - tags = { + tags = merge(each.value.tags, { Name = join("-", compact([local.prefix_name, each.key])) - } + }) } resource "aws_eip" "self" { diff --git a/variables.tf b/variables.tf index 3552950..5e48005 100644 --- a/variables.tf +++ b/variables.tf @@ -1,32 +1,5 @@ # variables.tf -variable "prefix_name" { - description = "Value used to prefix resources name." - type = string - default = null -} - -variable "cidr_block" { - description = "The IPv4 CIDR block for the VPC." - type = string - default = null -} - -variable "availability_zones" { - description = "Target availability zones." - type = list(string) - default = null -} - -variable "subnet_groups" { - description = "One subnet in each availablity zone is created per subnet group." - type = map(object({ - cidr_block = string - default_route = optional(string, "") - })) - default = null -} - variable "network_acl_disabled" { description = "If disabled, default Network ACLs allow all inbound and outbound protocols." type = bool