From ed9a3000f9c84aa38c78223d99518d2ab74db9cc Mon Sep 17 00:00:00 2001 From: Michael Prokop Date: Sun, 30 Mar 2014 01:10:16 +0100 Subject: [PATCH] Block volume creation until the AMI is available Otherwise we might stop/terminate the instance ID before AMI creation finished. --- ec2-create-ce | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ec2-create-ce b/ec2-create-ce index 6d46b0d..6d3efb1 100755 --- a/ec2-create-ce +++ b/ec2-create-ce @@ -251,6 +251,20 @@ ec2-create-image --region "$AWS_REGION" \ AMI_ID="$(awk '/^IMAGE/ {print $2}' $LOGFILE_AMI)" +retry=120 # up to 10 minutes +STATUS="" +while [ "$STATUS" != "available" ] && [ $retry -ne 0 ] ; do + STATUS="$(ec2-describe-images --region ${AWS_REGION} "$AMI_ID" | awk '/^IMAGE/ {print $5}')" + echo "AMI ID $AMI_ID not yet finished (status: ${STATUS}), checking again in 5 seconds ($retry retries left)." + sleep 5 + retry=$(($retry - 1)) +done + +if [ "$STATUS" != "available" ] ; then + echo "No further retries left and couldn't finish creation of AMI ID ${AMI_ID} for instance ID ${INSTANCE_ID}, giving up." >&2 + exit 1 +fi + if [ -n "$AMI_ID" ] ; then echo "Successfully created AMI with ID $AMI_ID" else