0

I deal with asymmetric clusters of EC2 nodes. There are certain operations which can only be safely carried out when none of the EBS volumes in the cluster are in an "optimizing" state (e.g. after a resize). Checking this manually via the console is time-consuming and error prone - so I tried to script this.

Getting a list of the hostname/instance id and attached volumes is easy enough...

aws --profile "$PROFILE" ec2 describe-instances --region "$REGION" --filters \ "Name=tag:Name,Values=${PREFIX}*" \ --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value,InstanceId,BlockDeviceMappings[*].Ebs.VolumeId]' 

However it appears that I need to make a separate aws cli call, describe-volumes-modifications, to find out if the volume is optimizing.

That should not be a problem, however:

  1. If any of the volume ids passed as arguments is not currently optimizing (or possibly has not been modified since creation?) aws cli returns an error and no data

  2. There does not appear to be a list of exit codes applicable in this scenario, on testing the same error code is returned in different error scenarios

  3. the text of the error does not appear to be readily machine-parsable:

"An error occurred (InvalidVolumeModification.NotFound) when calling the DescribeVolumesModifications operation: Modification for volume 'vol-01abcd3e7f8612345' does not exist. 

(and yes, the volume exists)

It does not help that the error is sent to stderr even when I ask for JSON output (and I don't get any json either).

I can pre-empt the first issue by checking a single volume at time, but I don't know how to script around the latter 2, differentiating between no current optimizing state vs something else went wrong.

Asking Google I just get a sea of pages describing how to "optimize" my use of AWS infrastructure.

1 Answer 1

0

I came up with this - but it's a very ugly solution

function get_ebs_opt() { local hostname volids destdir hostname="$1" volumes="$2" destdir="$3" local state rstate result for v in $volumes ; do state="$( aws --profile "$PROFILE" ec2 describe-volumes-modifications --region "$REGION" --volume-ids "$v" 2>&1 | tr -d '\n' )" result=$? if [[ "$state" =~ Modification[[:space:]]for[[:space:]]volume.+does[[:space:]]not[[:space:]]exist ]]; then rstate="OK (no change)" elif [ 0 -ne "$result" ]; then rstate="ERROR (aws cli returned $result)\t$state" elif [[ "$state" =~ is[[:space:]]invalid ]] ; then rstate="WARNING (volume not found)" elif [[ "$state" =~ modifying ]] ; then rstate="OPTIMIZING" elif [[ "$state" =~ completed ]] ; then rstate="OK (completed optimization)" else rstate="ERROR(unknown)" fi echo -e "${hostname}\t${volid}\t${rstate}" >>"${destdir}/${hostname}" done } 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.