feat: nat-gateways
This commit is contained in:
1 parent
b7b1653cc6
commit
e19d7fc053
11 files changed
+88
-53
No files matched your search
@@ -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__.
|
||||
|
||||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
|
||||
<!-- BEGIN_TF_DOCS -->
|
||||
## Requirements
|
||||
|
||||
| Name | Version |
|
||||
|------|---------|
|
||||
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >=1.0.0 |
|
||||
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >=1.3.0 |
|
||||
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >=4.0 |
|
||||
|
||||
## Providers
|
||||
|
||||
| Name | Version |
|
||||
|------|---------|
|
||||
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.33.0 |
|
||||
| <a name="provider_aws"></a> [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.
|
||||
| <a name="input_cidr_block"></a> [cidr\_block](#input\_cidr\_block) | The IPv4 CIDR block for the VPC. | `string` | n/a | yes |
|
||||
| <a name="input_network_acl_disabled"></a> [network\_acl\_disabled](#input\_network\_acl\_disabled) | If disabled, default Network ACLs allow all inbound and outbound protocols. | `bool` | `true` | no |
|
||||
| <a name="input_prefix"></a> [prefix](#input\_prefix) | Value used to prefix resources name. | `string` | `"noname"` | no |
|
||||
| <a name="input_subnet_groups"></a> [subnet\_groups](#input\_subnet\_groups) | One subnet in each availablity zone is created per subnet group. | <pre>map(object({<br> cidr_block = string<br> public = optional(bool, false)<br> }))</pre> | n/a | yes |
|
||||
| <a name="input_subnet_groups"></a> [subnet\_groups](#input\_subnet\_groups) | One subnet in each availablity zone is created per subnet group. | <pre>map(object({<br> cidr_block = string<br> default_route = optional(string, "")<br> }))</pre> | n/a | yes |
|
||||
|
||||
## Outputs
|
||||
|
||||
@@ -88,4 +91,4 @@ No modules.
|
||||
| <a name="output_aws_availability_zones"></a> [aws\_availability\_zones](#output\_aws\_availability\_zones) | List of available AZ names. |
|
||||
| <a name="output_subnet_ids"></a> [subnet\_ids](#output\_subnet\_ids) | Lisr of public subnet IDs. |
|
||||
| <a name="output_vpc"></a> [vpc](#output\_vpc) | value |
|
||||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
|
||||
<!-- END_TF_DOCS -->
|
||||
@@ -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
|
||||
```
|
||||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
|
||||
<!-- BEGIN_TF_DOCS -->
|
||||
## Requirements
|
||||
|
||||
| Name | Version |
|
||||
@@ -52,4 +52,4 @@ No inputs.
|
||||
| Name | Description |
|
||||
|------|-------------|
|
||||
| <a name="output_module"></a> [module](#output\_module) | VPC module outputs |
|
||||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
|
||||
<!-- END_TF_DOCS -->
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"]))
|
||||
}
|
||||
}
|
||||
@@ -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])
|
||||
]
|
||||
]))
|
||||
}
|
||||
@@ -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]
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
+3
-27
@@ -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"
|
||||
}
|
||||
+2
-2
@@ -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, "")
|
||||
}))
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
# versions.tf
|
||||
|
||||
terraform {
|
||||
required_version = ">=1.0.0"
|
||||
required_version = ">=1.3.0"
|
||||
|
||||
required_providers {
|
||||
aws = {
|
||||
|
||||
Reference in new issue
Block a user