Search code examples
amazon-web-servicesamazon-ec2aws-cli

aws command line interface - aws ec2 wait - Max attempts exceeded


I am working on shell script, witch does follow:

  • creates snapshot of EBS Volume;
  • creates AMI image based on this snapshot.

1) I use follow command to create snapshot:
SNAPSHOT_ID=$(aws ec2 create-snapshot "${DRYRUN}" --volume-id "${ROOT_VOLUME_ID}" --description "${SNAPSHOT_DESCRIPTION}" --query 'SnapshotId')

2) I use waiter to wait complete state:
aws ec2 wait snapshot-completed --snapshot-ids "${SNAPSHOT_ID}"

When I test it with EBS Volume 8 GB size everything goes well.
When it is 40 GB, I have an exception:
Waiter SnapshotCompleted failed: Max attempts exceeded

Probably, 40 GB takes more time, then 8 GB one, just need to wait.

AWS Docs (http://docs.aws.amazon.com/cli/latest/reference/ec2/wait/snapshot-completed.html) don't have any timeout or attempts quantity option.

May be some of you have faced the same issue?


Solution

  • So, finally, I used follow way to solve it:

    1. Create snapshot
    2. Use loop to check exit status of command aws ec2 wait snapshot-completed
    3. If exit status is not 0 then print current state, progress and run waiter again.

    # Create snapshot
    SNAPSHOT_DESCRIPTION="Snapshot of Primary frontend instance $(date +%Y-%m-%d)"
    SNAPSHOT_ID=$(aws ec2 create-snapshot "${DRYRUN}" --volume-id "${ROOT_VOLUME_ID}" --description "${SNAPSHOT_DESCRIPTION}" --query 'SnapshotId')
    
    while [ "${exit_status}" != "0" ]
    do
        SNAPSHOT_STATE="$(aws ec2 describe-snapshots --filters Name=snapshot-id,Values=${SNAPSHOT_ID} --query 'Snapshots[0].State')"
        SNAPSHOT_PROGRESS="$(aws ec2 describe-snapshots --filters Name=snapshot-id,Values=${SNAPSHOT_ID} --query 'Snapshots[0].Progress')"
        echo "### Snapshot id ${SNAPSHOT_ID} creation: state is ${SNAPSHOT_STATE}, ${SNAPSHOT_PROGRESS}%..."
    
        aws ec2 wait snapshot-completed --snapshot-ids "${SNAPSHOT_ID}"
        exit_status="$?"
    
    done
    

    If you have something that can improve it, please share with us.