Created
May 19, 2019 09:47
-
-
Save iKalin/4d5d3de55360e4bfc20dbe2469e507aa to your computer and use it in GitHub Desktop.
have 120 instances, which for this example translates to 235 volumes
This file contains 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 | |
volCheck() { | |
check="true" | |
count=1 | |
# iterate through volumes, apply ec2 tags via tag() | |
while [ "$check" = "true" ]; do | |
vol=$(echo $blockDevices | awk -F ' ' -v item=$count '{print $item}') | |
if [ -n "$vol" ]; then #if vol exists, then call tag function | |
echo "vol $vol exists" | |
tag $vol | |
((count++)) | |
else | |
check="false" | |
fi | |
done | |
} | |
# function that uses awscli to apply tags, is called form volCheck() | |
tag() { | |
aws ec2 create-tags --region $REGION --resources $1 --tags Key=Location,Value=$LOCATION Key=Environment,Value=$ENVIRONMENT Key=Service,Value=$SERVICE Key=Role,Value=$ROLE Key=SubService,Value=VOL Key=Name,Value=$NAME | |
} | |
# grab ec2 instance metadata | |
INSTANCEID=$(curl -s http://111.111.111.254/latest/meta-data/instance-id) | |
REGION=$(curl -s http://111.111.111.254/latest/dynamic/instance-identity/document | jq -r .region) | |
INSTDATA=$(aws ec2 describe-instances --region $REGION --filters "Name=instance-id,Values=$INSTANCEID") | |
# specify tags | |
TAGS=$(echo "$INSTDATA" | jq ".Reservations[0].Instances[0].Tags") | |
LOCATION=$(echo "$TAGS" | jq -r '.[] | select(.Key == "Location").Value') | |
ENVIRONMENT=$(echo "$TAGS" | jq -r '.[] | select(.Key == "Environment").Value') | |
SERVICE=$(echo "$TAGS" | jq -r '.[] | select(.Key == "Service").Value') | |
ROLE=$(echo "$TAGS" | jq -r '.[] | select(.Key == "Role").Value') | |
NAME=$(echo "$TAGS" | jq -r '.[] | select(.Key == "Name").Value') | |
# grab block devices from instance metadata | |
blockDevices=$(echo "$INSTDATA" | jq -r '.Reservations[0].Instances[0].BlockDeviceMappings[].Ebs.VolumeId') | |
# start tagging | |
volCheck | |
view raw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let’s say you were tasked with ensuring that EC2 instance volumes have proper tags. Tags are used for many reasons including billing. You have 120 instances, which for this example translates to 235 volumes. This could be done manually but that sounds awful and impractical. If we take a manual approach we will have to tag more volumes every time an instance is created. Lets examine an on machine solution. We are going to look at a bash example. The AWS cli is required for these scripts to work.
So now we have some tagging automation and it works well. We have partially solved the problem for Linux and a PowerShell implementation can be very similar. If we put this into configuration management then we have a reliable hands off process. The main problem with the above example is that we have a solution that is dependent on a specific operating system. Let’s explore some other examples that we can plug and play anywhere.