시나브로

Terraform 구성 본문

Cloud/Terraform

Terraform 구성

sa1347 2025. 11. 15. 23:14

AWS 시작한지 1년후부터 IaC에 관심이 있었는데

오늘에서야 Terraform 코드를 적용하게 되었다

로컬에 설치하고 아주 기본 리소스인 VPC, Subnet, IGW 등 VSCode에 plan 및 apply해봤다

VScode에 테라폼 설치하는것도 어떻게 보면 쉬운 일인데 난 어렵게만 생각해서 헤맸다

더보기
더보기

provider "aws" {
  region = "ap-northeast-2"
}

# VPC
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "main-vpc"
  }
}

# Subnet
resource "aws_subnet" "main" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.10.0/24"

  tags = {
    Name = "main-subnet"
  }
}

# IGW
resource "aws_internet_gateway" "main" {
  vpc_id = aws_vpc.main.id

  tags = {
    Name = "main-igw"
  }
}

# Route (예: 0.0.0.0/0 → IGW)
resource "aws_route" "main" {
  route_table_id         = aws_route_table.main.id  # 기존 Route Table
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.main.id  # 기존 IGW
}

# Route Table
resource "aws_route_table" "main" {
  vpc_id = aws_vpc.main.id

  tags = {
    Name = "main-route_table"
  }
}

# Route Table Association (Subnet에 Route Table 연결)
resource "aws_route_table_association" "main" {
  subnet_id      = aws_subnet.main.id    # 기존 Subnet
  route_table_id = aws_route_table.main.id
}

/*
resource "aws_route_table_association" "a" {
  subnet_id      = aws_subnet.foo.id
  route_table_id = aws_route_table.bar.id
}

resource "aws_route_table_association" "b" {
  gateway_id     = aws_internet_gateway.foo.id
  route_table_id = aws_route_table.bar.id
}
*/

# 보안그룹
resource "aws_security_group" "allow_TLS" {
  name        = "allow_TLS"
  description = "Allow TLS inbound traffic and all outbound traffic"
  vpc_id      = aws_vpc.main.id

  tags = {
    Name = "allow_TLS"
  }
}

resource "aws_vpc_security_group_ingress_rule" "allow_TLS_ipv4" {
  security_group_id = aws_security_group.allow_TLS.id
  cidr_ipv4         = aws_vpc.main.cidr_block
  from_port         = 443
  ip_protocol       = "tcp"
  to_port           = 443
}

resource "aws_vpc_security_group_egress_rule" "allow_all_traffic_ipv4" {
  security_group_id = aws_security_group.allow_TLS.id
  cidr_ipv4         = "0.0.0.0/0"
  ip_protocol       = "-1" # semantically equivalent to all ports
}

# SSH
resource "aws_security_group" "main" {
  name        = "ssh-security-group"
  description = "Allow SSH access"
  vpc_id      = "vpc-xxxxxxxx"  # 사용할 vpc id

  ingress {
    description      = "SSH from anywhere"
    from_port        = 22
    to_port          = 22
    protocol         = "tcp"
    cidr_blocks      = ["My IP"]   # 보안상 제한 가능
  }

  egress {
    from_port        = 0
    to_port          = 0
    protocol         = "-1"           # 모든 아웃바운드 허용
    cidr_blocks      = ["0.0.0.0/0"]
  }

  tags = {
    Name = "ssh-sg"
  }
}

 

생각보다 수월했다

네트워크만 해봤고, 이제 인스턴스 구성도 해봐야겠다

 

 

 

AWS 콘솔 확인

 

 

 

이 3가지가 좀 헷갈리다...

Route Table → 규칙 모음이에요. 어떤 네트워크 대역을 어디로 보내야 하는지 목록으로 가지고 있음.
Route → 그 규칙 하나 하나를 의미. 예: "10.0.1.0/22 → 인터넷 게이트웨이"
Route Table Association → 그 Route Table을 어떤 Subnet이나 Gateway에 적용할지 연결

💡 한 줄 요약:
Route = 규칙 하나, Route Table = 규칙 모음, Association = 적용 대상 연결

 

 

 

내 연구를 도와준 링크 !!

https://brunch.co.kr/@topasvga/5007
https://kukim.tistory.com/154
https://registry.terraform.io/providers/hashicorp/aws/latest/docs