Add group 3 network imports

This commit is contained in:
2026-11-14 18:13:08 +09:00
parent 5a223937cf
commit 4433535637
11 changed files with 964 additions and 0 deletions
+93
View File
@@ -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]
}
}
+28
View File
@@ -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
}
+73
View File
@@ -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")
}
+180
View File
@@ -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
}
+52
View File
@@ -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"]
}
}
+162
View File
@@ -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), {})
})
})
}
+10
View File
@@ -0,0 +1,10 @@
terraform {
required_version = "= 1.17.2"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.73"
}
}
}