
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.14.4"
name = "glen-test-vpc"
cidr = "10.10.0.0/16"
azs = ["ap-northeast-2a", "ap-northeast-2c"]
public_subnets = ["10.10.100.0/24", "10.10.101.0/24"]
private_subnets = ["10.10.110.0/24", "10.10.111.0/24"]
database_subnets = ["10.10.200.0/24", "10.10.201.0/24"]
enable_nat_gateway = true
}

resource "aws_vpc" "this" {
count = local.create_vpc ? 1 : 0
cidr_block = var.cidr
instance_tenancy = var.instance_tenancy
enable_dns_hostnames = var.enable_dns_hostnames
enable_dns_support = var.enable_dns_support
enable_classiclink = null # https://github.com/hashicorp/terraform/issues/31730
enable_classiclink_dns_support = null # https://github.com/hashicorp/terraform/issues/31730
assign_generated_ipv6_cidr_block = var.enable_ipv6
tags = merge(
{ "Name" = var.name },
var.tags,
var.vpc_tags,
)
}
resource "aws_eip" "nat" {
count = local.create_vpc && var.enable_nat_gateway && false == var.reuse_nat_ips ? local.nat_gateway_count : 0
vpc = true
tags = merge(
{
"Name" = format(
"${var.name}-%s",
element(var.azs, var.single_nat_gateway ? 0 : count.index),
)
},
var.tags,
var.nat_eip_tags,
)
}
resource "aws_route_table" "public" {
count = local.create_vpc && length(var.public_subnets) > 0 ? 1 : 0
vpc_id = local.vpc_id
tags = merge(
{ "Name" = "${var.name}-${var.public_subnet_suffix}" },
var.tags,
var.public_route_table_tags,
)
}
resource "aws_route_table" "private" {
count = local.create_vpc && local.max_subnet_length > 0 ? local.nat_gateway_count : 0
vpc_id = local.vpc_id
tags = merge(
{
"Name" = var.single_nat_gateway ? "${var.name}-${var.private_subnet_suffix}" : format(
"${var.name}-${var.private_subnet_suffix}-%s",
element(var.azs, count.index),
)
},
var.tags,
var.private_route_table_tags,
)
}


resource "aws_internet_gateway" "this" {
count = local.create_vpc && var.create_igw && length(var.public_subnets) > 0 ? 1 : 0
vpc_id = local.vpc_id
tags = merge(
{ "Name" = var.name },
var.tags,
var.igw_tags,
)
}
resource "aws_route_table_association" "private" {
count = local.create_vpc && length(var.private_subnets) > 0 ? length(var.private_subnets) : 0
subnet_id = element(aws_subnet.private[*].id, count.index)
route_table_id = element(
aws_route_table.private[*].id,
var.single_nat_gateway ? 0 : count.index,
)
}
resource "aws_route_table_association" "public" {
count = local.create_vpc && length(var.public_subnets) > 0 ? length(var.public_subnets) : 0
subnet_id = element(aws_subnet.public[*].id, count.index)
route_table_id = aws_route_table.public[0].id
}
resource "aws_db_subnet_group" "database" {
count = local.create_vpc && length(var.database_subnets) > 0 && var.create_database_subnet_group ? 1 : 0
name = lower(coalesce(var.database_subnet_group_name, var.name))
description = "Database subnet group for ${var.name}"
subnet_ids = aws_subnet.database[*].id
tags = merge(
{
"Name" = lower(coalesce(var.database_subnet_group_name, var.name))
},
var.tags,
var.database_subnet_group_tags,
)
}
resource "aws_nat_gateway" "this" {
count = local.create_vpc && var.enable_nat_gateway ? local.nat_gateway_count : 0
allocation_id = element(
local.nat_gateway_ips,
var.single_nat_gateway ? 0 : count.index,
)
subnet_id = element(
aws_subnet.public[*].id,
var.single_nat_gateway ? 0 : count.index,
)
tags = merge(
{
"Name" = format(
"${var.name}-%s",
element(var.azs, var.single_nat_gateway ? 0 : count.index),
)
},
var.tags,
var.nat_gateway_tags,
)
depends_on = [aws_internet_gateway.this]
}
resource "aws_route" "private_nat_gateway" {
count = local.create_vpc && var.enable_nat_gateway ? local.nat_gateway_count : 0
route_table_id = element(aws_route_table.private[*].id, count.index)
destination_cidr_block = var.nat_gateway_destination_cidr_block
nat_gateway_id = element(aws_nat_gateway.this[*].id, count.index)
timeouts {
create = "5m"
}
}
module "security-group" {
source = "terraform-aws-modules/security-group/aws"
version = "4.13.0"
name = "glen_bastion_sg"
description = "Security group for public bastion host"
vpc_id = module.vpc.vpc_id
ingress_with_cidr_blocks = [
{
from_port = 10022
to_port = 10022
protocol = "tcp"
description = "glen_bastion_ssh"
cidr_blocks = "0.0.0.0/0"
},
{
from_port = 22
to_port = 22
protocol = "tcp"
description = "default_ssh"
cidr_blocks = "0.0.0.0/0"
},
{
cidr_blocks = "0.0.0.0/0"
description = "ICMP"
from_port = -1
protocol = "icmp"
to_port = -1
}
]
}
resource "aws_security_group" "this_name_prefix" {
count = local.create && var.create_sg && var.use_name_prefix ? 1 : 0
name_prefix = "${var.name}-"
description = var.description
vpc_id = var.vpc_id
revoke_rules_on_delete = var.revoke_rules_on_delete
tags = merge(
{
"Name" = format("%s", var.name)
},
var.tags,
)
lifecycle {
create_before_destroy = true
}
timeouts {
create = var.create_timeout
delete = var.delete_timeout
}
}
resource "aws_security_group_rule" "ingress_with_cidr_blocks" {
count = local.create ? length(var.ingress_with_cidr_blocks) : 0
security_group_id = local.this_sg_id
type = "ingress"
cidr_blocks = split(
",",
lookup(
var.ingress_with_cidr_blocks[count.index],
"cidr_blocks",
join(",", var.ingress_cidr_blocks),
),
)
prefix_list_ids = var.ingress_prefix_list_ids
description = lookup(
var.ingress_with_cidr_blocks[count.index],
"description",
"Ingress Rule",
)
from_port = lookup(
var.ingress_with_cidr_blocks[count.index],
"from_port",
var.rules[lookup(var.ingress_with_cidr_blocks[count.index], "rule", "_")][0],
)
to_port = lookup(
var.ingress_with_cidr_blocks[count.index],
"to_port",
var.rules[lookup(var.ingress_with_cidr_blocks[count.index], "rule", "_")][1],
)
protocol = lookup(
var.ingress_with_cidr_blocks[count.index],
"protocol",
var.rules[lookup(var.ingress_with_cidr_blocks[count.index], "rule", "_")][2],
)
}
module "ec2_instance" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "4.1.4"
name = "glen_bastion-instance"
ami = "ami-0e4a9ad2eb120e054"
instance_type = "t2.micro"
monitoring = true
key_name = "glen_keypair_bastion"
vpc_security_group_ids = [
module.security-group.security_group_id
]
subnet_id = module.vpc.public_subnets[0]
tags = {
Name = "glen_bastion_host1"
}
}

다른 VPC와 통신을 하기 위한 설정
plan을 할때마다 change 발생함
accepter의 tag가 변경되지 않았는데 같은값이면 null 값이 들어가게됨

해결