본문 바로가기
Terraform/AWS and 테라폼: Infrastructure as

[AWS and 테라폼: Infrastructure as Code] Route Table 구성하기

by 개발자 영만 2024. 1. 21.

Route Table 생성


  • Route Table은 트래픽을 규칙에 맞게 전달해주기 위해 필요한 일종의 테이블.
  • Route table은 여러 서브넷에서 동시에 사용할 수 있으며, 이렇게 연결하는 작업은 Association 이라고 한다.
  • Route Table은 aws_route_table 리소스를 생성하면 되고, 서브넷과 연결할 때는 aws_route_table_association 을 사용하면 된다.
resource "aws_route_table" "public" {
  vpc_id = aws_vpc.ihwoo-tftest-vpc.id

  tags = {
    Name = "ihwoo-tftest-public-rt",
    Date = "2022-10-05",
    User = "ihwoo"
  }
}

resource "aws_route_table_association" "route_table_association_public" {
  subnet_id = aws_subnet.ihwoo-tftest-public-subnet.id
  route_table_id = aws_route_table.public.id
}

resource "aws_route_table" "private" {
  vpc_id = aws_vpc.ihwoo-tftest-vpc.id

  tags = {
    Name = "ihwoo-tftest-private-rt",
    Date = "2022-10-05",
    User = "ihwoo"
  }
}

resource "aws_route_table_association" "route_table_association_private" {
  subnet_id = aws_subnet.ihwoo-tftest-private-subnet.id
  route_table_id = aws_route_table.private.id
}

 

  • 이제 Route Table의 Rule을 설정하면 된다.
  • aws_route 리소스를 사용하여 Rule을 설정한다.
resource "aws_route" "private_nat_1" {
  route_table_id = aws_route_table.private.id # 어떤 라우트 테이블에
  destination_cidr_block = "0.0.0.0/0" # 외부로 나갈 룰
  nat_gateway_id = aws_nat_gateway.ihwoo-tftest-natgw.id
}

 

  • aws_route_table 내부에서 route 를 사용해 Rule을 설정할 수도 있다.
resource "aws_route_table" "public" {
  vpc_id = aws_vpc.ihwoo-tftest-vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.ihwoo-tftest-igw.id
  }

  tags = {
    Name = "ihwoo-tftest-public-rt",
    Date = "2022-10-05",
    User = "ihwoo"
  }
}
  • inner rule 보다 aws_route 를 사용하는 게 확장성이 더 높다.

Terraform을 쓰는 이유


  • terraform plan과 apply 는 실제 리소스를 검사한다. → 실제 리소스가 삭제가 되면 terraform plan 시 추가 표시가 뜨게 된다.
  • 테라폼 코드로 만든 리소스에 한해서 누군가가 콘솔에서 수정을 하게 된다면 그 수정 사항을 plan을 통해 알 수가 있다.
  • 테라폼 사용 시 콘솔을 통해서 누가 수정을 하거나 잘못 수정을 하게 된다면 내가 원했던 형상 그대로 복구를 할 수 있다.