Trying to get the route table ID in AWS cli using the tag name as a filter

Viewed 1595

How can I get the AWS route table ID using a tag name as the filter?

The tag name I want to look for is - eksctl-live-cluster/PublicRouteTable

In the below example, the end result is that I would want to get the command to return the id of "rtb-0b6d5359a281c6fd9"

Using the below command I can get all the info for all the route tables in my VPC. I have tried adding tags and names in the query part unsuccessfully and played around with --filter. I just want to get the ID for one table that uses the name "eksctl-live-cluster/PublicRouteTable".

aws ec2 describe-route-tables --filters "Name=vpc-id,Values=vpc-0a75516801dc9a130" --query "RouteTables[]"

The name I want to use in the search is - eksctl-live-cluster/PublicRouteTable

Here is the output of all the route tables when i use the first command -

[
    {
        "Associations": [
            {
                "AssociationState": {
                    "State": "associated"
                },
                "RouteTableAssociationId": "rtbassoc-07ef991c747ba58a5",
                "Main": true,
                "RouteTableId": "rtb-0ad0dde171cc946c9"
            }
        ],
        "RouteTableId": "rtb-0ad0dde171cc946c9",
        "VpcId": "vpc-0a75516801dc9a130",
        "PropagatingVgws": [],
        "Tags": [],
        "Routes": [
            {
                "GatewayId": "local",
                "DestinationCidrBlock": "10.170.0.0/16",
                "State": "active",
                "Origin": "CreateRouteTable"
            }
        ],
        "OwnerId": "000000000"
    },
    {
        "Associations": [
            {
                "SubnetId": "subnet-0e079eb96b85fc72c",
                "AssociationState": {
                    "State": "associated"
                },
                "RouteTableAssociationId": "rtbassoc-062f19d9175f4f596",
                "Main": false,
                "RouteTableId": "rtb-0b6d5359a281c6fd9"
            },
            {
                "SubnetId": "subnet-0b1fae931da8c9d8f",
                "AssociationState": {
                    "State": "associated"
                },
                "RouteTableAssociationId": "rtbassoc-0a22d395d0b6196ac",
                "Main": false,
                "RouteTableId": "rtb-0b6d5359a281c6fd9"
            }
        ],
        "RouteTableId": "rtb-0b6d5359a281c6fd9",
        "VpcId": "vpc-0a75516801dc9a130",
        "PropagatingVgws": [],
        "Tags": [
            {
                "Value": "live",
                "Key": "eksctl.cluster.k8s.io/v1alpha1/cluster-name"
            },
            {
                "Value": "live",
                "Key": "alpha.eksctl.io/cluster-name"
            },
            {
                "Value": "0.29.2",
                "Key": "alpha.eksctl.io/eksctl-version"
            },
            {
                "Value": "PublicRouteTable",
                "Key": "aws:cloudformation:logical-id"
            },
            {
                "Value": "eksctl-live-cluster",
                "Key": "aws:cloudformation:stack-name"
            },
            {
                "Value": "eksctl-live-cluster/PublicRouteTable",
                "Key": "Name"
            },
            {
                "Value": "arn:aws:cloudformation:us-east-1:000000000:stack/eksctl-live-cluster/ef543610-3981-11eb-abcc-0af655d000e7",
                "Key": "aws:cloudformation:stack-id"
            }
        ],
        "Routes": [
            {
                "GatewayId": "local",
                "DestinationCidrBlock": "10.170.0.0/16",
                "State": "active",
                "Origin": "CreateRouteTable"
            },
            {
                "GatewayId": "igw-072414b2b1d313970",
                "DestinationCidrBlock": "0.0.0.0/0",
                "State": "active",
                "Origin": "CreateRoute"
            }
        ],
        "OwnerId": "996762160"
    }
]
1 Answers

This should return what you're looking for:

aws ec2 describe-route-tables --filters 'Name=tag:Name,Values=eksctl-live-cluster/PublicRouteTable' --query 'RouteTables[].Associations[].RouteTableId'

In general you can filter with tags using the tag:<tag name> construct. I'm not sure what a / value will do.

tag :key- The key/value combination of a tag assigned to the resource. Use the tag key in the filter name and the tag value as the filter value. For example, to find all resources that have a tag with the key Owner and the value TeamA , specify tag:Owner for the filter name and TeamA for the filter value.

If you want to further filter it by VPC id, you can add on to the filter like this:

aws ec2 describe-route-tables --filters 'Name=tag:Name,Values=eksctl-live-cluster/PublicRouteTable' Name=vpc-id,Values=<VPC ID> --query 'RouteTables[].Associations[].RouteTableId'

This line:

 aws ec2 --profile prod --region eu-west-1 describe-route-tables --filters Name=tag:Name,Values=private-route-table-eu-west-1b --query 'RouteTables[].Associations[].RouteTableId'

Returned the following because my route table is associated with two subnets:

[ "rtb-04d4b860", "rtb-04d4b860" ]

If you need a unique output you could pipe this all through jq :

 |jq -r .[] |sort |uniq

References

Related