181 lines
5.8 KiB
Terraform
181 lines
5.8 KiB
Terraform
resource "aws_vpc" "protected" {
|
|
count = var.protect_from_destroy ? 1 : 0
|
|
|
|
region = var.region
|
|
cidr_block = var.vpc.cidr_block
|
|
instance_tenancy = "default"
|
|
enable_dns_support = var.vpc.enable_dns_support
|
|
enable_dns_hostnames = var.vpc.enable_dns_hostnames
|
|
tags = var.vpc.tags
|
|
|
|
lifecycle {
|
|
prevent_destroy = true
|
|
}
|
|
}
|
|
|
|
resource "aws_vpc" "unprotected" {
|
|
count = var.protect_from_destroy ? 0 : 1
|
|
|
|
region = var.region
|
|
cidr_block = var.vpc.cidr_block
|
|
instance_tenancy = "default"
|
|
enable_dns_support = var.vpc.enable_dns_support
|
|
enable_dns_hostnames = var.vpc.enable_dns_hostnames
|
|
tags = var.vpc.tags
|
|
}
|
|
|
|
resource "aws_subnet" "protected" {
|
|
for_each = var.protect_from_destroy ? var.subnets : {}
|
|
|
|
region = var.region
|
|
vpc_id = local.vpc.id
|
|
cidr_block = each.value.cidr_block
|
|
availability_zone = each.value.availability_zone
|
|
map_public_ip_on_launch = each.value.map_public_ip_on_launch
|
|
assign_ipv6_address_on_creation = each.value.assign_ipv6_address_on_creation
|
|
enable_dns64 = each.value.enable_dns64
|
|
private_dns_hostname_type_on_launch = each.value.private_dns_hostname_type_on_launch
|
|
enable_resource_name_dns_a_record_on_launch = each.value.enable_resource_name_dns_a_record_on_launch
|
|
enable_resource_name_dns_aaaa_record_on_launch = each.value.enable_resource_name_dns_aaaa_record_on_launch
|
|
tags = each.value.tags
|
|
|
|
lifecycle {
|
|
prevent_destroy = true
|
|
}
|
|
}
|
|
|
|
resource "aws_subnet" "unprotected" {
|
|
for_each = var.protect_from_destroy ? {} : var.subnets
|
|
|
|
region = var.region
|
|
vpc_id = local.vpc.id
|
|
cidr_block = each.value.cidr_block
|
|
availability_zone = each.value.availability_zone
|
|
map_public_ip_on_launch = each.value.map_public_ip_on_launch
|
|
assign_ipv6_address_on_creation = each.value.assign_ipv6_address_on_creation
|
|
enable_dns64 = each.value.enable_dns64
|
|
private_dns_hostname_type_on_launch = each.value.private_dns_hostname_type_on_launch
|
|
enable_resource_name_dns_a_record_on_launch = each.value.enable_resource_name_dns_a_record_on_launch
|
|
enable_resource_name_dns_aaaa_record_on_launch = each.value.enable_resource_name_dns_aaaa_record_on_launch
|
|
tags = each.value.tags
|
|
}
|
|
|
|
resource "aws_internet_gateway" "protected" {
|
|
count = var.protect_from_destroy ? 1 : 0
|
|
|
|
region = var.region
|
|
vpc_id = local.vpc.id
|
|
tags = var.internet_gateway_tags
|
|
|
|
lifecycle {
|
|
prevent_destroy = true
|
|
}
|
|
}
|
|
|
|
resource "aws_internet_gateway" "unprotected" {
|
|
count = var.protect_from_destroy ? 0 : 1
|
|
|
|
region = var.region
|
|
vpc_id = local.vpc.id
|
|
tags = var.internet_gateway_tags
|
|
}
|
|
|
|
# The default route table is adopted rather than created. Keep routes in
|
|
# standalone aws_route resources and require an import-only, zero-change plan.
|
|
resource "aws_default_route_table" "protected" {
|
|
count = var.protect_from_destroy ? 1 : 0
|
|
|
|
region = var.region
|
|
default_route_table_id = local.vpc.default_route_table_id
|
|
tags = var.route_table_tags.dmz
|
|
|
|
lifecycle {
|
|
prevent_destroy = true
|
|
}
|
|
}
|
|
|
|
resource "aws_default_route_table" "unprotected" {
|
|
count = var.protect_from_destroy ? 0 : 1
|
|
|
|
region = var.region
|
|
default_route_table_id = local.vpc.default_route_table_id
|
|
tags = var.route_table_tags.dmz
|
|
}
|
|
|
|
resource "aws_route_table" "protected" {
|
|
count = var.protect_from_destroy ? 1 : 0
|
|
|
|
region = var.region
|
|
vpc_id = local.vpc.id
|
|
tags = var.route_table_tags.intra
|
|
|
|
lifecycle {
|
|
prevent_destroy = true
|
|
}
|
|
}
|
|
|
|
resource "aws_route_table" "unprotected" {
|
|
count = var.protect_from_destroy ? 0 : 1
|
|
|
|
region = var.region
|
|
vpc_id = local.vpc.id
|
|
tags = var.route_table_tags.intra
|
|
}
|
|
|
|
resource "aws_route" "this" {
|
|
for_each = local.routes
|
|
|
|
region = var.region
|
|
route_table_id = local.route_table_ids[each.value.route_table_key]
|
|
destination_cidr_block = each.value.destination
|
|
gateway_id = each.value.gateway_id
|
|
network_interface_id = each.value.network_interface_id
|
|
}
|
|
|
|
resource "aws_route_table_association" "this" {
|
|
for_each = local.route_table_associations
|
|
|
|
region = var.region
|
|
subnet_id = local.subnet_ids[each.value.subnet_key]
|
|
route_table_id = local.route_table_ids[each.value.route_table_key]
|
|
}
|
|
|
|
# The provider manages default NACL rules as an absolute inline set and is
|
|
# incompatible with aws_network_acl_rule for the same ACL.
|
|
resource "aws_default_network_acl" "this" {
|
|
region = var.region
|
|
default_network_acl_id = local.vpc.default_network_acl_id
|
|
subnet_ids = values(local.subnet_ids)
|
|
|
|
ingress {
|
|
protocol = -1
|
|
rule_no = 100
|
|
action = "allow"
|
|
cidr_block = "0.0.0.0/0"
|
|
from_port = 0
|
|
to_port = 0
|
|
}
|
|
|
|
egress {
|
|
protocol = -1
|
|
rule_no = 100
|
|
action = "allow"
|
|
cidr_block = "0.0.0.0/0"
|
|
from_port = 0
|
|
to_port = 0
|
|
}
|
|
}
|
|
|
|
resource "aws_vpc_dhcp_options" "this" {
|
|
region = var.region
|
|
domain_name = var.dhcp_options.domain_name
|
|
domain_name_servers = var.dhcp_options.domain_name_servers
|
|
tags = var.dhcp_options.tags
|
|
}
|
|
|
|
resource "aws_vpc_dhcp_options_association" "this" {
|
|
region = var.region
|
|
vpc_id = local.vpc.id
|
|
dhcp_options_id = aws_vpc_dhcp_options.this.id
|
|
}
|