Route Table 생성

박도준·2021년 5월 16일
0

[IaC] Terraform

목록 보기
5/9
post-thumbnail

Route Table

Route Table은 트래픽을 규칙에 맞게 전달해주기 위한 규칙을 담고있는 테이블이다.

Route Table은 여러 서브넷에서 동시에 사용할 수 있다.



테라폼으로 Route Table 생성

  • vpc.tf
resource "aws_route_table" "public"{
 vpc_id = aws_vpc.main.id

 tags = {
   Name = "terraform-rt-public"
 }
}

resource "aws_route_table" "private"{
 vpc_id = aws_vpc.main.id

 tags = {
   Name = "terraform-rt-private"
 }
}

Route Table은 aws_route_table 리소스을 사용한다.

terraform plan

terraform apply



테라폼으로 Association 작업 수행

어떤 subnet을 어떤 Route Table에 연결할지 정하는 작업을 Association 이라고 한다.

  • vpc.tf
resource "aws_route_table_association" "route_table_association_public"{
 subnet_id = aws_subnet.public_subnet.id
 route_table_id = aws_route_table.public.id

}

resource "aws_route_table_association" "route_table_association_private"{
 subnet_id = aws_subnet.private_subnet.id
 route_table_id = aws_route_table.private.id

}

Association은 aws_route_table_association 리소스 사용한다. Association 할 subnet id와 Route Table id를 적어준다.

terraform plan

terraform apply

  • terraform-rt-public에 연결된 subnet
  • terraform-rt-private에 연결된 subnet



테라폼으로 Route Table rule 추가

Route Table의 rule을 추가할 때, 2가지 방법이 있다. 앞선 작성한 route table 테라폼 코드에 ingress로 작성하는 방법외부에 테라폼 코드로 작성하는 방법이 있다.

public은 ingress로, private는 외부에 작성해본다.

  • vpc.tf
resource "aws_route_table" "public"{
 vpc_id = aws_vpc.main.id
 
 route {
  cidr_block = "0.0.0.0/0"
  gateway_id = aws_internet_gateway.IGW.id
 } 

 tags = {
   Name = "terraform-rt-public"
 }
}


resource "aws_route" "private_nat" {
  route_table_id              = aws_route_table.private.id
  destination_cidr_block      = "0.0.0.0/0"
  nat_gateway_id              = aws_nat_gateway.NAT_gateway.id
}

terraform plan

terraform apply

위의 두가지 방법이 존재하지만 확장성을 생각하면 외부로 빼서 작성하는 것이 더 좋다.

  • terraform-rt-public의 라우팅 rule

  • terraform-rt-private의 라우팅 rule


profile
Better late than never

0개의 댓글