Answers: <p>You can achieve this by determining which subnet the ec2 instance belongs to, then check which route table the subnet is associated with.</p>
  
  <pre><code>#!/bin/bash
  
  instanceId='YOU-INSTANCE-ID'
  
  #finds the subnetId that the instance belongs to
  subnetId=$(aws ec2 describe-instances \
             --instance-id $instanceId \
             --query "Reservations[*].Instances[].SubnetId" \
             --output text)
  
  routingTableId=$(aws ec2 describe-route-tables \
                   --query "RouteTables[*].Associations[?SubnetId=='$subnetId'].RouteTableId" \
                   --output text)
  
  echo $routingTableId
  </code></pre>
  
Answers: CC BY-SA 3.0
Answers: 2018-02-07T22:11:12.580Z
Answers: 2018-02-07T22:11:12.580Z
Answers: Saad Zaamout
Answers: <p>The route table is associated with VPC, which is associated with the instance.</p>
  
  <p>Given <code>$id</code> shell variable has the instance ID, e.g.</p>
  
  <pre><code>id=i-0xyz # Replace i-0xyz with the real Instance ID.
  </code></pre>
  
  <p>here is the shell command to get VPCs associated with the instance and assign to <code>$vpcs</code> variable:</p>
  
  <pre><code>vpcs=$(aws ec2 describe-instances --instance-id $id --query 'Reservations[].Instances[].NetworkInterfaces[].VpcId' --output text)
  </code></pre>
  
  <p>Then to list the route tables associated with VPC, run:</p>
  
  <pre><code>aws ec2 describe-route-tables --filters "Name=vpc-id,Values=$vpcs"
  </code></pre>
  
  <p>To get just the route ids, add: <code>--query "RouteTables[].RouteTableId[]"</code> parameter.</p>
  
Answers: CC BY-SA 4.0
Answers: 2018-11-29T22:10:08.087Z
Answers: it's better to get the route table id from the perspective of the VPCID as done here if you're building something. You can't get the RTID from an EC2 if you are trying to launch one.
Answers: james6125
Answers: CC BY-SA 3.0
Answers: 2018-02-07T22:03:22.777Z
Answers: 2018-02-07T22:03:22.777Z
Answers: kenorb
Body: <p>I'd like to find a route table id associated with the given EC2 instance.</p>
  
  <p>How can this be achieved using AWS CLI?</p>
  
  <hr>
  
  <p>I've tried to use <code>aws ec2 describe-vpcs</code>, but the route tables are not there.</p>
  
  <p>I'm using <code>aws-cli/1.11.13</code>.</p>
  
Tags: <amazon-web-services><amazon-ec2><aws-cli>
Title: How to get instance's route table id using CLI?
UserDisplayName: kenorb
ThreadText: # How to get instance's route table id using CLI?
  
  (Asked by: username_0 on 2018-02-07)
  
  I'd like to find a route table id associated with the given EC2 instance.
  
  How can this be achieved using AWS CLI?
  
  ---
  
  I've tried to use `aws ec2 describe-vpcs`, but the route tables are not there.
  
  I'm using `aws-cli/1.11.13`.
  
  **Tags:** amazon-web-services, amazon-ec2, aws-cli
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-02-07. Score: 1 | Accepted answer
  
  You can achieve this by determining which subnet the ec2 instance belongs to, then check which route table the subnet is associated with.
  
  ```
  #!/bin/bash
  
  instanceId='YOU-INSTANCE-ID'
  
  #finds the subnetId that the instance belongs to
  subnetId=$(aws ec2 describe-instances \
             --instance-id $instanceId \
             --query "Reservations[*].Instances[].SubnetId" \
             --output text)
  
  routingTableId=$(aws ec2 describe-route-tables \
                   --query "RouteTables[*].Associations[?SubnetId=='$subnetId'].RouteTableId" \
                   --output text)
  
  echo $routingTableId
  
  ```
  
  
  ---
  
  ### Answer by username_0 on 2018-02-07. Score: 1
  
  The route table is associated with VPC, which is associated with the instance.
  
  Given `$id` shell variable has the instance ID, e.g.
  
  ```
  id=i-0xyz # Replace i-0xyz with the real Instance ID.
  
  ```
  
  here is the shell command to get VPCs associated with the instance and assign to `$vpcs` variable:
  
  ```
  vpcs=$(aws ec2 describe-instances --instance-id $id --query 'Reservations[].Instances[].NetworkInterfaces[].VpcId' --output text)
  
  ```
  
  Then to list the route tables associated with VPC, run:
  
  ```
  aws ec2 describe-route-tables --filters "Name=vpc-id,Values=$vpcs"
  
  ```
  
  To get just the route ids, add: `--query "RouteTables[].RouteTableId[]"` parameter.
  
  > **Comments:**
  > **username_2**: it's better to get the route table id from the perspective of the VPCID as done here if you're building something. You can't get the RTID from an EC2 if you are trying to launch one.  
  
  ---
  
  