Last active
June 9, 2020 21:52
-
-
Save vallamost/5a9cddaba40a5135b01db8ed988cb7ff to your computer and use it in GitHub Desktop.
Grab value out of AMI Description from AWS EC2 describe AMI call via AWS CLI
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 | |
# For a list of EC2 AMI IDs in a file, find something in each of the AMI description attributes and capture the characters after it. | |
# Then output it in JSON. | |
# Replace 'SAMBA' with the string you want to search for. | |
# ami file of ids example: | |
# | |
# ami-a231212 | |
# ami-sf12518agsj2l31 | |
# ami-03230eaaaa85ed | |
# | |
filename='ami_ids_with_descriptions.txt' | |
region='us-west-2' | |
# || [ -n "$ami_id" ] is used so that we don't skip the last line of data if a new line is missing at the end of the file. | |
while read -r ami_id || [ -n "$ami_id" ]; do | |
#echo $ami_id | |
samba_ver=$(aws ec2 describe-image-attribute --image-id $ami_id --attribute description --region $region | awk '/SAMBA/ { match($0, /SAMBA/); print substr($0, RSTART - 0, RLENGTH + 20); }') | |
echo "{'ami_id': \"$ami_id\", \"samba_ver\": \"$samba_ver\"}," | |
sleep 2 | |
done < $filename | |
# Output Example | |
# ------- | |
# | |
# {'ami_id': "ami-54...2c", "samba_ver": "SAMBA:4.5.2-2"}, | |
# | |
# ------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment