Created
February 28, 2018 16:40
-
-
Save aarondodd/9ef20793cc6f397199d90e924b3c98c2 to your computer and use it in GitHub Desktop.
Query AWS EC2 instances based on launch time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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