Skip to content

Instantly share code, notes, and snippets.

@Nicolas-Richard
Created March 21, 2023 17:54
Show Gist options
  • Select an option

  • Save Nicolas-Richard/7a37b6f0a7ef4c61b27042d4c2c4ada1 to your computer and use it in GitHub Desktop.

Select an option

Save Nicolas-Richard/7a37b6f0a7ef4c61b27042d4c2c4ada1 to your computer and use it in GitHub Desktop.
This script lists the Auto Scaling groups in an AWS account that have the "eks:cluster-name" tag matching the specified value, along with the number of instances in each group and the oldest launch time of those instances.
#!/bin/bash
# Filename: asg_view.sh
#
# Description: This script lists the Auto Scaling groups in an AWS account that have the "eks:cluster-name" tag
# matching the specified value, along with the number of instances in each group and the oldest
# launch time of those instances. It uses the AWS CLI to query the necessary information.
#
# Usage: ./asg_view.sh <cluster_name> <region> <profile>
#
# Parameters:
# <cluster_name> - The value of the "eks:cluster-name" tag to filter Auto Scaling groups by.
# <region> - The AWS region to run the AWS CLI commands in.
# <profile> - The AWS CLI profile to use for authentication.
cluster_name=$1
region=$2
profile=$3
export AWS_DEFAULT_REGION=$region
export AWS_PROFILE=$profile
# Get the list of Auto Scaling groups with the "eks:cluster-name" tag
asg_list=$(aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[?contains(Tags[?Key=='eks:cluster-name'].Value, '$cluster_name')].AutoScalingGroupName" --output text)
for asg_name in $asg_list; do
# Get the instance IDs of the instances in the Auto Scaling group
instance_ids=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names $asg_name --query 'AutoScalingGroups[].Instances[].InstanceId' --output text)
# Get the launch time of the instances and find the oldest launch time
if [ -n "$instance_ids" ]; then
oldest_launch_time=$(aws ec2 describe-instances --instance-ids $instance_ids --query 'Reservations[].Instances[].LaunchTime' --output text | sort | cut -d$'\t' -f 1)
else
oldest_launch_time=null
fi
# Highlight the oldest launch time if it's more than 2 days old
if [ "$oldest_launch_time" != "null" ]; then
launch_time_seconds=$(date -j -f '%Y-%m-%dT%H:%M:%S' "$oldest_launch_time" +%s 2>/dev/null) # redicect to /dev/null to silence warning `Warning: Ignoring 6 extraneous characters in date string (+00:00)`
now_seconds=$(date +%s)
if (( ($now_seconds - ${launch_time_seconds:-0}) > (2 * 24 * 60 * 60) )); then
oldest_launch_time=$(date -j -r "$launch_time_seconds" +"%Y-%m-%dT%H:%M:%S")
oldest_launch_time="⚠️ $oldest_launch_time"
fi
fi
# Get the number of instances in the Auto Scaling group
instance_ids__arr=($instance_ids)
instances_count=${#instance_ids__arr[@]}
# Print the ASG name, the number of instances, and the oldest launch time
echo "AutoScalingGroupName: $asg_name, OldestInstanceLaunchTime: $oldest_launch_time, InstancesCount: $instances_count"
done
unset AWS_DEFAULT_REGION
unset AWS_PROFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment