diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a5d0509..1d7a84e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,28 +1,24 @@ repos: - repo: https://github.com/antonbabenko/pre-commit-terraform - rev: v1.88.2 + rev: v1.88.3 hooks: - id: terraform_fmt - id: terraform_validate + - id: terraform_tflint + args: + - '--args=--recursive' - id: terraform_docs args: - --hook-config=--add-to-existing-file=true - --hook-config=--create-file-if-not-exist=true - - id: terraform_tflint + - id: terraform_checkov args: - - '--args=--only=terraform_deprecated_interpolation' - - '--args=--only=terraform_deprecated_index' - - '--args=--only=terraform_unused_declarations' - - '--args=--only=terraform_comment_syntax' - - '--args=--only=terraform_documented_outputs' - - '--args=--only=terraform_documented_variables' - - '--args=--only=terraform_typed_variables' - - '--args=--only=terraform_module_pinned_source' - - '--args=--only=terraform_naming_convention' - - '--args=--only=terraform_required_version' - - '--args=--only=terraform_required_providers' - - '--args=--only=terraform_standard_module_structure' - - '--args=--only=terraform_workspace_remote' + - '--args=--quiet' + - '--args=--skip-check CKV_AWS_229' # Ensure no NACL allow ingress from 0.0.0.0:0 to port 21 + - '--args=--skip-check CKV_AWS_230' # Ensure no NACL allow ingress from 0.0.0.0:0 to port 20 + - '--args=--skip-check CKV_AWS_231' # Ensure no NACL allow ingress from 0.0.0.0:0 to port 3389 + - '--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 hooks: diff --git a/README.md b/README.md index 1d2f9d7..ddae59c 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,36 @@ module "network" { source = "git:https://gitea.fillon.info/terraform-modules/terraform-aws-vpc-factory?ref=main" - cidr_block = "10.0.0.0/16" + prefix = "example" + cidr_block = "10.0.0.0/16" + availability_zones = ["eu-west-1a", "eu-west-1b"] + network_acl_disabled = true + + subnet_groups = { + dmz = { + public = true + cidr_block = "10.0.0.0/26" + } + frontend = { + cidr_block = "10.0.16.0/20" + } + backend = { + cidr_block = "10.0.32.0/20" + } + } } ``` + +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. + +If __availability_zones__ is not specified, module use all 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__. + ## Requirements @@ -34,8 +61,13 @@ No modules. | [aws_default_network_acl.self](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_network_acl) | resource | | [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.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_vpc_dhcp_options) | resource | -| [aws_subnet.public](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource | +| [aws_default_vpc_dhcp_options.self](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_vpc_dhcp_options) | resource | +| [aws_internet_gateway.self](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/internet_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_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 | | [aws_availability_zones.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source | @@ -43,13 +75,17 @@ No modules. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| [cidr\_block](#input\_cidr\_block) | The IPv4 CIDR block for the VPC. | `string` | `"10.0.0.0/16"` | no | -| [public\_subnets](#input\_public\_subnets) | Map of public subnets |
map(object({
cidr_block = string
}))
| `{}` | no | +| [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 | +| [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 | ## 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. | | [vpc](#output\_vpc) | value | diff --git a/examples/minimum/README.md b/examples/minimum/README.md index 8f89ee0..1cb3fb6 100644 --- a/examples/minimum/README.md +++ b/examples/minimum/README.md @@ -1,6 +1,16 @@ # Minimum VPC Factory -Configuration in this directory creates a VPC from module VPC Factory with default variables values. +Configuration in this directory creates a VPC with 2 groups of subnets named __public__ and __private__. +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. + +Other subnets are routed localy inside the VPC. + +__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. ## Usage diff --git a/examples/minimum/main.tf b/examples/minimum/main.tf index e3b029a..e9291e3 100644 --- a/examples/minimum/main.tf +++ b/examples/minimum/main.tf @@ -1,14 +1,7 @@ -provider "aws" { - region = local.region - - default_tags { - tags = local.tags - } -} - locals { - name = "ex-${basename(path.cwd)}" - region = "eu-west-1" + name = "ex-${basename(path.cwd)}" + region = "eu-west-1" + cidr_block = "10.0.0.0/16" tags = { Example = local.name @@ -17,6 +10,25 @@ locals { } } +provider "aws" { + region = local.region + + default_tags { + tags = local.tags + } +} + module "vpc" { - source = "../.." + source = "../.." + prefix = local.name + cidr_block = local.cidr_block + subnet_groups = { + public = { + public = true + cidr_block = cidrsubnet(local.cidr_block, 1, 0) + } + private = { + cidr_block = cidrsubnet(local.cidr_block, 1, 1) + } + } } diff --git a/main.tf b/main.tf index 52fa863..a122cb2 100644 --- a/main.tf +++ b/main.tf @@ -1,44 +1,31 @@ -terraform { - required_version = ">=1.0.0" - - required_providers { - aws = { - source = "hashicorp/aws" - version = ">=4.0" - } - } -} +# main.tf data "aws_availability_zones" "current" { state = "available" } -resource "aws_vpc" "self" { - cidr_block = var.cidr_block -} +locals { + # global + availability_zones = var.availability_zones == null ? data.aws_availability_zones.current.names : var.availability_zones -resource "aws_default_route_table" "self" { - default_route_table_id = aws_vpc.self.default_route_table_id + # vpc + vpc_name = join("-", compact([var.prefix, "vpc"])) - route = [] -} + # subnets + subnet_newbits = ceil(log(length(local.availability_zones), 2)) + subnets_attributes = merge(flatten([ + for k, v in var.subnet_groups : [ + for i, z in local.availability_zones : { + join("-", [k, z]) = { + availability_zone = z + subnet_group = k + public = v.public + cidr_block = cidrsubnet(v.cidr_block, local.subnet_newbits, i) + } + } + ] + ])...) -resource "aws_default_network_acl" "self" { - default_network_acl_id = aws_vpc.self.default_network_acl_id -} - -resource "aws_default_security_group" "self" { - vpc_id = aws_vpc.self.id -} - -resource "aws_default_vpc_dhcp_options" "default" { - lifecycle { - ignore_changes = [tags_all] - } -} - -resource "aws_subnet" "public" { - for_each = var.public_subnets - - vpc_id = aws_vpc.self.id + # internet gateway + public_enabled = contains([for g in var.subnet_groups : g.public], true) } diff --git a/outputs.tf b/outputs.tf index 4ac9a38..bde1109 100644 --- a/outputs.tf +++ b/outputs.tf @@ -7,3 +7,8 @@ output "vpc" { description = "value" value = aws_vpc.self } + +output "subnet_ids" { + description = "Lisr 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] } +} diff --git a/subnets.tf b/subnets.tf new file mode 100644 index 0000000..f93a504 --- /dev/null +++ b/subnets.tf @@ -0,0 +1,41 @@ +# subnets.tf + +resource "aws_subnet" "self" { + for_each = local.subnets_attributes + vpc_id = aws_vpc.self.id + cidr_block = each.value.cidr_block + availability_zone = each.value.availability_zone + + tags = { + Name = join("-", compact([var.prefix, each.key])) + } +} + +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 +} diff --git a/variables.tf b/variables.tf index 2e84209..f1274a4 100644 --- a/variables.tf +++ b/variables.tf @@ -1,33 +1,32 @@ +# variables.tf + +variable "prefix" { + description = "Value used to prefix resources name." + type = string + default = "noname" +} + variable "cidr_block" { description = "The IPv4 CIDR block for the VPC." type = string - default = "10.0.0.0/16" } -# variable "public_cidr_block" { -# description = "Alternate CIDR block " -# type = string -# default = "" -# } +variable "availability_zones" { + description = "Target availability zones." + type = list(string) + default = null +} -# variable "internal_cidr_block" { -# description = "value" -# type = string -# default = "" -# } +variable "network_acl_disabled" { + description = "If disabled, default Network ACLs allow all inbound and outbound protocols." + type = bool + default = true +} -variable "public_subnets" { - description = "Map of public subnets" +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) })) - default = {} } - -# variable "private_subnets" { - -# } - -# variable "internal_subnets" { - -# } diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..8222ae5 --- /dev/null +++ b/versions.tf @@ -0,0 +1,12 @@ +# versions.tf + +terraform { + required_version = ">=1.0.0" + + required_providers { + aws = { + source = "hashicorp/aws" + version = ">=4.0" + } + } +} diff --git a/vpc.tf b/vpc.tf new file mode 100644 index 0000000..bc888a7 --- /dev/null +++ b/vpc.tf @@ -0,0 +1,66 @@ +# vpc.tf + +resource "aws_vpc" "self" { + cidr_block = var.cidr_block + enable_dns_hostnames = true + enable_dns_support = true + + tags = { + Name = local.vpc_name + } +} + +resource "aws_default_route_table" "self" { + default_route_table_id = aws_vpc.self.default_route_table_id + route = [] + + tags = { + Name = join("-", compact([var.prefix, "default"])) + } +} + +resource "aws_default_network_acl" "self" { + default_network_acl_id = aws_vpc.self.default_network_acl_id + + tags = { + Name = join("-", compact([var.prefix, "nacl"])) + } + + lifecycle { + ignore_changes = [subnet_ids, egress, ingress] + } +} + +resource "aws_network_acl_rule" "all_ingress" { + count = var.network_acl_disabled == true ? 1 : 0 + network_acl_id = aws_default_network_acl.self.id + rule_number = 100 + rule_action = "allow" + protocol = -1 + egress = false + cidr_block = "0.0.0.0/0" +} + +resource "aws_network_acl_rule" "all_egress" { + count = var.network_acl_disabled ? 1 : 0 + network_acl_id = aws_default_network_acl.self.id + rule_number = 100 + rule_action = "allow" + protocol = -1 + egress = true + cidr_block = "0.0.0.0/0" +} + +resource "aws_default_security_group" "self" { + vpc_id = aws_vpc.self.id + + tags = { + Name = join("-", compact([var.prefix, "default", "sg"])) + } +} + +resource "aws_default_vpc_dhcp_options" "self" { + lifecycle { + ignore_changes = [tags_all] + } +}