From 44335356374803cc1944a6adf30776ef91363194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E8=94=B5=E4=B8=80=E5=BC=98?= Date: Sat, 14 Nov 2026 18:13:08 +0900 Subject: [PATCH] Add group 3 network imports --- envs/prod/imports.tf | 94 +++++++ envs/prod/main.tf | 258 ++++++++++++++++++++ modules/audit_logging_encryption/outputs.tf | 10 + modules/iam_auth/outputs.tf | 4 + modules/network_public/compute.tf | 93 +++++++ modules/network_public/flow_logs.tf | 28 +++ modules/network_public/locals.tf | 73 ++++++ modules/network_public/network.tf | 180 ++++++++++++++ modules/network_public/security_groups.tf | 52 ++++ modules/network_public/variables.tf | 162 ++++++++++++ modules/network_public/versions.tf | 10 + 11 files changed, 964 insertions(+) create mode 100644 modules/iam_auth/outputs.tf create mode 100644 modules/network_public/compute.tf create mode 100644 modules/network_public/flow_logs.tf create mode 100644 modules/network_public/locals.tf create mode 100644 modules/network_public/network.tf create mode 100644 modules/network_public/security_groups.tf create mode 100644 modules/network_public/variables.tf create mode 100644 modules/network_public/versions.tf diff --git a/envs/prod/imports.tf b/envs/prod/imports.tf index 332ac5c..9906ba0 100644 --- a/envs/prod/imports.tf +++ b/envs/prod/imports.tf @@ -105,3 +105,97 @@ import { to = module.iam_auth.aws_iam_user_policy.sts_route53 id = "vault-iam-create:sts-route53" } + +import { + to = module.network_public.aws_vpc.protected[0] + id = "vpc-68ee3b0d" +} + +import { + for_each = local.network_subnet_ids + to = module.network_public.aws_subnet.protected[each.key] + id = each.value +} + +import { + to = module.network_public.aws_internet_gateway.protected[0] + id = "igw-bfe81eda" +} + +import { + to = module.network_public.aws_default_route_table.protected[0] + id = "vpc-68ee3b0d" +} + +import { + to = module.network_public.aws_route_table.protected[0] + id = "rtb-0cb396364ef535126" +} + +import { + for_each = local.network_route_import_ids + to = module.network_public.aws_route.this[each.key] + id = each.value +} + +import { + for_each = local.network_route_association_import_ids + to = module.network_public.aws_route_table_association.this[each.key] + id = each.value +} + +import { + to = module.network_public.aws_default_network_acl.this + id = "acl-208c5e45" +} + +import { + to = module.network_public.aws_vpc_dhcp_options.this + id = "dopt-816972e3" +} + +import { + to = module.network_public.aws_vpc_dhcp_options_association.this + id = "vpc-68ee3b0d" +} + +import { + to = module.network_public.aws_security_group.web + id = "sg-b5e45ed0" +} + +import { + for_each = local.web_ingress_rule_ids + to = module.network_public.aws_vpc_security_group_ingress_rule.web[each.key] + id = each.value +} + +import { + to = module.network_public.aws_vpc_security_group_egress_rule.web_all_ipv4 + id = "sgr-0f27372c08f83f904" +} + +import { + to = module.network_public.aws_instance.web_protected[0] + id = "i-0a2d6193bfd326038" +} + +import { + to = module.network_public.aws_flow_log.s3 + id = local.vpc_flow_log_ids.s3 +} + +import { + to = module.network_public.aws_flow_log.cloudwatch + id = local.vpc_flow_log_ids.cloudwatch +} + +import { + to = module.network_public.aws_key_pair.amazon + id = "amazon" +} + +import { + to = module.network_public.aws_default_security_group.this + id = "sg-d3e15bb6" +} diff --git a/envs/prod/main.tf b/envs/prod/main.tf index 65208b8..74dfcd4 100644 --- a/envs/prod/main.tf +++ b/envs/prod/main.tf @@ -10,6 +10,48 @@ locals { retention_in_days = 14 } } + + network_subnet_ids = { + intra_a = "subnet-08ec1e8aa9b59a741" + dmz = "subnet-0254975b" + intra_d = "subnet-0a6f7342f20992028" + } + + network_route_import_ids = { + dmz_private_a = "rtb-881ca2ec_172.20.0.0/24" + dmz_private_b = "rtb-881ca2ec_192.168.0.0/16" + dmz_internet = "rtb-881ca2ec_0.0.0.0/0" + intra_private_a = "rtb-0cb396364ef535126_172.20.0.0/24" + intra_private_b = "rtb-0cb396364ef535126_192.168.0.0/16" + intra_default = "rtb-0cb396364ef535126_0.0.0.0/0" + } + + network_route_association_import_ids = { + dmz = "subnet-0254975b/rtb-881ca2ec" + intra_a = "subnet-08ec1e8aa9b59a741/rtb-0cb396364ef535126" + intra_d = "subnet-0a6f7342f20992028/rtb-0cb396364ef535126" + } + + web_ingress_rule_ids = { + icmp_private = "sgr-047648c0e31750f8d" + icmp_vyos = "sgr-0c6e27ce7adeebf87" + ssh_vyos = "sgr-004a45983fc47a5d3" + http = "sgr-068d0f27af39e8e1e" + https = "sgr-0e751496addb91413" + http3 = "sgr-0ca29322688fd5b02" + consul_server = "sgr-0fca5bbcb9ee874b0" + consul_lan_tcp = "sgr-0a9ebdc07f7e552fa" + consul_lan_udp = "sgr-0038e075c7bfd5dc5" + ssh_admin = "sgr-092cd72b6bb22d201" + ssh_git = "sgr-0f8d953855c7315f4" + wireguard = "sgr-0d85fe840d3fc1172" + prometheus = "sgr-074e62f626b5ffd11" + } + + vpc_flow_log_ids = { + s3 = "fl-078c5e5e029ddbf59" + cloudwatch = "fl-04b92a3f869a9f322" + } } module "audit_logging_encryption" { @@ -56,3 +98,219 @@ module "iam_auth" { vault_kms_resource_arns = module.audit_logging_encryption.vault_kms_resource_arns } + +module "network_public" { + source = "../../modules/network_public" + + region = var.region + name_prefix = var.name_prefix + protect_from_destroy = true + + vpc = { + cidr_block = "10.0.0.0/16" + enable_dns_support = true + enable_dns_hostnames = true + tags = { + Name = "NW" + Env = "homelab" + } + } + + subnets = { + intra_a = { + cidr_block = "10.0.110.0/24" + availability_zone = "ap-northeast-1a" + tags = { + Name = "NW-Intra-az4" + Env = "homelab" + } + } + dmz = { + cidr_block = "10.0.1.0/24" + availability_zone = "ap-northeast-1c" + tags = { + Name = "NW-DMZ-az1c" + } + } + intra_d = { + cidr_block = "10.0.120.0/24" + availability_zone = "ap-northeast-1d" + tags = { + Name = "NW-Intra-az2" + } + } + } + + route_table_tags = { + dmz = { Name = "ACT-DMZ" } + intra = { Name = "ACT-Intra" } + } + + internet_gateway_tags = { + Env = "homelab" + } + + route_destinations = { + private_network_a = "172.20.0.0/24" + private_network_b = "192.168.0.0/16" + default_ipv4 = "0.0.0.0/0" + } + + vyos_network_interface_id = "eni-066a00f06de027688" + vyos_security_group_id = "sg-827947e5" + + dhcp_options = { + domain_name = "ap-northeast-1.compute.internal" + domain_name_servers = ["AmazonProvidedDNS"] + } + + ssh_security_group = { + name = "SSH Enabled" + description = "launch-wizard-1 created 2015-03-18T23:05:30.230+09:00" + } + + ssh_ingress_rules = { + icmp_private = { + description = "ICMP" + ip_protocol = "icmp" + cidr_ipv4 = "10.100.0.0/16" + } + icmp_vyos = { + description = "ICMP" + ip_protocol = "icmp" + use_vyos_security_group = true + } + ssh_vyos = { + description = "SSH" + ip_protocol = "tcp" + from_port = 22 + to_port = 22 + use_vyos_security_group = true + } + http = { + description = "HTTP" + ip_protocol = "tcp" + from_port = 80 + to_port = 80 + cidr_ipv4 = "0.0.0.0/0" + } + https = { + description = "HTTPs" + ip_protocol = "tcp" + from_port = 443 + to_port = 443 + cidr_ipv4 = "0.0.0.0/0" + } + http3 = { + description = "" + ip_protocol = "udp" + from_port = 443 + to_port = 443 + cidr_ipv4 = "0.0.0.0/0" + } + consul_server = { + description = "consul" + ip_protocol = "tcp" + from_port = 8300 + to_port = 8300 + use_vyos_security_group = true + } + consul_lan_tcp = { + description = "consul" + ip_protocol = "tcp" + from_port = 8301 + to_port = 8301 + use_vyos_security_group = true + } + consul_lan_udp = { + description = "consul" + ip_protocol = "udp" + from_port = 8301 + to_port = 8301 + use_vyos_security_group = true + } + ssh_admin = { + description = "SSH" + ip_protocol = "tcp" + from_port = 10022 + to_port = 10022 + cidr_ipv4 = "0.0.0.0/0" + } + ssh_git = { + description = "SSH for Git" + ip_protocol = "tcp" + from_port = 50022 + to_port = 50022 + cidr_ipv4 = "0.0.0.0/0" + } + wireguard = { + description = "wireguard" + ip_protocol = "udp" + from_port = 51820 + to_port = 51820 + cidr_ipv4 = "0.0.0.0/0" + } + prometheus = { + description = "prometheus" + ip_protocol = "tcp" + from_port = 60000 + to_port = 60000 + use_vyos_security_group = true + } + } + + web_instance = { + name = "web2404" + ami = "ami-0b20f552f63953f0e" + instance_type = "t2.small" + subnet_key = "dmz" + + associate_public_ip_address = true + source_dest_check = true + ebs_optimized = false + monitoring = false + + metadata_options = { + http_endpoint = "enabled" + http_tokens = "required" + http_put_response_hop_limit = 2 + http_protocol_ipv6 = "disabled" + instance_metadata_tags = "disabled" + } + + root_block_device = { + delete_on_termination = true + encrypted = false + volume_size = 30 + volume_type = "gp3" + iops = 3000 + throughput = 125 + } + + tags = { + Env = "homelab" + } + } + + key_pair = { + name = "amazon" + public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQtav7Kk5kQhycnIUieMbV5dULwXlagkozkFERf0KQG2ZaVwzscFNNbJyK0KpmQ08boJw7j6XC/iiM1TCXvzUzkLcWZzHjVMM6ZIMSp784dZKXl6zZoJK8xOznsHjopVC96kVJIMuS9CObI3K2M3UNWZh6Un5aB3BFEhFsVObaUX4PnEsBGtuC77Sx8XBfnyuhYWzCaZLEbIEsdXL/T/E4bsS5YnTif2dFI789QAPgZY5CWwKponlqvd2I1xlPYJbqzCGbLX0PV42TZkyRL4hDRU0rlXsoYMzSZCynwCJCeQsVsrjk6IPH73wZNCSCCdLIRWmKzVZaT5hNCFZ/F2X7 amazon" + } + + flow_logs = { + s3 = { + destination_arn = module.audit_logging_encryption.flow_logs_bucket_arn + tags = { + Name = "ACT-dmz-flowlogs" + } + } + cloudwatch = { + log_group_arn = module.audit_logging_encryption.vpc_flow_logs_log_group_arn + iam_role_arn = module.iam_auth.flow_logs_role_arn + tags = { + Name = "Cloudwatch" + Env = "homelab" + } + } + } +} diff --git a/modules/audit_logging_encryption/outputs.tf b/modules/audit_logging_encryption/outputs.tf index 4a7b5e8..09cc1be 100644 --- a/modules/audit_logging_encryption/outputs.tf +++ b/modules/audit_logging_encryption/outputs.tf @@ -5,3 +5,13 @@ output "vault_kms_resource_arns" { aws_kms_alias.vault.arn, ] } + +output "flow_logs_bucket_arn" { + description = "ARN of the managed S3 bucket used by VPC Flow Logs." + value = "arn:aws:s3:::${local.bucket_ids["flowlogs"]}" +} + +output "vpc_flow_logs_log_group_arn" { + description = "ARN of the managed CloudWatch log group used by VPC Flow Logs." + value = aws_cloudwatch_log_group.this["vpc_flowlogs"].arn +} diff --git a/modules/iam_auth/outputs.tf b/modules/iam_auth/outputs.tf new file mode 100644 index 0000000..4dff424 --- /dev/null +++ b/modules/iam_auth/outputs.tf @@ -0,0 +1,4 @@ +output "flow_logs_role_arn" { + description = "ARN of the IAM role used by VPC Flow Logs for CloudWatch delivery." + value = aws_iam_role.flow_logs.arn +} diff --git a/modules/network_public/compute.tf b/modules/network_public/compute.tf new file mode 100644 index 0000000..e95ab98 --- /dev/null +++ b/modules/network_public/compute.tf @@ -0,0 +1,93 @@ +resource "aws_key_pair" "amazon" { + region = var.region + + key_name = local.key_pair_name + public_key = var.key_pair.public_key + tags = var.key_pair.tags + + lifecycle { + # Provider 6.73 cannot read public key material during refresh and would + # otherwise plan a replacement immediately after import. + ignore_changes = [public_key] + } +} + +resource "aws_instance" "web_protected" { + count = var.protect_from_destroy ? 1 : 0 + + region = var.region + ami = var.web_instance.ami + instance_type = var.web_instance.instance_type + subnet_id = local.subnet_ids[var.web_instance.subnet_key] + vpc_security_group_ids = [aws_security_group.web.id] + key_name = aws_key_pair.amazon.key_name + associate_public_ip_address = var.web_instance.associate_public_ip_address + source_dest_check = var.web_instance.source_dest_check + ebs_optimized = var.web_instance.ebs_optimized + monitoring = var.web_instance.monitoring + + metadata_options { + http_endpoint = var.web_instance.metadata_options.http_endpoint + http_tokens = var.web_instance.metadata_options.http_tokens + http_put_response_hop_limit = var.web_instance.metadata_options.http_put_response_hop_limit + http_protocol_ipv6 = var.web_instance.metadata_options.http_protocol_ipv6 + instance_metadata_tags = var.web_instance.metadata_options.instance_metadata_tags + } + + root_block_device { + delete_on_termination = var.web_instance.root_block_device.delete_on_termination + encrypted = var.web_instance.root_block_device.encrypted + volume_size = var.web_instance.root_block_device.volume_size + volume_type = var.web_instance.root_block_device.volume_type + iops = var.web_instance.root_block_device.iops + throughput = var.web_instance.root_block_device.throughput + } + + tags = merge(var.web_instance.tags, { Name = local.web_instance_name }) + + lifecycle { + prevent_destroy = true + # AMI and user data are intentionally managed outside this import. The + # provider still reads user data during import/refresh and stores it in + # state; ignore_changes only suppresses drift and does not hide the value. + ignore_changes = [ami, user_data, user_data_base64] + } +} + +resource "aws_instance" "web_unprotected" { + count = var.protect_from_destroy ? 0 : 1 + + region = var.region + ami = var.web_instance.ami + instance_type = var.web_instance.instance_type + subnet_id = local.subnet_ids[var.web_instance.subnet_key] + vpc_security_group_ids = [aws_security_group.web.id] + key_name = aws_key_pair.amazon.key_name + associate_public_ip_address = var.web_instance.associate_public_ip_address + source_dest_check = var.web_instance.source_dest_check + ebs_optimized = var.web_instance.ebs_optimized + monitoring = var.web_instance.monitoring + + metadata_options { + http_endpoint = var.web_instance.metadata_options.http_endpoint + http_tokens = var.web_instance.metadata_options.http_tokens + http_put_response_hop_limit = var.web_instance.metadata_options.http_put_response_hop_limit + http_protocol_ipv6 = var.web_instance.metadata_options.http_protocol_ipv6 + instance_metadata_tags = var.web_instance.metadata_options.instance_metadata_tags + } + + root_block_device { + delete_on_termination = var.web_instance.root_block_device.delete_on_termination + encrypted = var.web_instance.root_block_device.encrypted + volume_size = var.web_instance.root_block_device.volume_size + volume_type = var.web_instance.root_block_device.volume_type + iops = var.web_instance.root_block_device.iops + throughput = var.web_instance.root_block_device.throughput + } + + tags = merge(var.web_instance.tags, { Name = local.web_instance_name }) + + lifecycle { + ignore_changes = [ami, user_data, user_data_base64] + } +} diff --git a/modules/network_public/flow_logs.tf b/modules/network_public/flow_logs.tf new file mode 100644 index 0000000..f6b2b53 --- /dev/null +++ b/modules/network_public/flow_logs.tf @@ -0,0 +1,28 @@ +resource "aws_flow_log" "s3" { + region = var.region + + vpc_id = local.vpc.id + traffic_type = var.flow_logs.s3.traffic_type + log_destination_type = "s3" + log_destination = var.flow_logs.s3.destination_arn + max_aggregation_interval = var.flow_logs.s3.max_aggregation_interval + tags = var.flow_logs.s3.tags + + destination_options { + file_format = "parquet" + hive_compatible_partitions = false + per_hour_partition = true + } +} + +resource "aws_flow_log" "cloudwatch" { + region = var.region + + vpc_id = local.vpc.id + traffic_type = var.flow_logs.cloudwatch.traffic_type + log_destination_type = "cloud-watch-logs" + log_destination = var.flow_logs.cloudwatch.log_group_arn + iam_role_arn = var.flow_logs.cloudwatch.iam_role_arn + max_aggregation_interval = var.flow_logs.cloudwatch.max_aggregation_interval + tags = var.flow_logs.cloudwatch.tags +} diff --git a/modules/network_public/locals.tf b/modules/network_public/locals.tf new file mode 100644 index 0000000..36326ca --- /dev/null +++ b/modules/network_public/locals.tf @@ -0,0 +1,73 @@ +locals { + vpc = var.protect_from_destroy ? aws_vpc.protected[0] : aws_vpc.unprotected[0] + + subnet_ids = merge( + { for key, subnet in aws_subnet.protected : key => subnet.id }, + { for key, subnet in aws_subnet.unprotected : key => subnet.id }, + ) + + internet_gateway_id = var.protect_from_destroy ? aws_internet_gateway.protected[0].id : aws_internet_gateway.unprotected[0].id + + route_table_ids = { + dmz = var.protect_from_destroy ? aws_default_route_table.protected[0].id : aws_default_route_table.unprotected[0].id + intra = var.protect_from_destroy ? aws_route_table.protected[0].id : aws_route_table.unprotected[0].id + } + + routes = { + dmz_private_a = { + route_table_key = "dmz" + destination = var.route_destinations.private_network_a + gateway_id = null + network_interface_id = var.vyos_network_interface_id + } + dmz_private_b = { + route_table_key = "dmz" + destination = var.route_destinations.private_network_b + gateway_id = null + network_interface_id = var.vyos_network_interface_id + } + dmz_internet = { + route_table_key = "dmz" + destination = var.route_destinations.default_ipv4 + gateway_id = local.internet_gateway_id + network_interface_id = null + } + intra_private_a = { + route_table_key = "intra" + destination = var.route_destinations.private_network_a + gateway_id = null + network_interface_id = var.vyos_network_interface_id + } + intra_private_b = { + route_table_key = "intra" + destination = var.route_destinations.private_network_b + gateway_id = null + network_interface_id = var.vyos_network_interface_id + } + intra_default = { + route_table_key = "intra" + destination = var.route_destinations.default_ipv4 + gateway_id = null + network_interface_id = var.vyos_network_interface_id + } + } + + route_table_associations = { + dmz = { + subnet_key = "dmz" + route_table_key = "dmz" + } + intra_a = { + subnet_key = "intra_a" + route_table_key = "intra" + } + intra_d = { + subnet_key = "intra_d" + route_table_key = "intra" + } + } + + ssh_security_group_name = coalesce(var.ssh_security_group.name, "${var.name_prefix}-ssh-enabled") + web_instance_name = coalesce(var.web_instance.name, "${var.name_prefix}-web") + key_pair_name = coalesce(var.key_pair.name, "${var.name_prefix}-key") +} diff --git a/modules/network_public/network.tf b/modules/network_public/network.tf new file mode 100644 index 0000000..260f632 --- /dev/null +++ b/modules/network_public/network.tf @@ -0,0 +1,180 @@ +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 +} diff --git a/modules/network_public/security_groups.tf b/modules/network_public/security_groups.tf new file mode 100644 index 0000000..5d16526 --- /dev/null +++ b/modules/network_public/security_groups.tf @@ -0,0 +1,52 @@ +resource "aws_security_group" "web" { + region = var.region + + name = local.ssh_security_group_name + description = var.ssh_security_group.description + vpc_id = local.vpc.id + tags = var.ssh_security_group.tags +} + +resource "aws_vpc_security_group_ingress_rule" "web" { + for_each = var.ssh_ingress_rules + + region = var.region + security_group_id = aws_security_group.web.id + description = each.value.description + ip_protocol = each.value.ip_protocol + from_port = each.value.from_port + to_port = each.value.to_port + cidr_ipv4 = each.value.cidr_ipv4 + referenced_security_group_id = each.value.use_vyos_security_group ? var.vyos_security_group_id : null +} + +resource "aws_vpc_security_group_egress_rule" "web_all_ipv4" { + region = var.region + + security_group_id = aws_security_group.web.id + ip_protocol = "-1" + cidr_ipv4 = "0.0.0.0/0" +} + +# The default SG is adopted. Its two rules are managed as an absolute inline +# set and must not be mixed with standalone rule resources. +resource "aws_default_security_group" "this" { + region = var.region + vpc_id = local.vpc.id + + ingress { + description = null + protocol = "tcp" + from_port = 22 + to_port = 22 + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + description = null + protocol = "-1" + from_port = 0 + to_port = 0 + cidr_blocks = ["0.0.0.0/0"] + } +} diff --git a/modules/network_public/variables.tf b/modules/network_public/variables.tf new file mode 100644 index 0000000..88ad914 --- /dev/null +++ b/modules/network_public/variables.tf @@ -0,0 +1,162 @@ +variable "region" { + description = "AWS region for the managed network and public path." + type = string +} + +variable "name_prefix" { + description = "Prefix used for isolated copies when an explicit production name is omitted." + type = string +} + +variable "protect_from_destroy" { + description = "Protect VPC, subnets, internet gateway, route tables, and web instance from destroy. prod は true 固定。既存 workspace で true→false に反転しない。" + type = bool + default = true +} + +variable "vpc" { + description = "Existing VPC configuration." + type = object({ + cidr_block = string + enable_dns_support = optional(bool, true) + enable_dns_hostnames = optional(bool, true) + tags = optional(map(string), {}) + }) +} + +variable "subnets" { + description = "Subnets keyed by a stable logical name." + type = map(object({ + cidr_block = string + availability_zone = string + map_public_ip_on_launch = optional(bool, false) + assign_ipv6_address_on_creation = optional(bool, false) + enable_dns64 = optional(bool, false) + private_dns_hostname_type_on_launch = optional(string, "ip-name") + enable_resource_name_dns_a_record_on_launch = optional(bool, false) + enable_resource_name_dns_aaaa_record_on_launch = optional(bool, false) + tags = optional(map(string), {}) + })) +} + +variable "route_table_tags" { + description = "Tags for the main DMZ and custom intra route tables." + type = object({ + dmz = optional(map(string), {}) + intra = optional(map(string), {}) + }) + default = {} +} + +variable "internet_gateway_tags" { + description = "Tags on the existing internet gateway." + type = map(string) + default = {} +} + +variable "route_destinations" { + description = "Existing route destinations. Values must stay aligned with the production route tables." + type = object({ + private_network_a = string + private_network_b = string + default_ipv4 = optional(string, "0.0.0.0/0") + }) +} + +variable "vyos_network_interface_id" { + description = "Existing VyOS primary ENI referenced by routes. The VyOS instance itself is managed separately." + type = string +} + +variable "vyos_security_group_id" { + description = "Existing VyOS security group referenced as a source. The group itself is managed separately." + type = string +} + +variable "dhcp_options" { + description = "Existing VPC DHCP options." + type = object({ + domain_name = string + domain_name_servers = list(string) + tags = optional(map(string), {}) + }) +} + +variable "ssh_security_group" { + description = "Existing security group used by the public web instance." + type = object({ + name = optional(string) + description = string + tags = optional(map(string), {}) + }) +} + +variable "ssh_ingress_rules" { + description = "Ingress rules for the web security group, one source per resource." + type = map(object({ + description = optional(string) + ip_protocol = string + from_port = optional(number) + to_port = optional(number) + cidr_ipv4 = optional(string) + use_vyos_security_group = optional(bool, false) + })) +} + +variable "web_instance" { + description = "Existing public web EC2 instance. user_data is intentionally absent." + type = object({ + name = optional(string) + ami = string + instance_type = string + subnet_key = string + associate_public_ip_address = optional(bool, true) + source_dest_check = optional(bool, true) + ebs_optimized = optional(bool, false) + monitoring = optional(bool, false) + metadata_options = object({ + http_endpoint = string + http_tokens = string + http_put_response_hop_limit = number + http_protocol_ipv6 = string + instance_metadata_tags = string + }) + root_block_device = object({ + delete_on_termination = bool + encrypted = bool + volume_size = number + volume_type = string + iops = optional(number) + throughput = optional(number) + }) + tags = optional(map(string), {}) + }) +} + +variable "key_pair" { + description = "Existing EC2 key pair. Public key material is not secret; private key material is never handled." + type = object({ + name = optional(string) + public_key = string + tags = optional(map(string), {}) + }) +} + +variable "flow_logs" { + description = "Existing VPC Flow Log destinations managed by previously imported modules." + type = object({ + s3 = object({ + destination_arn = string + traffic_type = optional(string, "ALL") + max_aggregation_interval = optional(number, 60) + tags = optional(map(string), {}) + }) + cloudwatch = object({ + log_group_arn = string + iam_role_arn = string + traffic_type = optional(string, "ALL") + max_aggregation_interval = optional(number, 600) + tags = optional(map(string), {}) + }) + }) +} diff --git a/modules/network_public/versions.tf b/modules/network_public/versions.tf new file mode 100644 index 0000000..3d89ea3 --- /dev/null +++ b/modules/network_public/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = "= 1.17.2" + + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 6.73" + } + } +}