Created
February 25, 2025 16:45
-
-
Save jcefoli/43f331eb0ee61202161ab65c21234520 to your computer and use it in GitHub Desktop.
Bash/AWSCLI/JQ method to start an EC2 Instance and Wait for Ping Status Online (Instance ID can be an ADO variable or modified)
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 | |
INSTANCE_ID="$(InstanceID)" | |
REGION="us-east-2" | |
PING_STATUS="" | |
aws ec2 start-instances --instance-ids $INSTANCE_ID --region $REGION | |
while :; do | |
# Run the AWS CLI command and capture the response | |
FOO=$(aws ssm describe-instance-information \ | |
--instance-information-filter-list key=InstanceIds,valueSet=$INSTANCE_ID \ | |
--region $REGION) | |
# Extract the PingStatus using jq | |
PING_STATUS=$(echo "$FOO" | jq -r '.InstanceInformationList[0]?.PingStatus // empty') | |
# Check if the PingStatus is Online | |
if [ "$PING_STATUS" == "Online" ]; then | |
echo "Instance is online!" | |
break | |
else | |
echo "Waiting for SSM to come online. Re-checking..." | |
fi | |
# Pause before the next iteration | |
sleep 4 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment