Find the Main Route Table of a VPC with PowerShell

In Amazon Web Services (AWS) Virtual Private Cloud (VPC), each VPC has a default route table, known as the “main” route table. If you want to identify the route table that is considered the “main” route table, it can be a little challenging.

Using the Get-EC2RouteTable command, you might notice that the route table objects don’t have a property denoting each of the route tables as “main” or not. It would be nice if each route table object exposed a boolean property called Main, but unfortunately that is not how the API works.

The option to find the “main” route table is buried under the -Filter parameter on the Get-EC2RouteTable command. You have to create an instance of the Amazon.EC2.Model.Filter object, with a specified filter name and value. The filter object enables you to specify a variety of options, including whether or not the route table is the “main” route table. For a full list of filter options, check out the documentation below, or use Get-Help -Name Get-EC2RouteTable from PowerShell.

Documentation for Get-EC2RouteTable

Here is a simple PowerShell wrapper function that should help you retrieve the “main” route table, for a given VPC ID.

function Get-EC2MainRouteTable {
  [CmdletBinding()]
  [OutputType([Amazon.EC2.Model.RouteTable])]
  param (
    [Parameter(Mandatory = $true)]
    [string] $VpcId
  )

  $FilterList = @(
    [Amazon.EC2.Model.Filter]::new('association.main', 'true')
    [Amazon.EC2.Model.Filter]::new('vpc-id', $VpcId)
  )
  Get-EC2RouteTable -Filter $FilterList
}

# Example invocation of the function
Get-EC2MainRouteTable -VpcId <yourVpcId>