Skip to content

Instantly share code, notes, and snippets.

@pjaudiomv
Created October 1, 2020 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pjaudiomv/ff5efca41cdb7244ef4416c6097f6a7a to your computer and use it in GitHub Desktop.
Save pjaudiomv/ff5efca41cdb7244ef4416c6097f6a7a to your computer and use it in GitHub Desktop.
Tags instances with detected platform and patch group
#!/bin/bash
INSTANCE_IDS=$(aws ssm describe-instance-information --query 'InstanceInformationList[?starts_with(InstanceId, `i-`) == `true` && starts_with(PingStatus, `Online`) == `true`].InstanceId' --output json)
# we don't wan't instances that start with m as they are not taggable, usually workspaces. We also will only tag instances that are online.
export INSTANCE_IDS
echo "Instances that are going to be updated: $INSTANCE_IDS"
echo "$INSTANCE_IDS" | jq -r '.[]' | while read INSTANCE; do
PLATFORM_NAME=$(aws ssm describe-instance-information --instance-information-filter-list key=InstanceIds,valueSet=$INSTANCE --output text --query 'InstanceInformationList[].PlatformName')
PLATFORM_VER=$(aws ssm describe-instance-information --instance-information-filter-list key=InstanceIds,valueSet=$INSTANCE --output text --query 'InstanceInformationList[].PlatformVersion')
if grep -qi 'amazon' <<< "$PLATFORM_NAME"; then
if [ "$PLATFORM_VER" == '2' ]; then
PATCH_GROUP="Amazon_2"
elif [ "$PLATFORM_VER" == '2018.03' ]; then
PATCH_GROUP="Amazon_1"
elif [ "$PLATFORM_VER" == '2017.09' ]; then
PATCH_GROUP="Amazon_1"
fi
elif grep -qi 'windows' <<< "$PLATFORM_NAME"; then
PATCH_GROUP="Windows"
elif grep -qi 'ubuntu' <<< "$PLATFORM_NAME"; then
PATCH_GROUP="Ubuntu"
elif grep -qi 'centos' <<< "$PLATFORM_NAME"; then
# SDElements server
PATCH_GROUP='CentOS'
elif grep -qi 'redhat' <<< "$PLATFORM_NAME"; then
PATCH_GROUP='RedHat'
elif grep -qi 'suse' <<< "$PLATFORM_NAME"; then
PATCH_GROUP='SuSe'
fi
echo "$INSTANCE: $PLATFORM_NAME $PLATFORM_VER :: $PATCH_GROUP"
aws ec2 create-tags --resources $INSTANCE --tags "Key=SSM-Detected-Platform,Value=$PLATFORM_NAME"
aws ec2 create-tags --resources $INSTANCE --tags "Key=Patch Group,Value=$PATCH_GROUP"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment