Dipendra Dangal
Dipendra Dangal

Reputation: 1183

Wait and Loop condition in Bash Script

I have an AWS CLI script that will take AMI of instance, create a launch configuration, update the autoscaling group with latest launch config and perform instance refresh operation.

I don't want to perform instance refresh operation unless the AMI is in "available state". So, I am thinking of adding a condition that checks every 10 seconds.

Here is my exisiting script file:

...
#Create AMI
IMAGE=`aws ec2 create-image --instance-id ${INST_ID} --name NEW-IMAGE-${TIME} --no-reboot --output text`
echo "Create Image of instance ${INST_ID}"

#Create new launch Configuration
aws autoscaling create-launch-configuration --launch-configuration-name ${NEW_LC} --image-id ${IMAGE} --instance-type t2.micro --key forclient --associate-public-ip-address --security-groups sg-01be135cb14a00960
echo "Create new Launch Configuration ${NEW_LC}"

#Update Auto Scaling Group to use new Launch Configuration
aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${ASG_NAME} --launch-configuration-name ${NEW_LC}
echo "New Launch Configuration is updated in ASG ${NEW_LC}"

aws autoscaling start-instance-refresh --auto-scaling-group-name ${ASG_NAME}

I don't want to run the 'start-instance-refresh' command until the 'create-image' is in 'available' state.

What changes do I need to make on this script file for this to happen?

Upvotes: 2

Views: 692

Answers (1)

Marcin
Marcin

Reputation: 238249

You can use image-available waiter after you create the image:

aws ec2 wait image-available --image-ids ${IMAGE}

Upvotes: 2

Related Questions