EC2でインスタンス起動を待つスクリプト

vim check_instance_alive.sh
---
#/bin/sh

instanceId=$1

if [ "$instanceId" = "" ]; then
  echo "usage: check_instance_alive.sh {instance_id}"
  exit 1
fi

result=`ec2-describe-instance-status -I $instanceId`

if [ "$result" != "" ]; then
  echo "not ready"
  exit 1
fi

privateIp=`ec2-describe-instances $instanceId --filter instance-state-name=running | grep INSTANCE | cut -f18`

if [ "$privateIp" = "" ]; then
  echo "not ready"
  exit 1
fi

ping -c 1 $privateIp > /dev/null

if [ $? -ne 0 ]; then
  echo "not ready"
  exit 1
fi

echo "OK: $privateIp"
---
vim forever_check_alive.sh
---
#/bin/sh

instanceId=$1

if [ "$instanceId" = "" ]; then
  echo "usage: {this} {instance_id}"
  exit 1
fi

function countDown() {
  start=1
  end=30
  echo "please wait $end seconds"
  while [[ $start -le $end ]]; do
    echo $(($end-$start))
    sleep 1
    start=$(($start+1))
  done
}

check_instance_alive.sh $instanceId

result=$?

while [ $result -ne 0 ]; do
  countDown
  check_instance_alive.sh $instanceId
  result=$?
done
---

インスタンスにログインできる状態になるとPrivateIPが表示される。