Route Tableの作成


Route Table


Route Tableは、ルールに従ってトラフィックを伝達するためのルールを含むテーブルです.
Route Tableは複数のサブネットで同時に使用できます.

Teraformを使用して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


    Teraformを使用した関連操作


    どのサブネットをどのルーティング・テーブルに接続するかを指定するアクションを関連付けと呼びます.
  • 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
    
    }
    関連付けは、aws_route_table_associationリソースを使用します.関連付けるサブネットIDとRoute Table IDを書きます.
    terraform plan
    terraform apply
  • terraform-rt-共通のサブネットに接続

  • terraform-rt-privateのサブネットに接続

  • Route Table RuleをTeraformに追加


    Route Tableのruleを追加するには、2つの方法があります.前に記述したroute table teraformコードでingressと外部でteraformコードを用いて記述する方法がある.
    共通は入力、プライベートは外部作成です.
  • 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
    上記の2つの方法があるが、拡張性を考慮して、それを除外して書くことが望ましい.

  • Terraform-RT-publicのルーティング規則


  • Terraform-RT-Privateルーティング規則