Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d250c09b91 | ||
|
|
68205e8f20 | ||
|
|
a7ec6de10d | ||
|
|
63ab9d90a1 | ||
|
|
5d9937808a | ||
|
|
e106aeec8b | ||
|
|
81013b8c86 |
No files matched your search
@@ -1,9 +1,12 @@
|
||||
repos:
|
||||
- repo: https://github.com/antonbabenko/pre-commit-terraform
|
||||
rev: v1.88.3
|
||||
rev: v1.89.1
|
||||
hooks:
|
||||
- id: terraform_fmt
|
||||
- id: terraform_validate
|
||||
args:
|
||||
- --hook-config=--parallelism-limit=1
|
||||
- --hook-config=--retry-once-with-cleanup=true
|
||||
- id: terraform_tflint
|
||||
args:
|
||||
- '--args=--recursive'
|
||||
@@ -11,16 +14,18 @@ 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'
|
||||
- '--args=--compact'
|
||||
- '--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
|
||||
rev: v4.6.0
|
||||
hooks:
|
||||
- id: check-merge-conflict
|
||||
- id: trailing-whitespace
|
||||
|
||||
@@ -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 .
|
||||
|
||||
<!-- BEGIN_TF_DOCS -->
|
||||
## Requirements
|
||||
@@ -78,17 +95,14 @@ No modules.
|
||||
|
||||
| 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_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> default_route = optional(string, "")<br> }))</pre> | n/a | yes |
|
||||
| <a name="input_config_file"></a> [config\_file](#input\_config\_file) | Network configuration file. | `string` | `null` | no |
|
||||
| <a name="input_config_template"></a> [config\_template](#input\_config\_template) | value | `string` | `"default"` | no |
|
||||
|
||||
## Outputs
|
||||
|
||||
| Name | Description |
|
||||
|------|-------------|
|
||||
| <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 |
|
||||
<!-- END_TF_DOCS -->
|
||||
@@ -0,0 +1,9 @@
|
||||
cidr_block: 10.1.0.0/16
|
||||
# max_azs: 2
|
||||
nacl_disabled: true
|
||||
|
||||
subnet_groups:
|
||||
public:
|
||||
default_route: igw
|
||||
private:
|
||||
default_route: ngw/public
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
cidr_block: 10.2.0.0/16
|
||||
# max_azs: 2
|
||||
nacl_disabled: true
|
||||
|
||||
subnet_groups:
|
||||
public:
|
||||
default_route: igw
|
||||
tags:
|
||||
kubernetes.io/role/elb: 1
|
||||
private:
|
||||
default_route: ngw/public
|
||||
tags:
|
||||
kubernetes.io/role/internal-elb: 1
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -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"]))
|
||||
}
|
||||
}
|
||||
@@ -5,31 +5,47 @@ data "aws_availability_zones" "current" {
|
||||
}
|
||||
|
||||
locals {
|
||||
availability_zones = var.availability_zones == null ? data.aws_availability_zones.current.names : var.availability_zones
|
||||
config_file = var.config_file == null ? "${path.module}/config/${var.config_template}.yaml" : var.config_file
|
||||
config = yamldecode(file(local.config_file))
|
||||
|
||||
# Configure defaults when attributes are not defined
|
||||
prefix_name = try(local.config.prefix_name, basename(path.cwd))
|
||||
cidr_block = try(local.config.cidr_block, "10.0.0.0/16")
|
||||
azs_count = min(try(local.config.max_azs, 2), length(data.aws_availability_zones.current.names))
|
||||
azs = try(local.config.availability_zones, slice(data.aws_availability_zones.current.names, 0, local.azs_count))
|
||||
nets = try(local.config.subnet_groups, {})
|
||||
nacl_disabled = try(local.config.nacl_disabled, false)
|
||||
|
||||
# subnets
|
||||
subnet_newbits = ceil(log(length(local.availability_zones), 2))
|
||||
nets_newbits = ceil(log(length(local.nets), 2))
|
||||
azs_newbits = ceil(log(length(local.azs), 2))
|
||||
|
||||
net_cidr_blocks = {
|
||||
for i, v in keys(local.nets) : v => try(local.nets[v].cidr_block, cidrsubnet(local.cidr_block, local.nets_newbits, i))
|
||||
}
|
||||
|
||||
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
|
||||
default_route = v.default_route
|
||||
cidr_block = cidrsubnet(v.cidr_block, local.subnet_newbits, i)
|
||||
default_route = try(v.default_route, "")
|
||||
cidr_block = cidrsubnet(local.net_cidr_blocks[k], local.azs_newbits, i)
|
||||
tags = try(v.tags, { test : "none" })
|
||||
}
|
||||
}
|
||||
]
|
||||
])...)
|
||||
|
||||
# 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 : try(g.default_route == "igw", false)], 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 g in local.nets : basename(g.default_route) if try(startswith(g.default_route, "ngw/"), false)])
|
||||
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])
|
||||
]
|
||||
]))
|
||||
}
|
||||
+2
-2
@@ -9,6 +9,6 @@ output "vpc" {
|
||||
}
|
||||
|
||||
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] }
|
||||
description = "Map of public subnet IDs."
|
||||
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
|
||||
|
||||
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]))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -6,9 +6,9 @@ resource "aws_subnet" "self" {
|
||||
cidr_block = each.value.cidr_block
|
||||
availability_zone = each.value.availability_zone
|
||||
|
||||
tags = {
|
||||
Name = join("-", compact([var.prefix, each.key]))
|
||||
}
|
||||
tags = merge(each.value.tags, {
|
||||
Name = join("-", compact([local.prefix_name, each.key]))
|
||||
})
|
||||
}
|
||||
|
||||
resource "aws_eip" "self" {
|
||||
|
||||
+10
-24
@@ -1,32 +1,18 @@
|
||||
# variables.tf
|
||||
|
||||
variable "prefix" {
|
||||
description = "Value used to prefix resources name."
|
||||
variable "config_file" {
|
||||
description = "Network configuration file."
|
||||
type = string
|
||||
default = "noname"
|
||||
}
|
||||
|
||||
variable "cidr_block" {
|
||||
description = "The IPv4 CIDR block for the VPC."
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "availability_zones" {
|
||||
description = "Target availability zones."
|
||||
type = list(string)
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "network_acl_disabled" {
|
||||
description = "If disabled, default Network ACLs allow all inbound and outbound protocols."
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
variable "config_template" {
|
||||
description = "value"
|
||||
type = string
|
||||
default = "default"
|
||||
|
||||
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, "")
|
||||
}))
|
||||
validation {
|
||||
condition = contains(["default", "eks"], var.config_template)
|
||||
error_message = "config_template must be one of 'default' 'eks'"
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
@@ -32,7 +32,7 @@ resource "aws_default_network_acl" "self" {
|
||||
}
|
||||
|
||||
resource "aws_network_acl_rule" "all_ingress" {
|
||||
count = var.network_acl_disabled == true ? 1 : 0
|
||||
count = local.nacl_disabled ? 1 : 0
|
||||
network_acl_id = aws_default_network_acl.self.id
|
||||
rule_number = 100
|
||||
rule_action = "allow"
|
||||
@@ -42,7 +42,7 @@ resource "aws_network_acl_rule" "all_ingress" {
|
||||
}
|
||||
|
||||
resource "aws_network_acl_rule" "all_egress" {
|
||||
count = var.network_acl_disabled ? 1 : 0
|
||||
count = local.nacl_disabled ? 1 : 0
|
||||
network_acl_id = aws_default_network_acl.self.id
|
||||
rule_number = 100
|
||||
rule_action = "allow"
|
||||
@@ -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"]))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user