feature: subnets

This commit is contained in:
David FILLON committed 2024-03-24 17:03:43 +01:00
1 parent af8e1cc4a6
commit b7b1653cc6
10 files changed
+253 -89

No files matched your search

+11 -15
View File
@@ -1,28 +1,24 @@
repos: repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform - repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.88.2 rev: v1.88.3
hooks: hooks:
- id: terraform_fmt - id: terraform_fmt
- id: terraform_validate - id: terraform_validate
- id: terraform_tflint
args:
- '--args=--recursive'
- id: terraform_docs - id: terraform_docs
args: args:
- --hook-config=--add-to-existing-file=true - --hook-config=--add-to-existing-file=true
- --hook-config=--create-file-if-not-exist=true - --hook-config=--create-file-if-not-exist=true
- id: terraform_tflint - id: terraform_checkov
args: args:
- '--args=--only=terraform_deprecated_interpolation' - '--args=--quiet'
- '--args=--only=terraform_deprecated_index' - '--args=--skip-check CKV_AWS_229' # Ensure no NACL allow ingress from 0.0.0.0:0 to port 21
- '--args=--only=terraform_unused_declarations' - '--args=--skip-check CKV_AWS_230' # Ensure no NACL allow ingress from 0.0.0.0:0 to port 20
- '--args=--only=terraform_comment_syntax' - '--args=--skip-check CKV_AWS_231' # Ensure no NACL allow ingress from 0.0.0.0:0 to port 3389
- '--args=--only=terraform_documented_outputs' - '--args=--skip-check CKV_AWS_232' # Ensure no NACL allow ingress from 0.0.0.0:0 to port 22
- '--args=--only=terraform_documented_variables' - '--args=--skip-check CKV_AWS_352' # Ensure NACL ingress does not allow all Ports
- '--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'
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0 rev: v4.5.0
hooks: hooks:
+41 -5
View File
@@ -6,9 +6,36 @@
module "network" { module "network" {
source = "git:https://gitea.fillon.info/terraform-modules/terraform-aws-vpc-factory?ref=main" 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__.
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements ## 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_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_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_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_default_vpc_dhcp_options.self](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_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_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 | | [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 | | Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:| |------|-------------|------|---------|:--------:|
| <a name="input_cidr_block"></a> [cidr\_block](#input\_cidr\_block) | The IPv4 CIDR block for the VPC. | `string` | `"10.0.0.0/16"` | no | | <a name="input_availability_zones"></a> [availability\_zones](#input\_availability\_zones) | Target availability zones. | `list(string)` | `null` | no |
| <a name="input_public_subnets"></a> [public\_subnets](#input\_public\_subnets) | Map of public subnets | <pre>map(object({<br> cidr_block = string<br> }))</pre> | `{}` | no | | <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 |
## Outputs ## Outputs
| Name | Description | | Name | Description |
|------|-------------| |------|-------------|
| <a name="output_aws_availability_zones"></a> [aws\_availability\_zones](#output\_aws\_availability\_zones) | List of available AZ names. | | <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 | | <a name="output_vpc"></a> [vpc](#output\_vpc) | value |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
+11 -1
View File
@@ -1,6 +1,16 @@
# Minimum VPC Factory # 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 ## Usage
+23 -11
View File
@@ -1,14 +1,7 @@
provider "aws" {
region = local.region
default_tags {
tags = local.tags
}
}
locals { locals {
name = "ex-${basename(path.cwd)}" name = "ex-${basename(path.cwd)}"
region = "eu-west-1" region = "eu-west-1"
cidr_block = "10.0.0.0/16"
tags = { tags = {
Example = local.name Example = local.name
@@ -17,6 +10,25 @@ locals {
} }
} }
provider "aws" {
region = local.region
default_tags {
tags = local.tags
}
}
module "vpc" { 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)
}
}
} }
+22 -35
View File
@@ -1,44 +1,31 @@
terraform { # main.tf
required_version = ">=1.0.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">=4.0"
}
}
}
data "aws_availability_zones" "current" { data "aws_availability_zones" "current" {
state = "available" state = "available"
} }
resource "aws_vpc" "self" { locals {
cidr_block = var.cidr_block # global
} availability_zones = var.availability_zones == null ? data.aws_availability_zones.current.names : var.availability_zones
resource "aws_default_route_table" "self" { # vpc
default_route_table_id = aws_vpc.self.default_route_table_id 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" { # internet gateway
default_network_acl_id = aws_vpc.self.default_network_acl_id public_enabled = contains([for g in var.subnet_groups : g.public], true)
}
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
} }
+5
View File
@@ -7,3 +7,8 @@ output "vpc" {
description = "value" description = "value"
value = aws_vpc.self 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] }
}
+41
View File
@@ -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
}
+21 -22
View File
@@ -1,33 +1,32 @@
# variables.tf
variable "prefix" {
description = "Value used to prefix resources name."
type = string
default = "noname"
}
variable "cidr_block" { variable "cidr_block" {
description = "The IPv4 CIDR block for the VPC." description = "The IPv4 CIDR block for the VPC."
type = string type = string
default = "10.0.0.0/16"
} }
# variable "public_cidr_block" { variable "availability_zones" {
# description = "Alternate CIDR block " description = "Target availability zones."
# type = string type = list(string)
# default = "" default = null
# } }
# variable "internal_cidr_block" { variable "network_acl_disabled" {
# description = "value" description = "If disabled, default Network ACLs allow all inbound and outbound protocols."
# type = string type = bool
# default = "" default = true
# } }
variable "public_subnets" { variable "subnet_groups" {
description = "Map of public subnets" description = "One subnet in each availablity zone is created per subnet group."
type = map(object({ type = map(object({
cidr_block = string cidr_block = string
public = optional(bool, false)
})) }))
default = {}
} }
# variable "private_subnets" {
# }
# variable "internal_subnets" {
# }
+12
View File
@@ -0,0 +1,12 @@
# versions.tf
terraform {
required_version = ">=1.0.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">=4.0"
}
}
}
+66
View File
@@ -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]
}
}