diff --git a/envs/prod/imports.tf b/envs/prod/imports.tf index 9906ba0..a261d9b 100644 --- a/envs/prod/imports.tf +++ b/envs/prod/imports.tf @@ -199,3 +199,24 @@ import { to = module.network_public.aws_default_security_group.this id = "sg-d3e15bb6" } + +import { + to = module.network_vyos.aws_security_group.protected[0] + id = "sg-827947e5" +} + +import { + for_each = local.vyos_ingress_rule_ids + to = module.network_vyos.aws_vpc_security_group_ingress_rule.this[each.key] + id = each.value +} + +import { + to = module.network_vyos.aws_vpc_security_group_egress_rule.all_ipv4 + id = "sgr-024024d4f607e7dfd" +} + +import { + to = module.network_vyos.aws_instance.protected[0] + id = "i-0ca9aa005fc681496" +} diff --git a/envs/prod/main.tf b/envs/prod/main.tf index 74dfcd4..8bd11a5 100644 --- a/envs/prod/main.tf +++ b/envs/prod/main.tf @@ -48,6 +48,14 @@ locals { prometheus = "sgr-074e62f626b5ffd11" } + vyos_ingress_rule_ids = { + vpc_all = "sgr-0928f20c49ffde656" + mosh = "sgr-0fbe8c57e8f184cdf" + home_all = "sgr-034cc92dca18b3394" + ssh = "sgr-0e86e1d0cd516c8bd" + wireguard = "sgr-03f382fd75347b435" + } + vpc_flow_log_ids = { s3 = "fl-078c5e5e029ddbf59" cloudwatch = "fl-04b92a3f869a9f322" @@ -314,3 +322,84 @@ module "network_public" { } } } + +module "network_vyos" { + source = "../../modules/network_vyos" + + region = var.region + name_prefix = var.name_prefix + protect_from_destroy = true + + vpc_id = "vpc-68ee3b0d" + subnet_id = "subnet-0254975b" + key_name = "amazon" + + security_group = { + name = "VyOS-Router" + description = "VyOS-Router FW" + } + + ingress_rules = { + vpc_all = { + ip_protocol = "-1" + cidr_ipv4 = "10.0.0.0/16" + } + mosh = { + description = "Allow mosh" + ip_protocol = "udp" + from_port = 60000 + to_port = 61000 + cidr_ipv4 = "0.0.0.0/0" + } + home_all = { + description = "Home" + ip_protocol = "-1" + cidr_ipv4 = "192.168.0.0/16" + } + ssh = { + description = "Allow SSH" + ip_protocol = "tcp" + from_port = 22 + to_port = 22 + cidr_ipv4 = "0.0.0.0/0" + } + wireguard = { + description = "Allow Wireguard" + ip_protocol = "udp" + from_port = 51820 + to_port = 51820 + cidr_ipv4 = "0.0.0.0/0" + } + } + + instance = { + name = "VyOS" + ami = "ami-09c81926f92d4475d" + instance_type = "t2.nano" + private_ip = "10.0.1.119" + associate_public_ip_address = true + source_dest_check = false + ebs_optimized = false + monitoring = false + + metadata_options = { + http_endpoint = "enabled" + http_tokens = "optional" + http_put_response_hop_limit = 1 + http_protocol_ipv6 = "disabled" + instance_metadata_tags = "disabled" + } + + root_block_device = { + delete_on_termination = true + encrypted = false + volume_size = 4 + volume_type = "gp2" + iops = 100 + } + + tags = { + Env = "homelab" + } + } +} diff --git a/modules/network_vyos/compute.tf b/modules/network_vyos/compute.tf new file mode 100644 index 0000000..a7289a3 --- /dev/null +++ b/modules/network_vyos/compute.tf @@ -0,0 +1,80 @@ +resource "aws_instance" "protected" { + count = var.protect_from_destroy ? 1 : 0 + + region = var.region + ami = var.instance.ami + instance_type = var.instance.instance_type + subnet_id = var.subnet_id + private_ip = var.instance.private_ip + vpc_security_group_ids = [local.security_group.id] + key_name = var.key_name + associate_public_ip_address = var.instance.associate_public_ip_address + source_dest_check = var.instance.source_dest_check + ebs_optimized = var.instance.ebs_optimized + monitoring = var.instance.monitoring + + metadata_options { + http_endpoint = var.instance.metadata_options.http_endpoint + http_tokens = var.instance.metadata_options.http_tokens + http_put_response_hop_limit = var.instance.metadata_options.http_put_response_hop_limit + http_protocol_ipv6 = var.instance.metadata_options.http_protocol_ipv6 + instance_metadata_tags = var.instance.metadata_options.instance_metadata_tags + } + + root_block_device { + delete_on_termination = var.instance.root_block_device.delete_on_termination + encrypted = var.instance.root_block_device.encrypted + volume_size = var.instance.root_block_device.volume_size + volume_type = var.instance.root_block_device.volume_type + iops = var.instance.root_block_device.iops + throughput = var.instance.root_block_device.throughput + } + + tags = merge(var.instance.tags, { Name = local.instance_name }) + + lifecycle { + prevent_destroy = true + # The deregistered AMI and user data remain externally managed. Import and + # refresh still read user data into state; ignore_changes only hides drift. + ignore_changes = [ami, user_data, user_data_base64] + } +} + +resource "aws_instance" "unprotected" { + count = var.protect_from_destroy ? 0 : 1 + + region = var.region + ami = var.instance.ami + instance_type = var.instance.instance_type + subnet_id = var.subnet_id + private_ip = var.instance.private_ip + vpc_security_group_ids = [local.security_group.id] + key_name = var.key_name + associate_public_ip_address = var.instance.associate_public_ip_address + source_dest_check = var.instance.source_dest_check + ebs_optimized = var.instance.ebs_optimized + monitoring = var.instance.monitoring + + metadata_options { + http_endpoint = var.instance.metadata_options.http_endpoint + http_tokens = var.instance.metadata_options.http_tokens + http_put_response_hop_limit = var.instance.metadata_options.http_put_response_hop_limit + http_protocol_ipv6 = var.instance.metadata_options.http_protocol_ipv6 + instance_metadata_tags = var.instance.metadata_options.instance_metadata_tags + } + + root_block_device { + delete_on_termination = var.instance.root_block_device.delete_on_termination + encrypted = var.instance.root_block_device.encrypted + volume_size = var.instance.root_block_device.volume_size + volume_type = var.instance.root_block_device.volume_type + iops = var.instance.root_block_device.iops + throughput = var.instance.root_block_device.throughput + } + + tags = merge(var.instance.tags, { Name = local.instance_name }) + + lifecycle { + ignore_changes = [ami, user_data, user_data_base64] + } +} diff --git a/modules/network_vyos/locals.tf b/modules/network_vyos/locals.tf new file mode 100644 index 0000000..e196e52 --- /dev/null +++ b/modules/network_vyos/locals.tf @@ -0,0 +1,7 @@ +locals { + security_group = var.protect_from_destroy ? aws_security_group.protected[0] : aws_security_group.unprotected[0] + instance = var.protect_from_destroy ? aws_instance.protected[0] : aws_instance.unprotected[0] + + security_group_name = coalesce(var.security_group.name, "${var.name_prefix}-vyos-router") + instance_name = coalesce(var.instance.name, "${var.name_prefix}-vyos") +} diff --git a/modules/network_vyos/outputs.tf b/modules/network_vyos/outputs.tf new file mode 100644 index 0000000..50fd967 --- /dev/null +++ b/modules/network_vyos/outputs.tf @@ -0,0 +1,14 @@ +output "instance_id" { + description = "Managed VyOS instance ID." + value = local.instance.id +} + +output "primary_network_interface_id" { + description = "Managed VyOS primary network interface ID for route targets." + value = local.instance.primary_network_interface_id +} + +output "security_group_id" { + description = "Managed VyOS security group ID for referenced ingress rules." + value = local.security_group.id +} diff --git a/modules/network_vyos/security_groups.tf b/modules/network_vyos/security_groups.tf new file mode 100644 index 0000000..9fd43f3 --- /dev/null +++ b/modules/network_vyos/security_groups.tf @@ -0,0 +1,45 @@ +resource "aws_security_group" "protected" { + count = var.protect_from_destroy ? 1 : 0 + + region = var.region + + name = local.security_group_name + description = var.security_group.description + vpc_id = var.vpc_id + tags = var.security_group.tags + + lifecycle { + prevent_destroy = true + } +} + +resource "aws_security_group" "unprotected" { + count = var.protect_from_destroy ? 0 : 1 + + region = var.region + + name = local.security_group_name + description = var.security_group.description + vpc_id = var.vpc_id + tags = var.security_group.tags +} + +resource "aws_vpc_security_group_ingress_rule" "this" { + for_each = var.ingress_rules + + region = var.region + security_group_id = local.security_group.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 +} + +resource "aws_vpc_security_group_egress_rule" "all_ipv4" { + region = var.region + + security_group_id = local.security_group.id + ip_protocol = "-1" + cidr_ipv4 = "0.0.0.0/0" +} diff --git a/modules/network_vyos/variables.tf b/modules/network_vyos/variables.tf new file mode 100644 index 0000000..b9d7634 --- /dev/null +++ b/modules/network_vyos/variables.tf @@ -0,0 +1,80 @@ +variable "region" { + description = "AWS region for the managed VyOS network 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 the VyOS instance and security group from destroy. prod は true 固定。既存 workspace で true→false に反転しない。" + type = bool + default = true +} + +variable "vpc_id" { + description = "Existing VPC ID containing the VyOS security group." + type = string +} + +variable "subnet_id" { + description = "Existing subnet ID containing the VyOS primary network interface." + type = string +} + +variable "key_name" { + description = "Existing EC2 key pair name used by VyOS." + type = string +} + +variable "security_group" { + description = "Existing VyOS security group configuration." + type = object({ + name = optional(string) + description = string + tags = optional(map(string), {}) + }) +} + +variable "ingress_rules" { + description = "Existing VyOS ingress rules keyed by a stable logical name, one CIDR per resource." + type = map(object({ + description = optional(string) + ip_protocol = string + from_port = optional(number) + to_port = optional(number) + cidr_ipv4 = string + })) +} + +variable "instance" { + description = "Existing VyOS EC2 instance. user_data is intentionally absent." + type = object({ + name = optional(string) + ami = string + instance_type = string + private_ip = string + associate_public_ip_address = bool + source_dest_check = bool + ebs_optimized = bool + monitoring = bool + 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), {}) + }) +} diff --git a/modules/network_vyos/versions.tf b/modules/network_vyos/versions.tf new file mode 100644 index 0000000..3d89ea3 --- /dev/null +++ b/modules/network_vyos/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = "= 1.17.2" + + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 6.73" + } + } +}