Skip to content

Instantly share code, notes, and snippets.

@aarondodd
Created February 28, 2018 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarondodd/9ef20793cc6f397199d90e924b3c98c2 to your computer and use it in GitHub Desktop.
Save aarondodd/9ef20793cc6f397199d90e924b3c98c2 to your computer and use it in GitHub Desktop.
Query AWS EC2 instances based on launch time
#!/bin/bash
# Example how to query AWS for nodes that have been online older than a certain date
# Example below returns just the "Name" tag value (intent is for looping through for other actions)
# Example below also filters by "state=running" to excluded stopped or pending instances
# To get newer than a certain date, just alter ?LaunchTime<=${ec2_older_than_date} for the <= to be >=
query_older_than_minutes=10
# sed line conforms date output to AWS's datetime format
ec2_older_than_date=$(date --date='-10 minutes' --utc "+%FT%T.%N" | sed -r 's/[[:digit:]]{6}$/Z/')
# add backticks to variable for inclusion in AWS call
ec2_older_than_date="\`${ec2_older_than_date}\`"
aws_servers=$(aws ec2 describe-instances --filters "Name=tag:APPGROUP,Values=myfunapp" "Name=instance-state-name,Values=running" --query "Reservations[].Instances[?LaunchTime<=${ec2_older_than_date}].[Tags[?Key==\`Name\`].Value]" --output text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment