feat: defaults
This commit is contained in:
1 parent
e106aeec8b
commit
5d9937808a
10 files changed
+83
-69
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/antonbabenko/pre-commit-terraform
|
- repo: https://github.com/antonbabenko/pre-commit-terraform
|
||||||
rev: v1.88.3
|
rev: v1.89.1
|
||||||
hooks:
|
hooks:
|
||||||
- id: terraform_fmt
|
- id: terraform_fmt
|
||||||
- id: terraform_validate
|
- id: terraform_validate
|
||||||
@@ -11,6 +11,7 @@ repos:
|
|||||||
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
|
||||||
|
- --hook-config=--use-standard-markers=true
|
||||||
- id: terraform_checkov
|
- id: terraform_checkov
|
||||||
args:
|
args:
|
||||||
- '--args=--quiet'
|
- '--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_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
|
- '--args=--skip-check CKV_AWS_352' # Ensure NACL ingress does not allow all Ports
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v4.5.0
|
rev: v4.6.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: check-merge-conflict
|
- id: check-merge-conflict
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
|
|||||||
@@ -1,23 +1,40 @@
|
|||||||
# Terraform module - VPC Factory
|
# 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
|
## Usage
|
||||||
|
|
||||||
```
|
```
|
||||||
|
# Defaults
|
||||||
module "network" {
|
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"
|
cidr_block = "10.0.0.0/16"
|
||||||
availability_zones = ["eu-west-1a", "eu-west-1b"]
|
availability_zones = ["eu-west-1a", "eu-west-1b"]
|
||||||
network_acl_disabled = true
|
network_acl_disabled = true
|
||||||
|
|
||||||
subnet_groups = {
|
subnet_groups = {
|
||||||
dmz = {
|
dmz = {
|
||||||
default_route = "internet_gateway"
|
default_route = "igw"
|
||||||
cidr_block = "10.0.0.0/26"
|
cidr_block = "10.0.0.0/26"
|
||||||
}
|
}
|
||||||
frontend = {
|
frontend = {
|
||||||
default_route = "nat_gateway.pub"
|
default_route = "ngw/pub"
|
||||||
cidr_block = "10.0.16.0/20"
|
cidr_block = "10.0.16.0/20"
|
||||||
}
|
}
|
||||||
backend = {
|
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.
|
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
|
All subnet_groups must be given a __cidr_block__ within __cidr_bloc__ (default 10.0.0.0/16) provided
|
||||||
into several sub-blocks to have a subnet per availability zone on each groups. Group's __cidr_block_
|
to the module. Each block which wil be cut into several sub-blocks to have a subnet per availability
|
||||||
must not overlap each others and must be included into VPC's __cidr_block__.
|
zone for each groups. Group's __cidr_block__ must not overlap each others .
|
||||||
|
|
||||||
<!-- BEGIN_TF_DOCS -->
|
<!-- BEGIN_TF_DOCS -->
|
||||||
## Requirements
|
## Requirements
|
||||||
@@ -79,16 +96,16 @@ No modules.
|
|||||||
| Name | Description | Type | Default | Required |
|
| Name | Description | Type | Default | Required |
|
||||||
|------|-------------|------|---------|:--------:|
|
|------|-------------|------|---------|:--------:|
|
||||||
| <a name="input_availability_zones"></a> [availability\_zones](#input\_availability\_zones) | Target availability zones. | `list(string)` | `null` | no |
|
| <a name="input_availability_zones"></a> [availability\_zones](#input\_availability\_zones) | Target availability zones. | `list(string)` | `null` | 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_cidr_block"></a> [cidr\_block](#input\_cidr\_block) | The IPv4 CIDR block for the VPC. | `string` | `null` | no |
|
||||||
| <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_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_prefix_name"></a> [prefix\_name](#input\_prefix\_name) | Value used to prefix resources name. | `string` | `null` | 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> default_route = optional(string, "")<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> | `null` | no |
|
||||||
|
|
||||||
## 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_subnet_ids"></a> [subnet\_ids](#output\_subnet\_ids) | Map of public subnet IDs. |
|
||||||
| <a name="output_vpc"></a> [vpc](#output\_vpc) | value |
|
| <a name="output_vpc"></a> [vpc](#output\_vpc) | value |
|
||||||
<!-- END_TF_DOCS -->
|
<!-- END_TF_DOCS -->
|
||||||
@@ -1,35 +1,14 @@
|
|||||||
locals {
|
# main.tf
|
||||||
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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
provider "aws" {
|
provider "aws" {
|
||||||
region = local.region
|
|
||||||
|
|
||||||
default_tags {
|
default_tags {
|
||||||
tags = local.tags
|
tags = {
|
||||||
|
Project = "ex-${basename(path.cwd)}"
|
||||||
|
Iac = "terraform"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module "vpc" {
|
module "vpc" {
|
||||||
source = "../.."
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+2
-2
@@ -1,10 +1,10 @@
|
|||||||
# internet-gateway.tf
|
# internet-gateway.tf
|
||||||
|
|
||||||
resource "aws_internet_gateway" "self" {
|
resource "aws_internet_gateway" "self" {
|
||||||
count = local.internet_gateway_enabled ? 1 : 0
|
count = local.igw_enabled ? 1 : 0
|
||||||
vpc_id = aws_vpc.self.id
|
vpc_id = aws_vpc.self.id
|
||||||
|
|
||||||
tags = {
|
tags = {
|
||||||
Name = join("-", compact([var.prefix, "igw"]))
|
Name = join("-", compact([local.prefix_name, "igw"]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,13 +5,28 @@ data "aws_availability_zones" "current" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
locals {
|
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
|
# subnets
|
||||||
subnet_newbits = ceil(log(length(local.availability_zones), 2))
|
subnet_newbits = ceil(log(length(local.azs), 2))
|
||||||
|
|
||||||
subnets_attributes = merge(flatten([
|
subnets_attributes = merge(flatten([
|
||||||
for k, v in var.subnet_groups : [
|
for k, v in local.nets : [
|
||||||
for i, z in local.availability_zones : {
|
for i, z in local.azs : {
|
||||||
join("-", [k, z]) = {
|
join("-", [k, z]) = {
|
||||||
availability_zone = z
|
availability_zone = z
|
||||||
subnet_group = k
|
subnet_group = k
|
||||||
@@ -23,13 +38,13 @@ locals {
|
|||||||
])...)
|
])...)
|
||||||
|
|
||||||
# internet gateway
|
# 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 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([
|
nat_gateways = toset(flatten([
|
||||||
for k in local.nat_subnet_groups : [
|
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])
|
||||||
]
|
]
|
||||||
]))
|
]))
|
||||||
}
|
}
|
||||||
+1
-1
@@ -10,5 +10,5 @@ output "vpc" {
|
|||||||
|
|
||||||
output "subnet_ids" {
|
output "subnet_ids" {
|
||||||
description = "Map of public 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] }
|
||||||
}
|
}
|
||||||
+3
-3
@@ -5,7 +5,7 @@ resource "aws_route_table" "self" {
|
|||||||
vpc_id = aws_vpc.self.id
|
vpc_id = aws_vpc.self.id
|
||||||
|
|
||||||
dynamic "route" {
|
dynamic "route" {
|
||||||
for_each = each.value.default_route == "internet_gateway" ? toset([0]) : []
|
for_each = each.value.default_route == "igw" ? toset([0]) : []
|
||||||
content {
|
content {
|
||||||
cidr_block = "0.0.0.0/0"
|
cidr_block = "0.0.0.0/0"
|
||||||
gateway_id = aws_internet_gateway.self[route.value].id
|
gateway_id = aws_internet_gateway.self[route.value].id
|
||||||
@@ -13,7 +13,7 @@ resource "aws_route_table" "self" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dynamic "route" {
|
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 {
|
content {
|
||||||
cidr_block = "0.0.0.0/0"
|
cidr_block = "0.0.0.0/0"
|
||||||
nat_gateway_id = aws_nat_gateway.self[join("-", [route.value, each.value.availability_zone])].id
|
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 = {
|
tags = {
|
||||||
Name = join("-", compact([var.prefix, each.key]))
|
Name = join("-", compact([local.prefix_name, each.key]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@ resource "aws_subnet" "self" {
|
|||||||
availability_zone = each.value.availability_zone
|
availability_zone = each.value.availability_zone
|
||||||
|
|
||||||
tags = {
|
tags = {
|
||||||
Name = join("-", compact([var.prefix, each.key]))
|
Name = join("-", compact([local.prefix_name, each.key]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
-8
@@ -1,14 +1,15 @@
|
|||||||
# variables.tf
|
# variables.tf
|
||||||
|
|
||||||
variable "prefix" {
|
variable "prefix_name" {
|
||||||
description = "Value used to prefix resources name."
|
description = "Value used to prefix resources name."
|
||||||
type = string
|
type = string
|
||||||
default = "noname"
|
default = null
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = null
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "availability_zones" {
|
variable "availability_zones" {
|
||||||
@@ -17,16 +18,17 @@ variable "availability_zones" {
|
|||||||
default = null
|
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" {
|
variable "subnet_groups" {
|
||||||
description = "One subnet in each availablity zone is created per subnet group."
|
description = "One subnet in each availablity zone is created per subnet group."
|
||||||
type = map(object({
|
type = map(object({
|
||||||
cidr_block = string
|
cidr_block = string
|
||||||
default_route = optional(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
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
# vpc.tf
|
# vpc.tf
|
||||||
|
|
||||||
resource "aws_vpc" "self" {
|
resource "aws_vpc" "self" {
|
||||||
cidr_block = var.cidr_block
|
cidr_block = local.cidr_block
|
||||||
enable_dns_hostnames = true
|
enable_dns_hostnames = true
|
||||||
enable_dns_support = true
|
enable_dns_support = true
|
||||||
|
|
||||||
tags = {
|
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 = []
|
route = []
|
||||||
|
|
||||||
tags = {
|
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
|
default_network_acl_id = aws_vpc.self.default_network_acl_id
|
||||||
|
|
||||||
tags = {
|
tags = {
|
||||||
Name = join("-", compact([var.prefix, "nacl"]))
|
Name = join("-", compact([local.prefix_name, "nacl"]))
|
||||||
}
|
}
|
||||||
|
|
||||||
lifecycle {
|
lifecycle {
|
||||||
@@ -55,7 +55,7 @@ resource "aws_default_security_group" "self" {
|
|||||||
vpc_id = aws_vpc.self.id
|
vpc_id = aws_vpc.self.id
|
||||||
|
|
||||||
tags = {
|
tags = {
|
||||||
Name = join("-", compact([var.prefix, "default", "sg"]))
|
Name = join("-", compact([local.prefix_name, "default", "sg"]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user