diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1d7a84e..f09f534 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/antonbabenko/pre-commit-terraform - rev: v1.88.3 + rev: v1.89.1 hooks: - id: terraform_fmt - id: terraform_validate @@ -11,6 +11,7 @@ repos: args: - --hook-config=--add-to-existing-file=true - --hook-config=--create-file-if-not-exist=true + - --hook-config=--use-standard-markers=true - id: terraform_checkov args: - '--args=--quiet' @@ -20,7 +21,7 @@ repos: - '--args=--skip-check CKV_AWS_232' # Ensure no NACL allow ingress from 0.0.0.0:0 to port 22 - '--args=--skip-check CKV_AWS_352' # Ensure NACL ingress does not allow all Ports - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v4.6.0 hooks: - id: check-merge-conflict - id: trailing-whitespace diff --git a/README.md b/README.md index c310bc1..91c543d 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,40 @@ # Terraform module - VPC Factory +Terraform VPC factory module creates a VPC with as many subnet tiers as described in subnet_groups attribute. +For each subnet tier, a subnet is created in each selectec availability zones in availability_zones attribute. +If these attributes are not specified, 2 tiers are creates : public and private in the 2 first availability +zones of the current AWS region.An Internet Gateway is configured as default route for public tier's subnets +and a NAT Gateway in each public subnets is created and configured as default route for private tier's subnets. + +The default CIDR block for VPC is 10.0.0.0/16, but it can be customized with cidr_block. + +By default, Network ACLs are configured to allow all ingress/egress. + ## Usage ``` +# Defaults module "network" { - source = "git:https://gitea.fillon.info/terraform-modules/terraform-aws-vpc-factory?ref=main" + source = "git:https://github.com/thunderbal/terraform-aws-vpc-factory?ref=main" +} - prefix = "example" + +# Custom example +module "network" { + source = "git:https://github.com/thunderbal/terraform-aws-vpc-factory?ref=main" + + prefix_name = "example" cidr_block = "10.0.0.0/16" availability_zones = ["eu-west-1a", "eu-west-1b"] network_acl_disabled = true subnet_groups = { dmz = { - default_route = "internet_gateway" + default_route = "igw" cidr_block = "10.0.0.0/26" } frontend = { - default_route = "nat_gateway.pub" + default_route = "ngw/pub" cidr_block = "10.0.16.0/20" } backend = { @@ -29,13 +46,13 @@ module "network" { By default, Network ACLs allow alltrafic. Set __network_acl_disabled__ to __false__ to block all trafic by default. -__prefix__ is used in tag Name to identify resources created by this module. +__prefix_name__ is used in tag Name to identify resources created by this module. -If __availability_zones__ is not specified, module use all availability zones in the target region. +If __availability_zones__ is not specified, module use 2 availability zones in the target region. -__cidr_block__ is mandatory for VPC, all subnet_groups must be given a __cidr_block__ which wil be cut -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__. +All subnet_groups must be given a __cidr_block__ within __cidr_bloc__ (default 10.0.0.0/16) provided +to the module. Each block which wil be cut into several sub-blocks to have a subnet per availability +zone for each groups. Group's __cidr_block__ must not overlap each others . ## Requirements @@ -79,16 +96,16 @@ 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` | n/a | yes | +| [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](#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
default_route = optional(string, "")
}))
| n/a | yes | +| [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 | Name | Description | |------|-------------| | [aws\_availability\_zones](#output\_aws\_availability\_zones) | List of available AZ names. | -| [subnet\_ids](#output\_subnet\_ids) | Lisr of public subnet IDs. | +| [subnet\_ids](#output\_subnet\_ids) | Map of public subnet IDs. | | [vpc](#output\_vpc) | value | diff --git a/examples/minimum/main.tf b/examples/minimum/main.tf index 48cae2a..e6d3b26 100644 --- a/examples/minimum/main.tf +++ b/examples/minimum/main.tf @@ -1,35 +1,14 @@ -locals { - name = "ex-${basename(path.cwd)}" - region = "eu-west-1" - cidr_block = "10.0.0.0/16" - - tags = { - Example = local.name - Repo = "terraform-aws-vpc-factory" - Org = "terraform-modules" - } -} +# main.tf provider "aws" { - region = local.region - default_tags { - tags = local.tags + tags = { + Project = "ex-${basename(path.cwd)}" + Iac = "terraform" + } } } module "vpc" { - source = "../.." - prefix = local.name - cidr_block = local.cidr_block - subnet_groups = { - public = { - default_route = "internet_gateway" - cidr_block = cidrsubnet(local.cidr_block, 1, 0) - } - private = { - default_route = "nat_gateway.public" - cidr_block = cidrsubnet(local.cidr_block, 1, 1) - } - } + source = "../.." } diff --git a/internet-gateway.tf b/internet-gateway.tf index 7ac9454..5cead55 100644 --- a/internet-gateway.tf +++ b/internet-gateway.tf @@ -1,10 +1,10 @@ # internet-gateway.tf resource "aws_internet_gateway" "self" { - count = local.internet_gateway_enabled ? 1 : 0 + count = local.igw_enabled ? 1 : 0 vpc_id = aws_vpc.self.id tags = { - Name = join("-", compact([var.prefix, "igw"])) + Name = join("-", compact([local.prefix_name, "igw"])) } } diff --git a/main.tf b/main.tf index f6f5a88..ee8f51b 100644 --- a/main.tf +++ b/main.tf @@ -5,13 +5,28 @@ data "aws_availability_zones" "current" { } locals { - availability_zones = var.availability_zones == null ? data.aws_availability_zones.current.names : var.availability_zones + # 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" + } + } # subnets - subnet_newbits = ceil(log(length(local.availability_zones), 2)) + subnet_newbits = ceil(log(length(local.azs), 2)) + subnets_attributes = merge(flatten([ - for k, v in var.subnet_groups : [ - for i, z in local.availability_zones : { + for k, v in local.nets : [ + for i, z in local.azs : { join("-", [k, z]) = { availability_zone = z subnet_group = k @@ -23,13 +38,13 @@ locals { ])...) # internet gateway - internet_gateway_enabled = contains([for g in local.subnets_attributes : g.default_route == "internet_gateway"], true) + igw_enabled = contains([for g in local.nets : g.default_route == "igw"], 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_subnet_groups = distinct([for k, v in local.nets : basename(v.default_route) if startswith(v.default_route, "ngw/")]) nat_gateways = toset(flatten([ for k in local.nat_subnet_groups : [ - for i, z in local.availability_zones : join("-", [k, z]) + for i, z in local.azs : join("-", [k, z]) ] ])) } diff --git a/outputs.tf b/outputs.tf index 7ceaa61..3b6ce24 100644 --- a/outputs.tf +++ b/outputs.tf @@ -10,5 +10,5 @@ output "vpc" { output "subnet_ids" { description = "Map of public subnet IDs." - value = { for g in keys(var.subnet_groups) : g => [for k, v in local.subnets_attributes : aws_subnet.self[k].id if g == v.subnet_group] } + value = { for g in keys(local.nets) : g => [for k, v in local.subnets_attributes : aws_subnet.self[k].id if g == v.subnet_group] } } diff --git a/route_table.tf b/route_table.tf index eb2dd08..8260617 100644 --- a/route_table.tf +++ b/route_table.tf @@ -5,7 +5,7 @@ resource "aws_route_table" "self" { vpc_id = aws_vpc.self.id dynamic "route" { - for_each = each.value.default_route == "internet_gateway" ? toset([0]) : [] + for_each = each.value.default_route == "igw" ? toset([0]) : [] content { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.self[route.value].id @@ -13,7 +13,7 @@ resource "aws_route_table" "self" { } dynamic "route" { - for_each = startswith(each.value.default_route, "nat_gateway.") ? toset([trimprefix(each.value.default_route, "nat_gateway.")]) : [] + for_each = startswith(each.value.default_route, "ngw/") ? toset([basename(each.value.default_route)]) : [] content { cidr_block = "0.0.0.0/0" nat_gateway_id = aws_nat_gateway.self[join("-", [route.value, each.value.availability_zone])].id @@ -21,7 +21,7 @@ resource "aws_route_table" "self" { } tags = { - Name = join("-", compact([var.prefix, each.key])) + Name = join("-", compact([local.prefix_name, each.key])) } } diff --git a/subnets.tf b/subnets.tf index 21f5818..a3e26ea 100644 --- a/subnets.tf +++ b/subnets.tf @@ -7,7 +7,7 @@ resource "aws_subnet" "self" { availability_zone = each.value.availability_zone tags = { - Name = join("-", compact([var.prefix, each.key])) + Name = join("-", compact([local.prefix_name, each.key])) } } diff --git a/variables.tf b/variables.tf index 5c4660d..3552950 100644 --- a/variables.tf +++ b/variables.tf @@ -1,14 +1,15 @@ # variables.tf -variable "prefix" { +variable "prefix_name" { description = "Value used to prefix resources name." type = string - default = "noname" + default = null } variable "cidr_block" { description = "The IPv4 CIDR block for the VPC." type = string + default = null } variable "availability_zones" { @@ -17,16 +18,17 @@ variable "availability_zones" { default = null } -variable "network_acl_disabled" { - description = "If disabled, default Network ACLs allow all inbound and outbound protocols." - type = bool - default = true -} - 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 + default = true } diff --git a/vpc.tf b/vpc.tf index fd1d155..b6b9db8 100644 --- a/vpc.tf +++ b/vpc.tf @@ -1,12 +1,12 @@ # vpc.tf resource "aws_vpc" "self" { - cidr_block = var.cidr_block + cidr_block = local.cidr_block enable_dns_hostnames = true enable_dns_support = true tags = { - Name = join("-", compact([var.prefix, "vpc"])) + Name = join("-", compact([local.prefix_name, "vpc"])) } } @@ -15,7 +15,7 @@ resource "aws_default_route_table" "self" { route = [] tags = { - Name = join("-", compact([var.prefix, "default"])) + Name = join("-", compact([local.prefix_name, "default"])) } } @@ -23,7 +23,7 @@ resource "aws_default_network_acl" "self" { default_network_acl_id = aws_vpc.self.default_network_acl_id tags = { - Name = join("-", compact([var.prefix, "nacl"])) + Name = join("-", compact([local.prefix_name, "nacl"])) } lifecycle { @@ -55,7 +55,7 @@ resource "aws_default_security_group" "self" { vpc_id = aws_vpc.self.id tags = { - Name = join("-", compact([var.prefix, "default", "sg"])) + Name = join("-", compact([local.prefix_name, "default", "sg"])) } }