From 1338b27c85d9685b53419c4a7b9862ac685e1ea3 Mon Sep 17 00:00:00 2001 From: Michael Prokop Date: Tue, 14 Aug 2018 15:42:20 +0200 Subject: [PATCH] TT#41051 Ensure EC2 instance is running before assigning elastic IP AWS instances usually move from state 'stopped' to 'pending' and to 'running'. If we try to assign an elastic IP address to an instance that's still in state 'pending', it fails with: | Client.InvalidInstanceID: The pending instance 'i-0a6a707e93073a3a0' is not in a valid state for this operation. (Service: AmazonEC2; Status Code: 400; Error Code: InvalidInstanceID; Request ID: 0ac151fc-5d89-45c1-b2da-9b3bd677c7f2) Change-Id: I4c62da9c7ba5b2e65e0324abb2260c9f268b3865 --- ec2-create-ce | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/ec2-create-ce b/ec2-create-ce index 618dca8..d880595 100755 --- a/ec2-create-ce +++ b/ec2-create-ce @@ -411,23 +411,13 @@ ec2-create-tags --region "$AWS_REGION" "$INSTANCE_ID" --tag Purpose="$TAG_PURPOS # assign name (useful for web management console browsing) ec2-create-tags --region "$AWS_REGION" "$INSTANCE_ID" --tag Name="${AMI_NAME}/${DATE_STRING}/${JOB_NAME}/build_${BUILD_NUMBER}" -# assign elastic IP address to instance, -# enabling us to enable access to restricted -# webserver areas -if [ -n "$ALLOCATION_ID" ] ; then - echo "Associating elastic IP $ELASTIC_IP allocation ID ${ALLOCATION_ID} with instance ID $INSTANCE_ID" - ec2-associate-address --region "${AWS_REGION}" --allocation-id "${ALLOCATION_ID}" --instance "${INSTANCE_ID}" -elif [ -n "$ELASTIC_IP" ] ; then - echo "Associating elastic IP $ELASTIC_IP with instance ID $INSTANCE_ID" - ec2-associate-address --region "${AWS_REGION}" "${ELASTIC_IP}" -i "${INSTANCE_ID}" -fi - -STATUS="$(ec2-describe-instances --region ${AWS_REGION} --filter instance-id="$INSTANCE_ID" | awk '/INSTANCE/ {print $6}')" +STATUS="$(ec2-describe-instances --region ${AWS_REGION} --filter instance-id="$INSTANCE_ID" | awk '/INSTANCE/ {print $5}')" echo "Starting instance ID $INSTANCE_ID for ngcp version $NGCP_VERSION" retry=120 # up to 10 minutes while [ "$STATUS" != "running" ] && [ $retry -ne 0 ] ; do - STATUS="$(ec2-describe-instances --region ${AWS_REGION} --filter instance-id="$INSTANCE_ID" | awk '/INSTANCE/ {print $6}')" + # NOTE: it's '$5' here, becoming '$6' later iff elastic IP is attached, this ugly parsing will stay until we switch to e.g. awscli + STATUS="$(ec2-describe-instances --region ${AWS_REGION} --filter instance-id="$INSTANCE_ID" | awk '/INSTANCE/ {print $5}')" retry=$((retry - 1)) echo "Instance ID $INSTANCE_ID not running yet, checking again in 5 seconds ($retry retries left)." sleep 5 @@ -438,6 +428,17 @@ if [ "$STATUS" != "running" ] ; then bailout 1 fi +# assign elastic IP address to instance, +# enabling us to enable access to restricted +# webserver areas +if [ -n "$ALLOCATION_ID" ] ; then + echo "Associating elastic IP $ELASTIC_IP allocation ID ${ALLOCATION_ID} with instance ID $INSTANCE_ID" + ec2-associate-address --region "${AWS_REGION}" --allocation-id "${ALLOCATION_ID}" --instance "${INSTANCE_ID}" +elif [ -n "$ELASTIC_IP" ] ; then + echo "Associating elastic IP $ELASTIC_IP with instance ID $INSTANCE_ID" + ec2-associate-address --region "${AWS_REGION}" "${ELASTIC_IP}" -i "${INSTANCE_ID}" +fi + HOSTNAME="$(ec2-describe-instances --region ${AWS_REGION} --filter instance-id="$INSTANCE_ID" | awk '/INSTANCE/ {print $4}')" retry=360 # up to 30 minutes