[Terraform]테라폼 네트워크 자동화 (Terraform Network Automation)

테라폼 네트워크 자동화 (Terraform Network Automation)

이 문서는 AWS 인프라 내 네트워크 구조를 Terraform으로 자동화하는 실습 가이드입니다.
VPC, Subnet, IGW, NAT Gateway 등 주요 네트워크 리소스를 모듈화하여 반복 가능한 코드로 작성합니다.
디렉토리 구성부터 리소스 생성까지 체계적으로 이해할 수 있도록 구성되어 있습니다.

Terraform Step-by-Step: AWS 네트워크 구조 자동화 구축 + 디렉토리 가이드

🎯 목표

엑셀 테이블 기반으로 아래 리소스를 Terraform으로 반복적이고 효율적으로 생성:

  • VPC
  • Subnet (Public, Private, RDS)
  • Route Table & Association
  • Internet Gateway
  • NAT Gateway
  • EC2 & Security Group (예정)

📁 디렉토리 구성 가이드

terraform/
├── main.tf                  # 모듈 호출 및 리소스 배치 (최상위 실행 파일)
├── provider.tf              # AWS Provider 설정
├── variables.tf             # 변수 정의
├── terraform.tfvars         # 민감정보 포함된 변수 값
├── modules/                 # 실제 리소스 정의
│   ├── vpc/
│   │   └── main.tf          # VPC 리소스 정의
│   ├── subnet/
│   │   └── main.tf          # Subnet 리소스 정의
│   ├── route_table/
│   │   └── main.tf          # Route Table & Association
│   ├── igw/
│   │   └── main.tf          # Internet Gateway
│   ├── nat_gateway/
│   │   └── main.tf          # NAT Gateway + EIP
│   └── ec2/
│       └── main.tf          # EC2, SG 정의 예정


✅ Step 1. 작업 디렉토리 생성

mkdir -p ~/terraform/pmpubp01dp01/modules/{vpc,subnet,route_table,igw,nat_gateway,ec2}
cd ~/terraform/pmpubp01dp01
code .


✅ Step 2. VPC 모듈 (modules/vpc/main.tf)

resource "aws_vpc" "main" {
  cidr_block           = "10.254.0.0/16"
  enable_dns_support   = true
  enable_dns_hostnames = true</p>
<p>tags = {
    Name        = "vpc-pmpubp01dp01"
    Environment = "dev"
  }
}


✅ Step 3. Subnet 모듈 (modules/subnet/main.tf)

variable "subnets" {
  type = map(object({
    cidr = string
    az   = string
    tier = string
  }))
}</p>
<p>resource "aws_subnet" "this" {
  for_each = var.subnets</p>
<p>vpc_id                  = aws_vpc.main.id
  cidr_block              = each.value.cidr
  availability_zone       = each.value.az
  map_public_ip_on_launch = each.value.tier == "pub" ? true : false</p>
<p>tags = {
    Name        = "snet-${each.key}"
    Tier        = each.value.tier
    Environment = "dev"
  }
}


✅ Step 4. Route Table 모듈 (modules/route_table/main.tf)

resource "aws_route_table" "pub" {
  vpc_id = aws_vpc.main.id</p>
<p>route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
  }</p>
<p>tags = {
    Name = "rt-pmpubp01dp01-pub"
  }
}</p>
<p>resource "aws_route_table_association" "pub" {
  subnet_id      = aws_subnet.this["pmpubp01dp01-pub-az-a"].id
  route_table_id = aws_route_table.pub.id
}


✅ Step 5. IGW 모듈 (modules/igw/main.tf)

resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.main.id</p>
<p>tags = {
    Name = "igw-pmpubp01dp01"
  }
}


✅ Step 6. NAT Gateway 모듈 (modules/nat_gateway/main.tf)

resource "aws_eip" "nat" {
  vpc = true
}</p>
<p>resource "aws_nat_gateway" "nat" {
  allocation_id = aws_eip.nat.id
  subnet_id     = aws_subnet.this["pmpubp01dp01-pub-az-c"].id</p>
<p>tags = {
    Name = "ngw-pmpubp01dp01"
  }</p>
<p>depends_on = [aws_internet_gateway.igw]
}


✅ Step 7. 다음 스텝

  • EC2 및 보안 그룹 → modules/ec2/main.tf에서 정의 예정
  • 모든 모듈을 main.tf에서 호출하여 구성

🔐 보안 및 관리 포인트

항목 설명
민감 정보 terraform.tfvars는 Git에 커밋 금지
모듈 분리 각 리소스를 역할별 디렉토리로 관리
상태 백엔드 S3 + DynamoDB 사용 권장
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x