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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loCredentials
LOCAL lcRegion
LOCAL loEndpoint
LOCAL loAws
LOCAL loRest
LOCAL loConnection
LOCAL loXml
LOCAL lcResponseXml
LOCAL lcErrorCode
LOCAL lcVolumeId
LOCAL lnDryRun
LOCAL lcCheckpointFile
LOCAL loFiles
LOCAL loCheckpoint
LOCAL lcSnapshotId
LOCAL lnFileStatus
LOCAL loStarted
LOCAL lcState
LOCAL lnAttempt
LOCAL lnDelayMs
LOCAL lnRetryRead
* Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
* Private configuration, not a file in public web content or source control.
* Load current IAM credentials authorized for the EC2 operation.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/ec2.json")
IF (lnSuccess <> 1) THEN
? "Could not load private AWS credentials."
RELEASE loCredentials
CANCEL
ENDIF
IF ((loCredentials.JsonTypeOf("access_key") <> 1) OR (loCredentials.StringOf("access_key") = "")) THEN
? "Missing or invalid access_key."
RELEASE loCredentials
CANCEL
ENDIF
IF ((loCredentials.JsonTypeOf("secret_key") <> 1) OR (loCredentials.StringOf("secret_key") = "")) THEN
? "Missing or invalid secret_key."
RELEASE loCredentials
CANCEL
ENDIF
* Set this once to the region containing the instances.
lcRegion = "us-west-2"
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("ec2.")
loEndpoint.Append(lcRegion)
loEndpoint.Append(".amazonaws.com")
loAws = CreateObject('Chilkat.AuthAws')
loAws.AccessKey = loCredentials.StringOf("access_key")
loAws.SecretKey = loCredentials.StringOf("secret_key")
loAws.Region = lcRegion
loAws.ServiceName = "ec2"
loAws.SignatureVersion = 4
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
lnSuccess = loRest.SetAuthAws(loAws)
IF (lnSuccess <> 1) THEN
? "Could not configure AWS signing."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
* 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 (loCredentials.HasMember("session_token") = 1) THEN
IF (loCredentials.JsonTypeOf("session_token") <> 1) THEN
? "session_token must be a string."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
IF (loCredentials.StringOf("session_token") <> "") THEN
lnSuccess = loRest.AddQueryParam("X-Amz-Security-Token",loCredentials.StringOf("session_token"))
IF (lnSuccess <> 1) THEN
? "Could not set the session-token parameter."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
CANCEL
ENDIF
ENDIF
ENDIF
* Supply a Socket to explicitly enable TLS certificate verification.
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
loConnection.MaxReadIdleMs = 60000
loConnection.MaxSendIdleMs = 60000
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
? "TLS connection failed; inspect diagnostics privately."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
lnSuccess = loRest.UseConnection(loConnection,1)
IF (lnSuccess <> 1) THEN
? "Could not attach the TLS connection."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
* 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.
loXml = CreateObject('Chilkat.Xml')
loXml.Strict = 1
lnSuccess = loRest.AddQueryParam("Version","2016-11-15")
IF (lnSuccess <> 1) THEN
? "Could not set Version."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
CANCEL
ENDIF
lcResponseXml = ""
lcErrorCode = ""
lcVolumeId = "vol-0123456789abcdef0"
lnDryRun = 1
* Serialize access. Keep this checkpoint after any uncertain create response.
lcCheckpointFile = "ec2-snapshot-request.json"
loFiles = CreateObject('Chilkat.FileAccess')
loCheckpoint = CreateObject('Chilkat.JsonObject')
loCheckpoint.Strict = 1
lcSnapshotId = ""
lnFileStatus = loFiles.FileExists3(lcCheckpointFile)
IF (lnFileStatus <> 0) THEN
IF (lnFileStatus <> 1) THEN
? "Snapshot checkpoint path is inaccessible or is not a file."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
CANCEL
ENDIF
lnSuccess = loCheckpoint.LoadFile(lcCheckpointFile)
IF (lnSuccess <> 1) THEN
? "Could not read snapshot checkpoint."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
CANCEL
ENDIF
IF ((loCheckpoint.JsonTypeOf("region") <> 1) OR (loCheckpoint.StringOf("region") = "")) THEN
? "Missing or invalid saved region."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
CANCEL
ENDIF
IF ((loCheckpoint.JsonTypeOf("volume_id") <> 1) OR (loCheckpoint.StringOf("volume_id") = "")) THEN
? "Missing or invalid saved volume_id."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
CANCEL
ENDIF
IF ((loCheckpoint.StringOf("region") <> lcRegion) OR (loCheckpoint.StringOf("volume_id") <> lcVolumeId)) THEN
? "Checkpoint belongs to another region or volume."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
CANCEL
ENDIF
IF (loCheckpoint.HasMember("snapshot_id") <> 1) THEN
? "An earlier create outcome is uncertain. Reconcile it before another CreateSnapshot."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
CANCEL
ENDIF
IF ((loCheckpoint.JsonTypeOf("snapshot_id") <> 1) OR (loCheckpoint.StringOf("snapshot_id") = "")) THEN
? "Missing or invalid saved snapshot_id."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
CANCEL
ENDIF
lcSnapshotId = loCheckpoint.StringOf("snapshot_id")
? "Resuming read-only status checks for " + lcSnapshotId
ENDIF
IF (lcSnapshotId = "") THEN
IF (lnDryRun = 0) THEN
loStarted = CreateObject('Chilkat.CkDateTime')
lnSuccess = loStarted.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
? "Could not read the current time."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loCheckpoint.UpdateString("region",lcRegion)
IF (lnSuccess <> 1) THEN
? "Could not record region."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loCheckpoint.UpdateString("volume_id",lcVolumeId)
IF (lnSuccess <> 1) THEN
? "Could not record volume."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loCheckpoint.UpdateString("requested_at",loStarted.GetAsTimestamp(0))
IF (lnSuccess <> 1) THEN
? "Could not record request time."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loCheckpoint.WriteFile(lcCheckpointFile)
IF (lnSuccess <> 1) THEN
? "Could not persist create intent; no snapshot request sent."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
ENDIF
lnSuccess = loRest.AddQueryParam("Action","CreateSnapshot")
IF (lnSuccess <> 1) THEN
? "Could not set Action."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("VolumeId",lcVolumeId)
IF (lnSuccess <> 1) THEN
? "Could not set VolumeId."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("Description","Example volume snapshot")
IF (lnSuccess <> 1) THEN
? "Could not set Description."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF (lnDryRun = 1) THEN
lnSuccess = loRest.AddQueryParam("DryRun","true")
IF (lnSuccess <> 1) THEN
? "Could not set DryRun."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
ENDIF
lcResponseXml = loRest.FullRequestNoBody("GET","/")
IF (loRest.LastMethodSuccess <> 1) THEN
? "Request did not complete; inspect diagnostics privately."
* The operation might already have been accepted. Check state before retrying.
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loXml.LoadXml(lcResponseXml)
IF (loRest.ResponseStatusCode <> 200) THEN
? "EC2 HTTP status: " + STR(loRest.ResponseStatusCode)
IF (lnSuccess = 1) THEN
IF (loXml.HasChildWithTag("Errors|Error|Code") = 1) THEN
lcErrorCode = loXml.GetChildContent("Errors|Error|Code")
IF ((lnDryRun = 1) AND (lcErrorCode = "DryRunOperation")) THEN
? "Dry run authorized. No resource was changed."
? "Set dryRun to false only when ready to perform the operation."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
? "AWS error code: " + lcErrorCode
ENDIF
IF (loXml.HasChildWithTag("RequestID") = 1) THEN
? "AWS request ID: " + loXml.GetChildContent("RequestID")
ENDIF
ENDIF
* 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.
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF (lnSuccess <> 1) THEN
? "Expected a well-formed XML response."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF (loXml.Tag <> "CreateSnapshotResponse") THEN
? "Unexpected EC2 response element."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF (lnDryRun = 1) THEN
? "Unexpected HTTP 200 for a dry run; do not treat this as authorization to proceed."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF ((loXml.HasChildWithTag("requestId") <> 1) OR (loXml.GetChildContent("requestId") = "")) THEN
? "Missing or empty requestId."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
? "AWS request ID: " + loXml.GetChildContent("requestId")
IF ((loXml.HasChildWithTag("snapshotId") <> 1) OR (loXml.GetChildContent("snapshotId") = "")) THEN
? "Missing or empty snapshotId."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lcSnapshotId = loXml.GetChildContent("snapshotId")
? "Snapshot creation accepted: " + lcSnapshotId
lnSuccess = loCheckpoint.UpdateString("snapshot_id",lcSnapshotId)
IF (lnSuccess <> 1) THEN
? "Could not record snapshot ID; save the displayed ID privately."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loCheckpoint.WriteFile(lcCheckpointFile)
IF (lnSuccess <> 1) THEN
? "Snapshot accepted but checkpoint save failed; record the ID before continuing."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loRest.RemoveQueryParam("Action")
IF (lnSuccess <> 1) THEN
? "Could not change action."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loRest.RemoveQueryParam("VolumeId")
IF (lnSuccess <> 1) THEN
? "Could not remove create parameter."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loRest.RemoveQueryParam("Description")
IF (lnSuccess <> 1) THEN
? "Could not remove description."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
ENDIF
* Keep the signed session token and Version; remove only the prior action's parameters.
lnSuccess = loRest.AddQueryParam("Action","DescribeSnapshots")
IF (lnSuccess <> 1) THEN
? "Could not set Action."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("SnapshotId.1",lcSnapshotId)
IF (lnSuccess <> 1) THEN
? "Could not set SnapshotId.1."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lcState = ""
* 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.
lnAttempt = 0
lnDelayMs = 2000
lnRetryRead = 0
DO WHILE (lnAttempt < 60)
lnRetryRead = 0
lcResponseXml = loRest.FullRequestNoBody("GET","/")
IF (loRest.LastMethodSuccess <> 1) THEN
? "Polling transport failed; resume the read later. No write was retried."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lnSuccess = loXml.LoadXml(lcResponseXml)
IF (loRest.ResponseStatusCode <> 200) THEN
lcErrorCode = ""
IF (lnSuccess = 1) THEN
IF (loXml.HasChildWithTag("Errors|Error|Code") = 1) THEN
lcErrorCode = loXml.GetChildContent("Errors|Error|Code")
ENDIF
ENDIF
IF ((lcErrorCode = "RequestLimitExceeded") OR (lcErrorCode = "InternalError") OR (lcErrorCode = "ServiceUnavailable") OR (lcErrorCode = "InvalidSnapshot.NotFound")) THEN
lnRetryRead = 1
ELSE
? "Polling failed: HTTP " + STR(loRest.ResponseStatusCode) + "; AWS code: " + lcErrorCode
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
ENDIF
IF (lnRetryRead = 0) THEN
IF (lnSuccess <> 1) THEN
? "Expected a well-formed XML response."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF (loXml.Tag <> "DescribeSnapshotsResponse") THEN
? "Unexpected polling response."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF ((loXml.HasChildWithTag("requestId") <> 1) OR (loXml.GetChildContent("requestId") = "")) THEN
? "Missing or empty requestId."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF (loXml.HasChildWithTag("snapshotSet") <> 1) THEN
? "Expected snapshotSet."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF (loXml.NumChildrenAt("snapshotSet") > 0) THEN
IF (loXml.NumChildrenAt("snapshotSet") <> 1) THEN
? "Expected one snapshot."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF ((loXml.HasChildWithTag("snapshotSet|item|snapshotId") <> 1) OR (loXml.GetChildContent("snapshotSet|item|snapshotId") = "")) THEN
? "Missing or empty snapshotSet|item|snapshotId."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF (loXml.GetChildContent("snapshotSet|item|snapshotId") <> lcSnapshotId) THEN
? "Unexpected snapshot ID."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF ((loXml.HasChildWithTag("snapshotSet|item|status") <> 1) OR (loXml.GetChildContent("snapshotSet|item|status") = "")) THEN
? "Missing or empty snapshotSet|item|status."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
lcState = loXml.GetChildContent("snapshotSet|item|status")
? "Snapshot state: " + lcState
IF (loXml.HasChildWithTag("snapshotSet|item|progress") = 1) THEN
? "Progress: " + loXml.GetChildContent("snapshotSet|item|progress")
ENDIF
IF (lcState = "completed") THEN
? "Snapshot completed: " + lcSnapshotId
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
IF (lcState = "error") THEN
? "Snapshot entered error state. Inspect details privately; do not create another automatically."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted
CANCEL
ENDIF
ENDIF
ENDIF
lnAttempt = lnAttempt + 1
IF (lnAttempt < 60) THEN
loConnection.SleepMs(lnDelayMs)
IF (lnDelayMs < 10000) THEN
lnDelayMs = lnDelayMs * 2
IF (lnDelayMs > 10000) THEN
lnDelayMs = 10000
ENDIF
ENDIF
ENDIF
ENDDO
? "Polling limit reached; completion has not been confirmed. Resume checking later."
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loFiles
RELEASE loCheckpoint
RELEASE loStarted