Skip to content

Instantly share code, notes, and snippets.

@tuxpower
Created September 24, 2016 14:34
Show Gist options
  • Save tuxpower/873d2aaa1521013450cf29a4364e3d25 to your computer and use it in GitHub Desktop.
Save tuxpower/873d2aaa1521013450cf29a4364e3d25 to your computer and use it in GitHub Desktop.

When using --filters option always check which ones are supported e.g. 'aws ec2 describe-vpcs help' (dhcp-options-id, isDefault, state, vpc-id).

Otherwise use tag-key and tag-value for key/value combination of a tag assigned to the resource:

$ aws ec2 describe-vpcs --profile xxx-sandbox --filters Name=tag-key,Values=Name Name=tag-value,Values=myVPC

{
    "Vpcs": [
        {
            "VpcId": "vpc-55de7c32",
            "InstanceTenancy": "default",
            "Tags": [
                {
                    "Value": "myVPC",
                    "Key": "Name"
                }
            ],
            "State": "available",
            "DhcpOptionsId": "dopt-d94abcbc",
            "CidrBlock": "172.31.0.0/16",
            "IsDefault": false
        }
    ]
}

$ aws ec2 describe-vpcs --filters Name=tag-key,Values=Name Name=tag-value,Values=myVPC | jq -r .[]

[
  {
    "VpcId": "vpc-55de7c32",
    "InstanceTenancy": "default",
    "Tags": [
      {
        "Value": "myVPC",
        "Key": "Name"
      }
    ],
    "State": "available",
    "DhcpOptionsId": "dopt-d94abcbc",
    "CidrBlock": "172.31.0.0/16",
    "IsDefault": false
  }
]

$ aws ec2 describe-vpcs --filters Name=tag-key,Values=Name Name=tag-value,Values=myVPC | jq '.Vpcs'

[
  {
    "VpcId": "vpc-55de7c32",
    "InstanceTenancy": "default",
    "Tags": [
      {
        "Value": "myVPC",
        "Key": "Name"
      }
    ],
    "State": "available",
    "DhcpOptionsId": "dopt-d94abcbc",
    "CidrBlock": "172.31.0.0/16",
    "IsDefault": false
  }
]

$ aws ec2 describe-vpcs --filters Name=tag-key,Values=Name Name=tag-value,Values=myVPC |jq -r '.Vpcs[].VpcId'

vpc-55de7c32

$ aws ec2 describe-instances | jq -r '.Reservations[].Instances[] | select(.VpcId == "vpc-d72e8fb0") | {instance: .InstanceId}'

{
  "instance": "i-cf117753"
}
{
  "instance": "i-f217716e"
}
{
  "instance": "i-1000668c"
}
{
  "instance": "i-251375b9"
}
{
  "instance": "i-651274f9"
}
{
  "instance": "i-3f0365a3"
}
{
  "instance": "i-b69c032a"
}
{
  "instance": "i-25197fb9"
}
{
  "instance": "i-6b1771f7"
}
{
  "instance": "i-b7a22a2b"
}
{
  "instance": "i-a29eed3e"
}
{
  "instance": "i-8d1a7c11"
}
{
  "instance": "i-c71e785b"
}
{
  "instance": "i-d01e784c"
}
{
  "instance": "i-ea6c0a76"
}
{
  "instance": "i-be157322"
}

$ aws ec2 describe-instances | jq -r '.Reservations[].Instances[] | select(.VpcId == "vpc-d72e8fb0") | .InstanceId'

i-cf117753
i-f217716e
i-1000668c
i-251375b9
i-651274f9
i-3f0365a3
i-b69c032a
i-25197fb9
i-6b1771f7
i-b7a22a2b
i-a29eed3e
i-8d1a7c11
i-c71e785b
i-d01e784c
i-ea6c0a76
i-be157322
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment