ss321c
ss321c

Reputation: 799

how to use filter in ec2 output on cli

Hello here is output of a command aws ec2 describe-vpcs --output table


|                      DescribeVpcs                       |
+---------------------------------------------------------+
||                         Vpcs                          ||
|+-----------------------+-------------------------------+|
||  CidrBlock            |  10.240.0.0/23                ||
||  DhcpOptionsId        |  dopt-b80a0ed0                ||
||  InstanceTenancy      |  default                      ||
||  IsDefault            |  False                        ||
||  State                |  available                    ||
||  VpcId                |  vpc-0a0a0699b064d3382        ||
|+-----------------------+-------------------------------+|
|||               CidrBlockAssociationSet               |||
||+----------------+------------------------------------+||
|||  AssociationId |  vpc-cidr-assoc-02f83d25b08ec323a  |||
|||  CidrBlock     |  10.240.0.0/23                     |||
||+----------------+------------------------------------+||
||||                  CidrBlockState                   ||||
|||+-------------------+-------------------------------+|||
||||  State            |  associated                   ||||
|||+-------------------+-------------------------------+|||
|||                        Tags                         |||
||+-------------------+---------------------------------+||
|||        Key        |              Value              |||
||+-------------------+---------------------------------+||
|||  Owner            |  dcnathlotasina                 |||
|||  Environment      |  Test                           |||
|||  Name             |  dcnathlotasina-vpc             |||
|||  Department       |  CSE                            |||
||+-------------------+---------------------------------+||
||                         Vpcs                          ||
|+----------------------------+--------------------------+|
||  CidrBlock                 |  172.31.0.0/16           ||
||  DhcpOptionsId             |  dopt-b80a0ed0           ||
||  InstanceTenancy           |  default                 ||
||  IsDefault                 |  True                    ||
||  State                     |  available               ||
||  VpcId                     |  vpc-08356c60            ||
|+----------------------------+--------------------------+|
|||               CidrBlockAssociationSet               |||
||+-------------------+---------------------------------+||
|||  AssociationId    |  vpc-cidr-assoc-b9413cd1        |||
|||  CidrBlock        |  172.31.0.0/16                  |||
||+-------------------+---------------------------------+||
||||                  CidrBlockState                   ||||
|||+-------------------+-------------------------------+|||
||||  State            |  associated                   ||||
|||+-------------------+-------------------------------+|||
||+-------------------+-------------------------------+|||
||                         Vpcs                          ||
|+-----------------------+-------------------------------+|
||  CidrBlock            |  10.240.0.0/23                ||
||  DhcpOptionsId        |  dopt-b80a0ed0                ||
||  InstanceTenancy      |  default                      ||
||  IsDefault            |  False                        ||
||  State                |  available                    ||
||  VpcId                |  vpc-090b6525d5e4166bd        ||
|+-----------------------+-------------------------------+|
|||               CidrBlockAssociationSet               |||
||+----------------+------------------------------------+||
|||  AssociationId |  vpc-cidr-assoc-0c6a823413b421d91  |||
|||  CidrBlock     |  10.240.0.0/23                     |||
||+----------------+------------------------------------+||
||||                  CidrBlockState                   ||||
|||+-------------------+-------------------------------+|||
||||  State            |  associated                   ||||
|||+-------------------+-------------------------------+|||
|||                        Tags                         |||
||+-------------------+---------------------------------+||
|||        Key        |              Value              |||
||+-------------------+---------------------------------+||
|||  Environment      |  Test                           |||
|||  Name             |  dcnathlotasina-vpc             |||
|||  Department       |  CSE                            |||
|||  Owner            |  dcnathlotasina                 |||
||+-------------------+---------------------------------+||
||                         Vpcs                          ||
|+-----------------------+-------------------------------+|
||  CidrBlock            |  10.240.0.0/23                ||
||  DhcpOptionsId        |  dopt-b80a0ed0                ||
||  InstanceTenancy      |  default                      ||
||  IsDefault            |  False                        ||
||  State                |  available                    ||
||  VpcId                |  vpc-06eedbb5dc8c8e20b        ||
|+-----------------------+-------------------------------+|
|||               CidrBlockAssociationSet               |||
||+----------------+------------------------------------+||
|||  AssociationId |  vpc-cidr-assoc-017526b49c268505f  |||
|||  CidrBlock     |  10.240.0.0/23                     |||
||+----------------+------------------------------------+||
||||                  CidrBlockState                   ||||
|||+-------------------+-------------------------------+|||
||||  State            |  associated                   ||||
|||+-------------------+-------------------------------+|||
|||                        Tags                         |||
||+-------------------+---------------------------------+||
|||        Key        |              Value              |||
  Owner               |  dcnathlotasina                 |||
|||  Department       |  CSE                            |||
|||  Environment      |  Test                           |||
|||  Name             |  dcnathlotasina-vpc             |||
||+-------------------+---------------------------------+||
||                         Vpcs                          ||
|+-----------------------+-------------------------------+|
||  CidrBlock            |  10.240.0.0/23                ||
||  DhcpOptionsId        |  dopt-b80a0ed0                ||
||  InstanceTenancy      |  default                      ||
||  IsDefault            |  False                        ||
||  State                |  available                    ||
||  VpcId                |  vpc-0cf43d41666c21f82        ||
|+-----------------------+-------------------------------+|
|||               CidrBlockAssociationSet               |||
||+----------------+------------------------------------+||
|||  AssociationId |  vpc-cidr-assoc-0b79b80c8397844f7  |||
|||  CidrBlock     |  10.240.0.0/23                     |||
||+----------------+------------------------------------+||
||||                  CidrBlockState                   ||||
|||+-------------------+-------------------------------+|||
||||  State            |  associated                   ||||
|||+-------------------+-------------------------------+|||
|||                        Tags                         |||
||+-------------------+---------------------------------+||
|||        Key        |              Value              |||
||+-------------------+---------------------------------+||
|||  Department       |  CSE                            |||
|||  Name             |                   -vpc          |||
|||  Environment      |  Test                           |||
|||  Owner            |                                 |||
||+-------------------+---------------------------------+||

The above are details of command line output generated for which I want to use a filter but I am not clear with the usage of filter. Let us say I just want to filter out VpcId from above output then how should I use --filter tag from aws command line?

Upvotes: 2

Views: 1335

Answers (1)

Alex Harvey
Alex Harvey

Reputation: 15502

Let us say I just want to filter out VpcId from above output then how should I use --filter tag from aws command line?

I am not sure if you mean you want to filter this out, as in exclude, or filter it in, as in include only this VPC, or if you mean you just want the VpcIds displayed in the response.

Filters are fairly limited and all you can do is filter-in by that VpcId:

aws ec2 describe-vpcs --filters 'VpcId=vpc-0a0a0699b064d3382'

Or if you wanted to filter in by a tag on the VPCs, you can something like this (include only VPCs with the Owner tag dcnathlotasina):

aws ec2 describe-vpcs --filters 'Name=tag:Owner,Values=dcnathlotasina'

If you wanted to exclude that VPC, you can do it with a query:

aws ec2 describe-vpcs --query 'Vpcs[?VpcId!=`vpc-0a0a0699b064d3382`]'

If you mean you want to include only this VPC with a query, you can do this:

aws ec2 describe-vpcs --query 'Vpcs[?VpcId==`vpc-0a0a0699b064d3382`]'

And if you want to only include the VpcId field, but for all VPCs, you can do this:

aws ec2 describe-vpcs --query 'Vpcs[*].VpcId'

Upvotes: 2

Related Questions