#!/usr/bin/env bash
ACCOUNT_ID=$(aws sts get-caller-identity --output text --query 'Account')
if [ -e "$ACCOUNT_ID" ]; then
echo -e "\e[1;31mYou are not logged to AWS cli!\e[0m"
exit 1
else
echo -e "\e[1;32mInventory being made for AWS user \e[1;4;35m#$ACCOUNT_ID\e[0m\e[1;32m (\e[39mif wrong please login to the correct account using AWS-CLI\e[32m)\e[0m\n"
fi
echo -e -n "\e[1;39mLooking for \e[1;4;35mProduction\e[0m\e[1;39m instances\e[0m "
for((i=0;i<4;i++)); do
echo -e -n "\e[1;35m.\e[0m"
sleep 0.5s
done
echo "[production]" > inventory
PROD_INSTANCES=$(aws ec2 describe-instances --filters "Name=tag:App,Values=Ted-Search" "Name=tag:Environment,Values=production" | sed -n 's/.*"PublicIpAddress": "\(.*\)",/\1/p')
echo -e -n " \e[1;32mDone!\e[0m"
sleep 1s
if [ -z "$PROD_INSTANCES" ]; then
echo -e "\n\n\e[1;39mNo \e[1;4;35mProduction\e[0m\e[1;39m instances were found!\e[0m"
else
echo -e "\n\n\e[1;39mFound:\e[0m"
echo -e "${PROD_INSTANCES}\n"
fi
echo "${PROD_INSTANCES}" >> inventory
##############
echo -e -n "\e[1;39mLooking for \e[1;4;35mTest\e[0m\e[1;39m instances\e[0m "
for((i=0;i<4;i++)); do
echo -e -n "\e[1;35m.\e[0m"
sleep 0.5s
done
echo "[test]" >> inventory
TEST_INSTANCES=$(aws ec2 describe-instances --filters "Name=tag:App,Values=Ted-Search" "Name=tag:Environment,Values=Test" | sed -n 's/.*"PublicIpAddress": "\(.*\)",/\1/p' | tr " " "\n")
echo -e -n " \e[1;32mDone!\e[0m"
sleep 1s
if [ -z "$TEST_INSTANCES" ]; then
echo -e "\n\n\e[1;39mNo \e[1;4;35mTest\e[0m\e[1;39m instances were found!\e[0m"
else
echo -e "\n\n\e[1;39mFound:\e[0m"
echo -e "${TEST_INSTANCES}\n"
fi
echo "${TEST_INSTANCES}" >> inventory