Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.6.0+

Amazon EC2: Create an EBS Snapshot and Monitor Completion

See more Amazon EC2 Examples

Live mode creates a billable snapshot. This is a single-volume backup, not an application-consistent multi-volume backup. Quiesce application writes and flush buffers as appropriate before the create request; cached data is not captured. For a root volume, stopping the instance first is recommended. Encryption follows the source volume; KMS access may be required.

The private checkpoint records region, volume, and request time before creation, then the returned snapshot ID. An existing ID resumes read-only polling, even with dryRun=true. A checkpoint without an ID stops for reconciliation: CreateSnapshot has no client token, so blindly repeating an uncertain create could produce another snapshot. Keep the checkpoint after timeouts. Polls up to 60 times with delays capped at 10 seconds and reports completion, error, or an unconfirmed timeout. This sample never deletes snapshots or resets the checkpoint automatically.

Dry run by default: change dryRun to false deliberately for the live operation. DryRunOperation means the permission check passed, not that a resource was changed or that the later operation is guaranteed to succeed. The example never automatically changes a dry run into a live request.

Uses the current EC2 Query API (Version=2016-11-15) over HTTPS. Chilkat.Rest and AuthAws sign the GET request with AWS Signature Version 4, service ec2. Set the region once; the code builds the corresponding commercial AWS endpoint. Other partitions require their own endpoint suffix. EC2 returns XML, unlike DynamoDB's JSON protocol. AddQueryParam encodes parameter values.

Load current IAM credentials from a private qa_data/aws/ec2.json with string fields access_key, secret_key, and, for temporary credentials, session_token. Prefer short-lived role credentials. The example explicitly reads this file; it does not discover profiles or refresh expired credentials. The session token is included in the signed query, so keep URLs and diagnostics private. A Socket enables TLS certificate verification before handing its connection to Rest.

IAM permissions for the illustrated requests: ec2:CreateSnapshot, ec2:DescribeSnapshots. Use least-privilege permissions for the intended resources; EC2 describe operations generally require Resource *.

Transport completion, HTTP status, and XML validity are checked separately. API errors contain Errors/Error/Code and RequestID; successful responses use requestId. Check the error code rather than assuming every HTTP 4xx means bad input. Use bounded exponential backoff with jitter for retryable failures. EC2 is eventually consistent: a recent resource change may not appear immediately. Verify account, region, ID, and observed state before retrying a change whose outcome is uncertain.

CreateSnapshot reference, DescribeSnapshots reference

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Credentials
string ls_Region
oleobject loo_Endpoint
oleobject loo_Aws
oleobject loo_Rest
oleobject loo_Connection
oleobject loo_Xml
string ls_ResponseXml
string ls_ErrorCode
string ls_VolumeId
long li_DryRun
string ls_CheckpointFile
oleobject loo_Files
oleobject loo_Checkpoint
string ls_SnapshotId
long li_FileStatus
oleobject loo_Started
string ls_State
long li_Attempt
long li_DelayMs
long li_RetryRead

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
//  Private configuration, not a file in public web content or source control.
//  Load current IAM credentials authorized for the EC2 operation.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Credentials
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/ec2.json")
if li_Success <> 1 then
    Write-Debug "Could not load private AWS credentials."
    destroy loo_Credentials
    return
end if

if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (loo_Credentials.StringOf("access_key") = "") then
    Write-Debug "Missing or invalid access_key."
    destroy loo_Credentials
    return
end if

if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (loo_Credentials.StringOf("secret_key") = "") then
    Write-Debug "Missing or invalid secret_key."
    destroy loo_Credentials
    return
end if

//  Set this once to the region containing the instances.
ls_Region = "us-west-2"
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")

loo_Endpoint.Append("ec2.")
loo_Endpoint.Append(ls_Region)
loo_Endpoint.Append(".amazonaws.com")
loo_Aws = create oleobject
li_rc = loo_Aws.ConnectToNewObject("Chilkat.AuthAws")

loo_Aws.AccessKey = loo_Credentials.StringOf("access_key")
loo_Aws.SecretKey = loo_Credentials.StringOf("secret_key")
loo_Aws.Region = ls_Region
loo_Aws.ServiceName = "ec2"
loo_Aws.SignatureVersion = 4
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
li_Success = loo_Rest.SetAuthAws(loo_Aws)
if li_Success <> 1 then
    Write-Debug "Could not configure AWS signing."
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    return
end if

//  Temporary IAM/STS credentials require their session token on every signed request.
//  AddQueryParam includes it in SigV4's canonical query. Never log the request URL.
//  Leave session_token absent or empty only for credentials that do not have one.
if loo_Credentials.HasMember("session_token") = 1 then
    if loo_Credentials.JsonTypeOf("session_token") <> 1 then
        Write-Debug "session_token must be a string."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        return
    end if

    if loo_Credentials.StringOf("session_token") <> "" then
        li_Success = loo_Rest.AddQueryParam("X-Amz-Security-Token",loo_Credentials.StringOf("session_token"))
        if li_Success <> 1 then
            Write-Debug "Could not set the session-token parameter."
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            return
        end if

    end if

end if

//  Supply a Socket to explicitly enable TLS certificate verification.
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")

loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
    Write-Debug "TLS connection failed; inspect diagnostics privately."
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

li_Success = loo_Rest.UseConnection(loo_Connection,1)
if li_Success <> 1 then
    Write-Debug "Could not attach the TLS connection."
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

//  EC2 uses the Query API and XML responses, not AWS JSON protocol headers.
//  AddQueryParam URL-encodes the values; do not pre-encode IDs or tokens.
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

loo_Xml.Strict = 1
li_Success = loo_Rest.AddQueryParam("Version","2016-11-15")
if li_Success <> 1 then
    Write-Debug "Could not set Version."
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

ls_ResponseXml = ""
ls_ErrorCode = ""
ls_VolumeId = "vol-0123456789abcdef0"
li_DryRun = 1
//  Serialize access. Keep this checkpoint after any uncertain create response.
ls_CheckpointFile = "ec2-snapshot-request.json"
loo_Files = create oleobject
li_rc = loo_Files.ConnectToNewObject("Chilkat.FileAccess")

loo_Checkpoint = create oleobject
li_rc = loo_Checkpoint.ConnectToNewObject("Chilkat.JsonObject")

loo_Checkpoint.Strict = 1
ls_SnapshotId = ""
li_FileStatus = loo_Files.FileExists3(ls_CheckpointFile)
if li_FileStatus <> 0 then
    if li_FileStatus <> 1 then
        Write-Debug "Snapshot checkpoint path is inaccessible or is not a file."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        return
    end if

    li_Success = loo_Checkpoint.LoadFile(ls_CheckpointFile)
    if li_Success <> 1 then
        Write-Debug "Could not read snapshot checkpoint."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        return
    end if

    if (loo_Checkpoint.JsonTypeOf("region") <> 1) OR (loo_Checkpoint.StringOf("region") = "") then
        Write-Debug "Missing or invalid saved region."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        return
    end if

    if (loo_Checkpoint.JsonTypeOf("volume_id") <> 1) OR (loo_Checkpoint.StringOf("volume_id") = "") then
        Write-Debug "Missing or invalid saved volume_id."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        return
    end if

    if (loo_Checkpoint.StringOf("region") <> ls_Region) OR (loo_Checkpoint.StringOf("volume_id") <> ls_VolumeId) then
        Write-Debug "Checkpoint belongs to another region or volume."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        return
    end if

    if loo_Checkpoint.HasMember("snapshot_id") <> 1 then
        Write-Debug "An earlier create outcome is uncertain. Reconcile it before another CreateSnapshot."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        return
    end if

    if (loo_Checkpoint.JsonTypeOf("snapshot_id") <> 1) OR (loo_Checkpoint.StringOf("snapshot_id") = "") then
        Write-Debug "Missing or invalid saved snapshot_id."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        return
    end if

    ls_SnapshotId = loo_Checkpoint.StringOf("snapshot_id")
    Write-Debug "Resuming read-only status checks for " + ls_SnapshotId
end if

if ls_SnapshotId = "" then
    if li_DryRun = 0 then
        loo_Started = create oleobject
        li_rc = loo_Started.ConnectToNewObject("Chilkat.CkDateTime")

        li_Success = loo_Started.SetFromCurrentSystemTime()
        if li_Success <> 1 then
            Write-Debug "Could not read the current time."
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Files
            destroy loo_Checkpoint
            destroy loo_Started
            return
        end if

        li_Success = loo_Checkpoint.UpdateString("region",ls_Region)
        if li_Success <> 1 then
            Write-Debug "Could not record region."
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Files
            destroy loo_Checkpoint
            destroy loo_Started
            return
        end if

        li_Success = loo_Checkpoint.UpdateString("volume_id",ls_VolumeId)
        if li_Success <> 1 then
            Write-Debug "Could not record volume."
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Files
            destroy loo_Checkpoint
            destroy loo_Started
            return
        end if

        li_Success = loo_Checkpoint.UpdateString("requested_at",loo_Started.GetAsTimestamp(0))
        if li_Success <> 1 then
            Write-Debug "Could not record request time."
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Files
            destroy loo_Checkpoint
            destroy loo_Started
            return
        end if

        li_Success = loo_Checkpoint.WriteFile(ls_CheckpointFile)
        if li_Success <> 1 then
            Write-Debug "Could not persist create intent; no snapshot request sent."
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Files
            destroy loo_Checkpoint
            destroy loo_Started
            return
        end if

    end if

    li_Success = loo_Rest.AddQueryParam("Action","CreateSnapshot")
    if li_Success <> 1 then
        Write-Debug "Could not set Action."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    li_Success = loo_Rest.AddQueryParam("VolumeId",ls_VolumeId)
    if li_Success <> 1 then
        Write-Debug "Could not set VolumeId."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    li_Success = loo_Rest.AddQueryParam("Description","Example volume snapshot")
    if li_Success <> 1 then
        Write-Debug "Could not set Description."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    if li_DryRun = 1 then
        li_Success = loo_Rest.AddQueryParam("DryRun","true")
        if li_Success <> 1 then
            Write-Debug "Could not set DryRun."
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Files
            destroy loo_Checkpoint
            destroy loo_Started
            return
        end if

    end if

    ls_ResponseXml = loo_Rest.FullRequestNoBody("GET","/")
    if loo_Rest.LastMethodSuccess <> 1 then
        Write-Debug "Request did not complete; inspect diagnostics privately."
        //  The operation might already have been accepted. Check state before retrying.
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    li_Success = loo_Xml.LoadXml(ls_ResponseXml)
    if loo_Rest.ResponseStatusCode <> 200 then
        Write-Debug "EC2 HTTP status: " + string(loo_Rest.ResponseStatusCode)
        if li_Success = 1 then
            if loo_Xml.HasChildWithTag("Errors|Error|Code") = 1 then
                ls_ErrorCode = loo_Xml.GetChildContent("Errors|Error|Code")
                if (li_DryRun = 1) AND (ls_ErrorCode = "DryRunOperation") then
                    Write-Debug "Dry run authorized. No resource was changed."
                    Write-Debug "Set dryRun to false only when ready to perform the operation."
                    destroy loo_Credentials
                    destroy loo_Endpoint
                    destroy loo_Aws
                    destroy loo_Rest
                    destroy loo_Connection
                    destroy loo_Xml
                    destroy loo_Files
                    destroy loo_Checkpoint
                    destroy loo_Started
                    return
                end if

                Write-Debug "AWS error code: " + ls_ErrorCode
            end if

            if loo_Xml.HasChildWithTag("RequestID") = 1 then
                Write-Debug "AWS request ID: " + loo_Xml.GetChildContent("RequestID")
            end if

        end if

        //  Keep full error messages and signed request diagnostics private.
        //  UnauthorizedOperation is not a successful dry run. Check IAM permissions.
        //  Retry throttled reads with bounded backoff; renew expired credentials.
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    if li_Success <> 1 then
        Write-Debug "Expected a well-formed XML response."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    if loo_Xml.Tag <> "CreateSnapshotResponse" then
        Write-Debug "Unexpected EC2 response element."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    if li_DryRun = 1 then
        Write-Debug "Unexpected HTTP 200 for a dry run; do not treat this as authorization to proceed."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    if (loo_Xml.HasChildWithTag("requestId") <> 1) OR (loo_Xml.GetChildContent("requestId") = "") then
        Write-Debug "Missing or empty requestId."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    Write-Debug "AWS request ID: " + loo_Xml.GetChildContent("requestId")
    if (loo_Xml.HasChildWithTag("snapshotId") <> 1) OR (loo_Xml.GetChildContent("snapshotId") = "") then
        Write-Debug "Missing or empty snapshotId."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    ls_SnapshotId = loo_Xml.GetChildContent("snapshotId")
    Write-Debug "Snapshot creation accepted: " + ls_SnapshotId
    li_Success = loo_Checkpoint.UpdateString("snapshot_id",ls_SnapshotId)
    if li_Success <> 1 then
        Write-Debug "Could not record snapshot ID; save the displayed ID privately."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    li_Success = loo_Checkpoint.WriteFile(ls_CheckpointFile)
    if li_Success <> 1 then
        Write-Debug "Snapshot accepted but checkpoint save failed; record the ID before continuing."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    li_Success = loo_Rest.RemoveQueryParam("Action")
    if li_Success <> 1 then
        Write-Debug "Could not change action."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    li_Success = loo_Rest.RemoveQueryParam("VolumeId")
    if li_Success <> 1 then
        Write-Debug "Could not remove create parameter."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    li_Success = loo_Rest.RemoveQueryParam("Description")
    if li_Success <> 1 then
        Write-Debug "Could not remove description."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

end if

//  Keep the signed session token and Version; remove only the prior action's parameters.
li_Success = loo_Rest.AddQueryParam("Action","DescribeSnapshots")
if li_Success <> 1 then
    Write-Debug "Could not set Action."
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Files
    destroy loo_Checkpoint
    destroy loo_Started
    return
end if

li_Success = loo_Rest.AddQueryParam("SnapshotId.1",ls_SnapshotId)
if li_Success <> 1 then
    Write-Debug "Could not set SnapshotId.1."
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    destroy loo_Files
    destroy loo_Checkpoint
    destroy loo_Started
    return
end if

ls_State = ""
//  At most 60 attempts, with delays growing from 2 to 10 seconds.
//  HTTP timeouts add to total elapsed time. No busy loop or automatic write retry.
li_Attempt = 0
li_DelayMs = 2000
li_RetryRead = 0
do while (li_Attempt < 60)
    li_RetryRead = 0
    ls_ResponseXml = loo_Rest.FullRequestNoBody("GET","/")
    if loo_Rest.LastMethodSuccess <> 1 then
        Write-Debug "Polling transport failed; resume the read later. No write was retried."
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        destroy loo_Files
        destroy loo_Checkpoint
        destroy loo_Started
        return
    end if

    li_Success = loo_Xml.LoadXml(ls_ResponseXml)
    if loo_Rest.ResponseStatusCode <> 200 then
        ls_ErrorCode = ""
        if li_Success = 1 then
            if loo_Xml.HasChildWithTag("Errors|Error|Code") = 1 then
                ls_ErrorCode = loo_Xml.GetChildContent("Errors|Error|Code")
            end if

        end if

        if (ls_ErrorCode = "RequestLimitExceeded") OR (ls_ErrorCode = "InternalError") OR (ls_ErrorCode = "ServiceUnavailable") OR (ls_ErrorCode = "InvalidSnapshot.NotFound") then
            li_RetryRead = 1
        else
            Write-Debug "Polling failed: HTTP " + string(loo_Rest.ResponseStatusCode) + "; AWS code: " + ls_ErrorCode
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Files
            destroy loo_Checkpoint
            destroy loo_Started
            return
        end if

    end if

    if li_RetryRead = 0 then
        if li_Success <> 1 then
            Write-Debug "Expected a well-formed XML response."
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Files
            destroy loo_Checkpoint
            destroy loo_Started
            return
        end if

        if loo_Xml.Tag <> "DescribeSnapshotsResponse" then
            Write-Debug "Unexpected polling response."
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Files
            destroy loo_Checkpoint
            destroy loo_Started
            return
        end if

        if (loo_Xml.HasChildWithTag("requestId") <> 1) OR (loo_Xml.GetChildContent("requestId") = "") then
            Write-Debug "Missing or empty requestId."
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Files
            destroy loo_Checkpoint
            destroy loo_Started
            return
        end if

        if loo_Xml.HasChildWithTag("snapshotSet") <> 1 then
            Write-Debug "Expected snapshotSet."
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_Aws
            destroy loo_Rest
            destroy loo_Connection
            destroy loo_Xml
            destroy loo_Files
            destroy loo_Checkpoint
            destroy loo_Started
            return
        end if

        if loo_Xml.NumChildrenAt("snapshotSet") > 0 then
            if loo_Xml.NumChildrenAt("snapshotSet") <> 1 then
                Write-Debug "Expected one snapshot."
                destroy loo_Credentials
                destroy loo_Endpoint
                destroy loo_Aws
                destroy loo_Rest
                destroy loo_Connection
                destroy loo_Xml
                destroy loo_Files
                destroy loo_Checkpoint
                destroy loo_Started
                return
            end if

            if (loo_Xml.HasChildWithTag("snapshotSet|item|snapshotId") <> 1) OR (loo_Xml.GetChildContent("snapshotSet|item|snapshotId") = "") then
                Write-Debug "Missing or empty snapshotSet|item|snapshotId."
                destroy loo_Credentials
                destroy loo_Endpoint
                destroy loo_Aws
                destroy loo_Rest
                destroy loo_Connection
                destroy loo_Xml
                destroy loo_Files
                destroy loo_Checkpoint
                destroy loo_Started
                return
            end if

            if loo_Xml.GetChildContent("snapshotSet|item|snapshotId") <> ls_SnapshotId then
                Write-Debug "Unexpected snapshot ID."
                destroy loo_Credentials
                destroy loo_Endpoint
                destroy loo_Aws
                destroy loo_Rest
                destroy loo_Connection
                destroy loo_Xml
                destroy loo_Files
                destroy loo_Checkpoint
                destroy loo_Started
                return
            end if

            if (loo_Xml.HasChildWithTag("snapshotSet|item|status") <> 1) OR (loo_Xml.GetChildContent("snapshotSet|item|status") = "") then
                Write-Debug "Missing or empty snapshotSet|item|status."
                destroy loo_Credentials
                destroy loo_Endpoint
                destroy loo_Aws
                destroy loo_Rest
                destroy loo_Connection
                destroy loo_Xml
                destroy loo_Files
                destroy loo_Checkpoint
                destroy loo_Started
                return
            end if

            ls_State = loo_Xml.GetChildContent("snapshotSet|item|status")
            Write-Debug "Snapshot state: " + ls_State
            if loo_Xml.HasChildWithTag("snapshotSet|item|progress") = 1 then
                Write-Debug "Progress: " + loo_Xml.GetChildContent("snapshotSet|item|progress")
            end if

            if ls_State = "completed" then
                Write-Debug "Snapshot completed: " + ls_SnapshotId
                destroy loo_Credentials
                destroy loo_Endpoint
                destroy loo_Aws
                destroy loo_Rest
                destroy loo_Connection
                destroy loo_Xml
                destroy loo_Files
                destroy loo_Checkpoint
                destroy loo_Started
                return
            end if

            if ls_State = "error" then
                Write-Debug "Snapshot entered error state. Inspect details privately; do not create another automatically."
                destroy loo_Credentials
                destroy loo_Endpoint
                destroy loo_Aws
                destroy loo_Rest
                destroy loo_Connection
                destroy loo_Xml
                destroy loo_Files
                destroy loo_Checkpoint
                destroy loo_Started
                return
            end if

        end if

    end if

    li_Attempt = li_Attempt + 1
    if li_Attempt < 60 then
        loo_Connection.SleepMs(li_DelayMs)
        if li_DelayMs < 10000 then
            li_DelayMs = li_DelayMs * 2
            if li_DelayMs > 10000 then
                li_DelayMs = 10000
            end if

        end if

    end if

loop
Write-Debug "Polling limit reached; completion has not been confirmed. Resume checking later."


destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_Files
destroy loo_Checkpoint
destroy loo_Started