Waiting for AWS Elastic Beanstalk environment to become ready

  |   Source

Elastic Beanstalk on AWS seems to be one of those services that are pretty cool but it's hard to get to know them. One of the tasks you may encounter while working with it is that after making some change to its configuration you would like to wait for it to be finished before proceeding further. The change may be setting an environment variable but can also be deploying a new version of the application. I created a small bash script to help with that, can be useful when you try to run this process unattended, e.g. from CI.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
set -e
set -o pipefail

application_name=$1
environment_name=$2
timeout_seconds=$3

function getStatus() {
echo `aws elasticbeanstalk describe-environments \
    --application-name $application_name --environment-name $environment_name |\
    jq -r '.Environments | .[]?' | jq -r '.Status'`
}

sleep_time_seconds=5
max_iterations_count=$(($timeout_seconds/$sleep_time_seconds))
iterations=0

echo "Waiting for a maximum of $timeout_seconds seconds for $environment_name to become ready"
status=$(getStatus)
while [[ ( $status != "Ready" ) && ( $iterations -lt $max_iterations_count ) ]]; do
    status=$(getStatus)
    echo $status
    sleep $sleep_time_seconds
    ((iterations+=1))
done

Happy coding !

Comments powered by Disqus