diff --git a/README.md b/README.md index ddae59c..c310bc1 100644 --- a/README.md +++ b/README.md @@ -13,10 +13,11 @@ module "network" { subnet_groups = { dmz = { - public = true - cidr_block = "10.0.0.0/26" + default_route = "internet_gateway" + cidr_block = "10.0.0.0/26" } frontend = { + default_route = "nat_gateway.pub" cidr_block = "10.0.16.0/20" } backend = { @@ -36,19 +37,19 @@ __cidr_block__ is mandatory for VPC, all subnet_groups must be given a __cidr_bl into several sub-blocks to have a subnet per availability zone on each groups. Group's __cidr_block_ must not overlap each others and must be included into VPC's __cidr_block__. - + ## Requirements | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >=1.0.0 | +| [terraform](#requirement\_terraform) | >=1.3.0 | | [aws](#requirement\_aws) | >=4.0 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | 5.33.0 | +| [aws](#provider\_aws) | 5.47.0 | ## Modules @@ -62,10 +63,12 @@ No modules. | [aws_default_route_table.self](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_route_table) | resource | | [aws_default_security_group.self](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_security_group) | resource | | [aws_default_vpc_dhcp_options.self](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_vpc_dhcp_options) | resource | +| [aws_eip.self](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip) | resource | | [aws_internet_gateway.self](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/internet_gateway) | resource | +| [aws_nat_gateway.self](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/nat_gateway) | resource | | [aws_network_acl_rule.all_egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | | [aws_network_acl_rule.all_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/network_acl_rule) | resource | -| [aws_route_table.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table) | resource | +| [aws_route_table.self](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table) | resource | | [aws_route_table_association.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table_association) | resource | | [aws_subnet.self](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource | | [aws_vpc.self](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc) | resource | @@ -79,7 +82,7 @@ No modules. | [cidr\_block](#input\_cidr\_block) | The IPv4 CIDR block for the VPC. | `string` | n/a | yes | | [network\_acl\_disabled](#input\_network\_acl\_disabled) | If disabled, default Network ACLs allow all inbound and outbound protocols. | `bool` | `true` | no | | [prefix](#input\_prefix) | Value used to prefix resources name. | `string` | `"noname"` | no | -| [subnet\_groups](#input\_subnet\_groups) | One subnet in each availablity zone is created per subnet group. |
map(object({
cidr_block = string
public = optional(bool, false)
}))
| n/a | yes | +| [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, "")
}))
| n/a | yes | ## Outputs @@ -88,4 +91,4 @@ No modules. | [aws\_availability\_zones](#output\_aws\_availability\_zones) | List of available AZ names. | | [subnet\_ids](#output\_subnet\_ids) | Lisr of public subnet IDs. | | [vpc](#output\_vpc) | value | - + diff --git a/examples/minimum/README.md b/examples/minimum/README.md index 1cb3fb6..9b702cc 100644 --- a/examples/minimum/README.md +++ b/examples/minimum/README.md @@ -4,10 +4,10 @@ Configuration in this directory creates a VPC with 2 groups of subnets named __p 1 subnet per availability zone in the region is created on each group. Cidr block for each subnet in the group is calculated automaticaly based on the group __cidr_block__ and the numbre of availability zone used. -The group named __public__ is declared as public, that means a route table with a default route to internet -gateway is associated to each subnet of this group. +The group named __public__ is declared as public with its default route through Internet Gatewat. -Other subnets are routed localy inside the VPC. +The group named __private__ has a default route to a NAT Gateway in __public__ subnets group. That means a NAT +Gateway will be created in each __public__ subnets as target default route in route tables of __private__ subnets. __network_acl_disabled__ is set to __true__ becsause in this exemple we don't want to manage Network ACLs. That way, the VPC default Network ACL is configure with ingress and egress rules whitch are allowing all trafic. @@ -21,7 +21,7 @@ terraform init terraform plan terraform apply ``` - + ## Requirements | Name | Version | @@ -52,4 +52,4 @@ No inputs. | Name | Description | |------|-------------| | [module](#output\_module) | VPC module outputs | - + diff --git a/examples/minimum/main.tf b/examples/minimum/main.tf index e9291e3..48cae2a 100644 --- a/examples/minimum/main.tf +++ b/examples/minimum/main.tf @@ -24,11 +24,12 @@ module "vpc" { cidr_block = local.cidr_block subnet_groups = { public = { - public = true - cidr_block = cidrsubnet(local.cidr_block, 1, 0) + default_route = "internet_gateway" + cidr_block = cidrsubnet(local.cidr_block, 1, 0) } private = { - cidr_block = cidrsubnet(local.cidr_block, 1, 1) + default_route = "nat_gateway.public" + cidr_block = cidrsubnet(local.cidr_block, 1, 1) } } } diff --git a/internet-gateway.tf b/internet-gateway.tf new file mode 100644 index 0000000..7ac9454 --- /dev/null +++ b/internet-gateway.tf @@ -0,0 +1,10 @@ +# internet-gateway.tf + +resource "aws_internet_gateway" "self" { + count = local.internet_gateway_enabled ? 1 : 0 + vpc_id = aws_vpc.self.id + + tags = { + Name = join("-", compact([var.prefix, "igw"])) + } +} diff --git a/main.tf b/main.tf index a122cb2..f6f5a88 100644 --- a/main.tf +++ b/main.tf @@ -5,12 +5,8 @@ data "aws_availability_zones" "current" { } locals { - # global availability_zones = var.availability_zones == null ? data.aws_availability_zones.current.names : var.availability_zones - # vpc - vpc_name = join("-", compact([var.prefix, "vpc"])) - # subnets subnet_newbits = ceil(log(length(local.availability_zones), 2)) subnets_attributes = merge(flatten([ @@ -19,7 +15,7 @@ locals { join("-", [k, z]) = { availability_zone = z subnet_group = k - public = v.public + default_route = v.default_route cidr_block = cidrsubnet(v.cidr_block, local.subnet_newbits, i) } } @@ -27,5 +23,13 @@ locals { ])...) # internet gateway - public_enabled = contains([for g in var.subnet_groups : g.public], true) + internet_gateway_enabled = contains([for g in local.subnets_attributes : g.default_route == "internet_gateway"], true) + + # NAT gateways + nat_subnet_groups = distinct([for k, v in var.subnet_groups : trimprefix(v.default_route, "nat_gateway.") if startswith(v.default_route, "nat_gateway")]) + nat_gateways = toset(flatten([ + for k in local.nat_subnet_groups : [ + for i, z in local.availability_zones : join("-", [k, z]) + ] + ])) } diff --git a/nat_gateway.tf b/nat_gateway.tf new file mode 100644 index 0000000..ba330d2 --- /dev/null +++ b/nat_gateway.tf @@ -0,0 +1,9 @@ +# nat_gateway.tf + +resource "aws_nat_gateway" "self" { + for_each = local.nat_gateways + subnet_id = aws_subnet.self[each.value].id + allocation_id = aws_eip.self[each.value].id + + depends_on = [aws_internet_gateway.self] +} diff --git a/route_table.tf b/route_table.tf new file mode 100644 index 0000000..eb2dd08 --- /dev/null +++ b/route_table.tf @@ -0,0 +1,32 @@ +# route_table.tf + +resource "aws_route_table" "self" { + for_each = local.subnets_attributes + vpc_id = aws_vpc.self.id + + dynamic "route" { + for_each = each.value.default_route == "internet_gateway" ? toset([0]) : [] + content { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.self[route.value].id + } + } + + dynamic "route" { + for_each = startswith(each.value.default_route, "nat_gateway.") ? toset([trimprefix(each.value.default_route, "nat_gateway.")]) : [] + content { + cidr_block = "0.0.0.0/0" + nat_gateway_id = aws_nat_gateway.self[join("-", [route.value, each.value.availability_zone])].id + } + } + + tags = { + Name = join("-", compact([var.prefix, each.key])) + } +} + +resource "aws_route_table_association" "public" { + for_each = local.subnets_attributes + route_table_id = aws_route_table.self[each.key].id + subnet_id = aws_subnet.self[each.key].id +} diff --git a/subnets.tf b/subnets.tf index f93a504..21f5818 100644 --- a/subnets.tf +++ b/subnets.tf @@ -11,31 +11,7 @@ resource "aws_subnet" "self" { } } -resource "aws_internet_gateway" "self" { - count = local.public_enabled ? 1 : 0 - vpc_id = aws_vpc.self.id - - tags = { - Name = join("-", compact([var.prefix, "gw"])) - } -} - -resource "aws_route_table" "public" { - count = local.public_enabled ? 1 : 0 - vpc_id = aws_vpc.self.id - - route { - cidr_block = "0.0.0.0/0" - gateway_id = aws_internet_gateway.self[0].id - } - - tags = { - Name = join("-", compact([var.prefix, "public"])) - } -} - -resource "aws_route_table_association" "public" { - for_each = toset([for k, v in local.subnets_attributes : k if v.public]) - route_table_id = aws_route_table.public[0].id - subnet_id = aws_subnet.self[each.value].id +resource "aws_eip" "self" { + for_each = local.nat_gateways + domain = "vpc" } diff --git a/variables.tf b/variables.tf index f1274a4..5c4660d 100644 --- a/variables.tf +++ b/variables.tf @@ -26,7 +26,7 @@ variable "network_acl_disabled" { variable "subnet_groups" { description = "One subnet in each availablity zone is created per subnet group." type = map(object({ - cidr_block = string - public = optional(bool, false) + cidr_block = string + default_route = optional(string, "") })) } diff --git a/versions.tf b/versions.tf index 8222ae5..8ee3189 100644 --- a/versions.tf +++ b/versions.tf @@ -1,7 +1,7 @@ # versions.tf terraform { - required_version = ">=1.0.0" + required_version = ">=1.3.0" required_providers { aws = { diff --git a/vpc.tf b/vpc.tf index bc888a7..fd1d155 100644 --- a/vpc.tf +++ b/vpc.tf @@ -6,7 +6,7 @@ resource "aws_vpc" "self" { enable_dns_support = true tags = { - Name = local.vpc_name + Name = join("-", compact([var.prefix, "vpc"])) } }