Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Query AWS EC2 instances based on launch time
# 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 >=
# 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
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