Skip to content

Instantly share code, notes, and snippets.

@carlessanagustin
Last active June 12, 2024 16:21
Show Gist options
  • Save carlessanagustin/7e18e272aafdd454e5b8091ae4f61099 to your computer and use it in GitHub Desktop.
Save carlessanagustin/7e18e272aafdd454e5b8091ae4f61099 to your computer and use it in GitHub Desktop.
Filtering AWS results

Via AWS CLI using jq

(https://github.com/mwilliamson/jq.py)

aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(.KeyName == "MyKey") | select(.State.Code != 48) | select(.Tags[]|select(.Key=="Name")|select(.Value=="InstanceName")) | [ .PublicIpAddress]'
 
aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(.KeyName == "MyKey") | select(.State.Code != 48) | select(.Tags[]|select(.Key=="Name")|select(.Value=="InstanceName")) | [ .PublicIpAddress, (.Tags[]|select(.Key=="Name").Value)]'

aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(.KeyName == "MyKey") | select(.State.Code != 48) | select(.Tags[]|select(.Key=="InventoryGroup").Value) | [ .PublicIpAddress, (.Tags[]|select(.Key=="Name").Value)]'

aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(.LaunchTime > "2015-01-28") | select(.State.Code != 48) | [.LaunchTime, .State.Name, (.Tags[]|select(.Key=="Name")|.Value)]'

aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(.KeyName == "MyKey") | { KeyName, PublicIpAddress}'

Via AWS CLI using filters

(http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html)

  • To describe all instances with the key-name type MyKey
aws ec2 describe-instances --output json --filters "Name=key-name,Values=MyKey"
  • To describe all instances with a Owner tag
aws ec2 describe-instances --filters "Name=tag-key,Values=Owner"
  • To describe all instances with a Purpose=test tag
aws ec2 describe-instances --filters "Name=tag:Purpose,Values=test"
  • To describe all EC2 instances that have an instance type of m1.small or m1.medium that are also in the us-west-2c Availability Zone
aws ec2 describe-instances --filters "Name=instance-type,Values=m1.small,m1.medium" "Name=availability-zone,Values=us-west-2c"
  • The following JSON input performs the same filtering.
aws ec2 describe-instances --filters file://filters.json

filters.json:

[
  {
    "Name": "instance-type",
    "Values": ["m1.small", "m1.medium"]
  },
  {
    "Name": "availability-zone",
    "Values": ["us-west-2c"]
  }
]

Via Ansible ec2.py using jq

(http://docs.ansible.com/ansible/intro_dynamic_inventory.html)

./ec2.py | jq '.tag_Environment_production'

./ec2.py | jq '.key_MyKey'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment