[IaC] Terraform Module๋กœ AWS Network ์ƒ์„ฑ

2025. 11. 11. 23:38ยทCloud/IaC

 

 

GitHub - 5a6io/OliveSafety: Cloud Wave 3๊ธฐ ํ”„๋กœ์ ํŠธ olivesafety

Cloud Wave 3๊ธฐ ํ”„๋กœ์ ํŠธ olivesafety. Contribute to 5a6io/OliveSafety development by creating an account on GitHub.

github.com

Cloud Wave์—์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ ์ฝ˜์†”๋กœ ์ž‘์—…ํ•ด Terraform ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ๋จผ์ € Network์™€ ๊ด€๋ จ๋œ ๋ชจ๋“ˆ์„ ์ƒ์„ฑํ•ด๋ณด๊ฒ ๋‹ค.

๐Ÿ“ŒNetwork ๋ชจ๋“ˆ ๊ตฌ์„ฑ์— ํ•„์š”ํ•œ ๋ฆฌ์†Œ์Šค

  • VPC: aws_vpc
  • Subnet: aws_subent
  • Internet Gateway&NAT: aws_internet_gateway, aws_route_table, aws_route, aws_eip, aws_nat_gateway, aws_route_table_association

โš™๏ธVPC ๋ชจ๋“ˆ

main.tf

resource "aws_vpc" "this" {
    cidr_block =  var.vpc_cidr #"10.0.1.0/24"
    enable_dns_support = true
    enable_dns_hostnames = false
    instance_tenancy = "default"
    tags = merge(var.common_tags, {
        Name = "${var.project_name}-vpc"
    })
}

outputs.tf

output "vpc_id" {
  value = aws_vpc.this.id
}

variables.tf

variable "project_name" {
  type = string
}

variable "common_tags" {
  type = map(string)
  default = {}
}

variable "vpc_cidr" {
  type = string
}

โš™๏ธSubnet ๋ชจ๋“ˆ

main.tf

resource "aws_subnet" "public" {
  for_each = { for idx, cidr in var.pub_sub_cidr : idx => { cidr = cidr, az = var.availability_zones[idx]}}
  vpc_id = var.vpc_id
  cidr_block = each.value.cidr
  availability_zone = each.value.az
  map_public_ip_on_launch = true
  tags = merge(var.common_tags, {
    Name = "${var.project_name}-pub-sub-${each.key + 1}"
  })
}

resource "aws_subnet" "private" {
  for_each = { for idx, cidr in var.pri_sub_cidr : idx => {cidr = cidr, az = var.availability_zones[idx]}}
  vpc_id = var.vpc_id
  cidr_block = each.value.cidr
  availability_zone = each.value.az
  tags = merge(var.common_tags, {
    Name = "${var.project_name}-pri-sub-${each.key + 1}"
  })
}

outputs.tf

output "public_subnets" {
  value = values(aws_subnet.public[*].id)
}

output "private_subnets" {
    value = values(aws_subnet.private[*].id)
}

vaiables.tf

variable "project_name" {
  type = string
  description = "ํ”„๋กœ์ ํŠธ ์ด๋ฆ„"
}

variable "common_tags" {
  type = map(string)
  default = {}
}

variable "vpc_id" {
  type = string
}

variable "availability_zones" {
  type = list(string)
}

variable "pub_sub_cidr" {
  type = list(string)
}

variable "pri_sub_cidr" {
  type = list(string)
}

โš™๏ธInternet Gateway์™€ NAT ๋ชจ๋“ˆ

main.tf

# Internet Gateway
resource "aws_internet_gateway" "this" {
    vpc_id = var.vpc_id
    tags = merge(var.common_tags, {
        Name = "${var.project_name}-igw"
    })
}

# Public Route Table + Internet Gateway
resource "aws_route_table" "rtb_pub" {
    vpc_id = var.vpc_id

    tags = merge(var.common_tags, {
        Name = "${var.project_name}-route-pub-${each.key + 1}"
    })
}

resource "aws_route" "rt_pub_route" {
    route_table_id = aws_route_table.rtb-pub-01
    destination_cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
}

resource "aws_route_table_association" "pub_association" {
    count = length(var.pub_sub_ids)
    route_table_id = aws_route_table.rtb_pub.id
    subnet_id = each.value.id[count.index]
}

# NAT
resource "aws_eip" "this" {
  count = var.multi_nat ? lenght(var.pub_sub_ids) : 1
  domain = "vpc"
  tags = merge(var.common_tags, {
    Name = "${var.project_name}-eip-nat-${count.index + 1}"
  })
}

resource "aws_nat_gateway" "this" {
    count = var.multi_nat ? length(var.pub_sub_ids) : 1
    subnet_id = var.pub_sub_ids[count.index]
    allocation_id = aws_eip.this[count.index].id
    tags = merge(var.common_tags, {
        Name = "${var.project_name}-nat-${count.index + 1}"
    })

    depends_on = [ aws_internet_gateway.this ]
}

# Private Route Table + NAT
resource "aws_route_table" "rtb_pri" {
    count = var.multi_nat ? length(var.pri_sub_ids) : 1
    vpc_id = var.vpc_id
    tags = merge(var.common_tags, {
        Name = "${var.project_name}-rtb-pri-${count.index + 1}"
    })
}

resource "aws_route" "rt_pri_rtb" {
  count = var.multi_nat ? length(var.pri_sub_ids) : 1
  route_table_id = aws_route_table.rtb_pri[count.index].id
  destination_cidr_block = "0.0.0.0/0"
  nat_gateway_id = aws_nat_gateway.this[var.multi_nat ? count.index : 0].id
}

resource "aws_route_table_association" "pri_association" {
  count = length(var.pri_sub_ids)
  route_table_id = aws_route_table.rtb_pri[var.multi_nat ? count.index : 0].id
  subnet_id = var.pri_sub_ids[count.index]
}

outputs.tf

output "private_route_table_ids" {
  value = aws_route_table.rtb_pri[*].id
}

output "public_route_table_ids" {
  value = aws_route_table.rtb_pub[*].id
}

vaiables.tf

variable "project_name" {
  type = string
}

variable "common_tags" {
  type = map(string)
  default = {}
}

variable "vpc_id" {
  type = string
}

variable "pub_sub_ids" {
  type = list(string)
}

variable "pri_sub_ids" {
  type = list(string)
}

variable "multi_nat" {
  type = bool
}

outputs.tf ํŒŒ์ผ์— ์žˆ๋Š” ๋‚ด์šฉ์€ ๋ชจ๋“ˆ์„ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  variables.tf ํŒŒ์ผ์— ์žˆ๋Š” ๋‚ด์šฉ์€ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ›์•„์˜ค๋ ค๊ณ  ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.


๐Ÿ“Œ๋ชจ๋“ˆ ์ƒ์„ฑ

์œ„์—์„œ ์ •์˜ํ•œ ๋ชจ๋“ˆ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

stacks/02_network.tf

module "vpc" {
  source = "../modules/network/vpc"
  project_name = var.project_name
  common_tags = var.common_tags
  vpc_cidr = var.vpc_cidr
}

module "subnets" {
  source = "../modules/network/subnets"
  project_name = var.project_name
  common_tags = var.common_tags

  vpc_id = module.vpc.vpc_id
  availability_zones = var.availability_zones
  pub_sub_cidr = var.pub_sub_cidr
  pri_sub_cidr = var.pri_sub_cidr
}

module "igw_nat" {
  source = "../modules/network/igw-nat"
  project_name = var.project_name
  common_tags = var.common_tags

  vpc_id = module.vpc.vpc_id
  pub_sub_ids = module.subnets.public_subnets
  pri_sub_ids = module.subnets.private_subnets
  multi_nat = var.multi_nat
}

๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ variables.tf๋กœ ์™ธ๋ถ€์—์„œ ๋ฐ›์•„์˜ฌ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  terraform applyํ•  ๋•Œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค.


๋‹ค์Œ์€ IAM๊ณผ Security Group๊ณผ ๊ด€๋ จํ•ด ์ž‘์„ฑํ•ด๋ณด๊ฒ ๋‹ค.

์ž‘์„ฑํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜ ๊นƒํ—ˆ๋ธŒ์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

GitHub - 5a6io/Cloud-Wave-Project-Terraform: Cloud Wave 3๊ธฐ ํ”„๋กœ์ ํŠธ Terraform ์ฝ”๋“œ

Cloud Wave 3๊ธฐ ํ”„๋กœ์ ํŠธ Terraform ์ฝ”๋“œ. Contribute to 5a6io/Cloud-Wave-Project-Terraform development by creating an account on GitHub.

github.com

 

์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋ณ€๊ฒฝ๊ธˆ์ง€ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'Cloud > IaC' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[IaC] Terraform Module๋กœ AWS IAM๊ณผ Security Group ์ƒ์„ฑ  (0) 2025.11.12
[IaC] Terraform module ์ƒ์„ฑ  (1) 2025.07.18
[IaC] Terraform์œผ๋กœ AWS ์ธํ”„๋ผ ๊ด€๋ฆฌ ๋ฐ ์ž๋™ํ™”  (0) 2025.06.30
[IaC] ํ…Œ๋ผํผ(Terraform)์ด๋ž€?  (0) 2025.06.28
'Cloud/IaC' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [IaC] Terraform Module๋กœ AWS IAM๊ณผ Security Group ์ƒ์„ฑ
  • [IaC] Terraform module ์ƒ์„ฑ
  • [IaC] Terraform์œผ๋กœ AWS ์ธํ”„๋ผ ๊ด€๋ฆฌ ๋ฐ ์ž๋™ํ™”
  • [IaC] ํ…Œ๋ผํผ(Terraform)์ด๋ž€?
The Engineer, Lucy
The Engineer, Lucy
  • The Engineer, Lucy
    Growing up for My Future๐Ÿ’•
    The Engineer, Lucy
    • Instagram
    • GitHub
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (190)
      • Linux (26)
      • Infra (9)
      • Cloud (30)
        • AWS (3)
        • GCP (4)
        • Docker (4)
        • Kubernetes (14)
        • IaC (5)
      • NGINX (1)
      • DevOps (3)
      • Computer Science (17)
        • Data Structure (0)
        • Algorithms (1)
        • Operating System (3)
        • Network (11)
        • Database System (2)
      • Coding Test (97)
        • Algorithms (89)
        • SQL (7)
      • ETC (7)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
  • ๊ณต์ง€์‚ฌํ•ญ

  • ๋งํฌ

    • Lucy's Instagram
    • Lucy's GitHub
  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ๋ฆฌ๋ˆ…์Šค๋งˆ์Šคํ„ฐ 2๊ธ‰
    ์˜ค๋ธ”์™„
    ๋„คํŠธ์›Œํฌ ๊ธฐ์ดˆ ์ง€์‹
    ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ
    bfs
    ์‰˜ ์Šคํฌ๋ฆฝํŠธ
    programmers
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๊ณต๋ถ€
    ๋ฆฌ๋ˆ…์Šค
    ๋„คํŠธ์›Œํฌ
    cs ๊ธฐ์ดˆ ์ง€์‹ ์ •๋ฆฌ
    ๋„ˆ๋น„์šฐ์„ ํƒ์ƒ‰
    ๋ฐฑ์ค€
    ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค
    Baekjoon
    ์…ธ ์Šคํฌ๋ฆฝํŠธ
    terraform
    ํ‹ฐ์Šคํ† ๋ฆฌ์ฑŒ๋ฆฐ์ง€
    AWS
    Java
    ์ž๋ฐ”
    docker
    network
    ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
    ๋„์ปค
    Kubernetes
    Shell Script
    Shell
    K8s
    Linux
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
The Engineer, Lucy
[IaC] Terraform Module๋กœ AWS Network ์ƒ์„ฑ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”