Skip to content

Instantly share code, notes, and snippets.

@jcefoli
Created February 25, 2025 16:45
Show Gist options
  • Save jcefoli/43f331eb0ee61202161ab65c21234520 to your computer and use it in GitHub Desktop.
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)
#!/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