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

Amazon SQS: Start, Inspect, and Cancel Dead-Letter Redrive

See more Amazon SQS Examples

Default list mode reads the ten most recent movement tasks for a DLQ ARN. Start mode begins redrive at ten messages per second; an empty destinationArn returns messages to their original source queues, or supply a same-type destination queue ARN. Cancel mode requires a running task handle. Each run performs one chosen operation, with no automatic polling or write replay. IDs and large returned counts stay strings.

This service redrive supports DLQs whose sources are SQS queues, not arbitrary queues or non-SQS sources. Only one task can be active per source DLQ. Cancellation stops remaining movement and does not undo moved messages. Redriven messages get new message IDs/enqueue times and can interleave with new producer traffic. Fix the processing problem before redrive to avoid cycling messages. List results are recent task history, not a paginated complete audit.

Start requires StartMessageMoveTask, ReceiveMessage, DeleteMessage and GetQueueAttributes on the DLQ, plus SendMessage on destinations. List requires ListMessageMoveTasks and GetQueueAttributes. Cancel requires CancelMessageMoveTask with the relevant receive/delete/attributes permissions. Encrypted messages also need appropriate KMS access. See AWS redrive permissions.

Uses SQS's AWS JSON protocol: POST to the regional root / with Content-Type: application/x-amz-json-1.0 and an operation-specific X-Amz-Target: AmazonSQS.... Chilkat.Rest and AuthAws sign with AWS Signature Version 4, service sqs. AWS still supports the older Query/XML protocol; these examples use JSON for simpler request and response handling. No AWS SDK is required.

Provide current IAM credentials in a private qa_data/aws/sqs.json with string fields access_key, secret_key, and session_token for temporary credentials. Queue-specific examples also read queue_url from this file. Set the region in the code to match the queue. These examples use commercial regional endpoints; other partitions or custom endpoints require corresponding changes. Prefer short-lived role credentials. The file is explicitly loaded; profiles and credential refresh are not automatic. Keep it outside public content and source control. The temporary token is included in the signed query; keep URLs and diagnostics private. TLS certificate verification is enabled.

Transport completion, HTTP status, and response validity are checked separately. Use bounded backoff with jitter for retryable errors, and refresh expired credentials. A timed-out write or receive may already have been accepted. Standard queues can deliver duplicates: make application processing idempotent and delete only after successful processing. Automatic reconnection is disabled for writes and receives to avoid replay after a lost response. Read-only queue listing permits reconnection.

Required permissions depend on the selected operation, as described above. Scope access to the intended queue. Customer-managed KMS encryption can require additional KMS permissions for producers and consumers.

AWS StartMessageMoveTask reference

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loTextCompare
LOCAL lcOperation
LOCAL lcSourceArn
LOCAL lcDestinationArn
LOCAL lcTaskHandle
LOCAL lnSameText1
LOCAL lnSameText2
LOCAL lnSameText3
LOCAL loCredentials
LOCAL lnSameText4
LOCAL lnSameText5
LOCAL lcRegion
LOCAL loEndpoint
LOCAL loAws
LOCAL loRest
LOCAL lnSameText6
LOCAL loConnection
LOCAL loBody
LOCAL loResult
LOCAL loRequestBody
LOCAL loResponseBody
LOCAL lnSameText7
LOCAL lnSameText8
LOCAL lnSameText9
LOCAL lnSameText10
LOCAL lnSameText11
LOCAL lnSameText12
LOCAL lnResultsType
LOCAL i
LOCAL lnSameText13

lnSuccess = 0

*  StringBuilder compares text by value in every generated language.
loTextCompare = CreateObject('Chilkat.StringBuilder')
*  list is read-only. Choose start or cancel deliberately to change message movement.
lcOperation = "list"
lcSourceArn = "arn:aws:sqs:us-west-2:123456789012:ExampleDeadLetterQueue"
lcDestinationArn = ""
lcTaskHandle = "TASK_HANDLE_FROM_START_OR_LIST"
loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText1 = loTextCompare.ContentsEqual("list",1)
loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText2 = loTextCompare.ContentsEqual("start",1)
loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText3 = loTextCompare.ContentsEqual("cancel",1)
IF ((lnSameText1 <> 1) AND (lnSameText2 <> 1) AND (lnSameText3 <> 1)) THEN
    ? "Choose list, start, or cancel."
    RELEASE loTextCompare
    CANCEL
ENDIF

*  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 SQS operation.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/sqs.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load private AWS credentials."
    RELEASE loTextCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loCredentials.StringOf("access_key"))
lnSameText4 = loTextCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("access_key") <> 1) OR (lnSameText4 = 1)) THEN
    ? "Missing or invalid access_key."
    RELEASE loTextCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loCredentials.StringOf("secret_key"))
lnSameText5 = loTextCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("secret_key") <> 1) OR (lnSameText5 = 1)) THEN
    ? "Missing or invalid secret_key."
    RELEASE loTextCompare
    RELEASE loCredentials
    CANCEL
ENDIF

*  Set this once to the region containing the queue.
lcRegion = "us-west-2"
loEndpoint = CreateObject('Chilkat.StringBuilder')
lnSuccess = loEndpoint.Append("sqs.")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    CANCEL
ENDIF

lnSuccess = loEndpoint.Append(lcRegion)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    CANCEL
ENDIF

lnSuccess = loEndpoint.Append(".amazonaws.com")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    CANCEL
ENDIF

loAws = CreateObject('Chilkat.AuthAws')
loAws.AccessKey = loCredentials.StringOf("access_key")
loAws.SecretKey = loCredentials.StringOf("secret_key")
loAws.Region = lcRegion
loAws.ServiceName = "sqs"
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 loTextCompare
    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 loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        CANCEL
    ENDIF

    loTextCompare.Clear()
    loTextCompare.SetString(loCredentials.StringOf("session_token"))
    lnSameText6 = loTextCompare.ContentsEqual("",1)
    IF (lnSameText6 <> 1) THEN
        lnSuccess = loRest.AddQueryParam("X-Amz-Security-Token",loCredentials.StringOf("session_token"))
        IF (lnSuccess <> 1) THEN
            ? "Could not set the session-token parameter."
            RELEASE loTextCompare
            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 loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    CANCEL
ENDIF

*  Do not automatically reconnect/replay a write or receive after a lost response.
lnSuccess = loRest.UseConnection(loConnection,0)
IF (lnSuccess <> 1) THEN
    ? "Could not attach the TLS connection."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    CANCEL
ENDIF

lnSuccess = loRest.AddHeader("Content-Type","application/x-amz-json-1.0")
IF (lnSuccess <> 1) THEN
    ? "Could not set the content type."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    CANCEL
ENDIF

loBody = CreateObject('Chilkat.JsonObject')
loResult = CreateObject('Chilkat.JsonObject')
loResult.Strict = 1
loRequestBody = CreateObject('Chilkat.StringBuilder')
loResponseBody = CreateObject('Chilkat.StringBuilder')
lnSuccess = loRest.AddHeader("X-Amz-Target","AmazonSQS.ListMessageMoveTasks")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText7 = loTextCompare.ContentsEqual("start",1)
IF (lnSameText7 = 1) THEN
    lnSuccess = loRest.AddHeader("X-Amz-Target","AmazonSQS.StartMessageMoveTask")
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    lnSuccess = loBody.UpdateString("SourceArn",lcSourceArn)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    lnSuccess = loBody.UpdateInt("MaxNumberOfMessagesPerSecond",10)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    loTextCompare.Clear()
    loTextCompare.SetString(lcDestinationArn)
    lnSameText8 = loTextCompare.ContentsEqual("",1)
    IF (lnSameText8 <> 1) THEN
        lnSuccess = loBody.UpdateString("DestinationArn",lcDestinationArn)
        IF (lnSuccess <> 1) THEN
            ? "Could not prepare the request."
            RELEASE loTextCompare
            RELEASE loCredentials
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loBody
            RELEASE loResult
            RELEASE loRequestBody
            RELEASE loResponseBody
            CANCEL
        ENDIF

    ENDIF

    *  EmitSb appends, so clear both builders before each request/page.
    loRequestBody.Clear()
    loResponseBody.Clear()
    lnSuccess = loBody.EmitSb(loRequestBody)
    IF (lnSuccess <> 1) THEN
        ? "Could not serialize JSON."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    lnSuccess = loRest.FullRequestSb("POST","/",loRequestBody,loResponseBody)
    IF (lnSuccess <> 1) THEN
        ? "Request did not complete; inspect diagnostics privately."
        *  A write or receive may have reached SQS. Do not blindly replay it.
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    IF (loRest.ResponseStatusCode <> 200) THEN
        ? "SQS HTTP status: " + STR(loRest.ResponseStatusCode)
        ? "AWS request ID: " + loRest.ResponseHdrByName("x-amzn-RequestId")
        lnSuccess = loResult.LoadSb(loResponseBody)
        IF ((lnSuccess = 1) AND (loResult.JsonTypeOf("__type") = 1)) THEN
            ? "AWS error type: " + loResult.StringOf("__type")
        ELSE
            ? "AWS error type: " + loRest.ResponseHdrByName("x-amzn-errortype")
        ENDIF

        *  Error messages, payloads, receipt handles and signed URLs can be private.
        *  HTTP 400 can be throttling, expired credentials, or invalid parameters.
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    lnSuccess = loResponseBody.Trim()
    IF (lnSuccess <> 1) THEN
        ? "Could not inspect response."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    IF (loResponseBody.StartsWith("{",1) <> 1) THEN
        ? "Expected a JSON object response."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    lnSuccess = loResult.LoadSb(loResponseBody)
    IF (lnSuccess <> 1) THEN
        ? "Expected well-formed JSON."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    loTextCompare.Clear()
    loTextCompare.SetString(loResult.StringOf("TaskHandle"))
    lnSameText9 = loTextCompare.ContentsEqual("",1)
    IF ((loResult.JsonTypeOf("TaskHandle") <> 1) OR (lnSameText9 = 1)) THEN
        ? "Missing or invalid TaskHandle."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    ? "Redrive started. Task handle: " + loResult.StringOf("TaskHandle")
    *  Save the handle privately; use list mode later to inspect progress.
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(lcOperation)
lnSameText10 = loTextCompare.ContentsEqual("cancel",1)
IF (lnSameText10 = 1) THEN
    loTextCompare.Clear()
    loTextCompare.SetString(lcTaskHandle)
    lnSameText11 = loTextCompare.ContentsEqual("",1)
    loTextCompare.Clear()
    loTextCompare.SetString(lcTaskHandle)
    lnSameText12 = loTextCompare.ContentsEqual("TASK_HANDLE_FROM_START_OR_LIST",1)
    IF ((lnSameText11 = 1) OR (lnSameText12 = 1)) THEN
        ? "Supply taskHandle from the application before running."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    lnSuccess = loRest.AddHeader("X-Amz-Target","AmazonSQS.CancelMessageMoveTask")
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    lnSuccess = loBody.UpdateString("TaskHandle",lcTaskHandle)
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    *  EmitSb appends, so clear both builders before each request/page.
    loRequestBody.Clear()
    loResponseBody.Clear()
    lnSuccess = loBody.EmitSb(loRequestBody)
    IF (lnSuccess <> 1) THEN
        ? "Could not serialize JSON."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    lnSuccess = loRest.FullRequestSb("POST","/",loRequestBody,loResponseBody)
    IF (lnSuccess <> 1) THEN
        ? "Request did not complete; inspect diagnostics privately."
        *  A write or receive may have reached SQS. Do not blindly replay it.
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    IF (loRest.ResponseStatusCode <> 200) THEN
        ? "SQS HTTP status: " + STR(loRest.ResponseStatusCode)
        ? "AWS request ID: " + loRest.ResponseHdrByName("x-amzn-RequestId")
        lnSuccess = loResult.LoadSb(loResponseBody)
        IF ((lnSuccess = 1) AND (loResult.JsonTypeOf("__type") = 1)) THEN
            ? "AWS error type: " + loResult.StringOf("__type")
        ELSE
            ? "AWS error type: " + loRest.ResponseHdrByName("x-amzn-errortype")
        ENDIF

        *  Error messages, payloads, receipt handles and signed URLs can be private.
        *  HTTP 400 can be throttling, expired credentials, or invalid parameters.
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    lnSuccess = loResponseBody.Trim()
    IF (lnSuccess <> 1) THEN
        ? "Could not inspect response."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    IF (loResponseBody.StartsWith("{",1) <> 1) THEN
        ? "Expected a JSON object response."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    lnSuccess = loResult.LoadSb(loResponseBody)
    IF (lnSuccess <> 1) THEN
        ? "Expected well-formed JSON."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    IF (loResult.JsonTypeOf("ApproximateNumberOfMessagesMoved") <> 2) THEN
        ? "Expected approximate moved count."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    ? "Cancellation accepted; messages already moved remain at their destination."
    ? "Approximate messages moved: " + loResult.StringOf("ApproximateNumberOfMessagesMoved")
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

lnSuccess = loBody.UpdateString("SourceArn",lcSourceArn)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

lnSuccess = loBody.UpdateInt("MaxResults",10)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

*  EmitSb appends, so clear both builders before each request/page.
loRequestBody.Clear()
loResponseBody.Clear()
lnSuccess = loBody.EmitSb(loRequestBody)
IF (lnSuccess <> 1) THEN
    ? "Could not serialize JSON."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

lnSuccess = loRest.FullRequestSb("POST","/",loRequestBody,loResponseBody)
IF (lnSuccess <> 1) THEN
    ? "Request did not complete; inspect diagnostics privately."
    *  A write or receive may have reached SQS. Do not blindly replay it.
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

IF (loRest.ResponseStatusCode <> 200) THEN
    ? "SQS HTTP status: " + STR(loRest.ResponseStatusCode)
    ? "AWS request ID: " + loRest.ResponseHdrByName("x-amzn-RequestId")
    lnSuccess = loResult.LoadSb(loResponseBody)
    IF ((lnSuccess = 1) AND (loResult.JsonTypeOf("__type") = 1)) THEN
        ? "AWS error type: " + loResult.StringOf("__type")
    ELSE
        ? "AWS error type: " + loRest.ResponseHdrByName("x-amzn-errortype")
    ENDIF

    *  Error messages, payloads, receipt handles and signed URLs can be private.
    *  HTTP 400 can be throttling, expired credentials, or invalid parameters.
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

lnSuccess = loResponseBody.Trim()
IF (lnSuccess <> 1) THEN
    ? "Could not inspect response."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

IF (loResponseBody.StartsWith("{",1) <> 1) THEN
    ? "Expected a JSON object response."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

lnSuccess = loResult.LoadSb(loResponseBody)
IF (lnSuccess <> 1) THEN
    ? "Expected well-formed JSON."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

lnResultsType = loResult.JsonTypeOf("Results")
IF ((lnResultsType = -1) OR (lnResultsType = 6)) THEN
    ? "No recent redrive tasks returned."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

IF (lnResultsType <> 4) THEN
    ? "Expected Results array."
    RELEASE loTextCompare
    RELEASE loCredentials
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loBody
    RELEASE loResult
    RELEASE loRequestBody
    RELEASE loResponseBody
    CANCEL
ENDIF

i = 0
DO WHILE (i < loResult.SizeOfArray("Results"))
    loResult.I = i
    loTextCompare.Clear()
    loTextCompare.SetString(loResult.StringOf("Results[i].Status"))
    lnSameText13 = loTextCompare.ContentsEqual("",1)
    IF ((loResult.JsonTypeOf("Results[i].Status") <> 1) OR (lnSameText13 = 1)) THEN
        ? "Missing or invalid Results[i].Status."
        RELEASE loTextCompare
        RELEASE loCredentials
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loBody
        RELEASE loResult
        RELEASE loRequestBody
        RELEASE loResponseBody
        CANCEL
    ENDIF

    ? "Status: " + loResult.StringOf("Results[i].Status")
    *  TaskHandle can be absent for tasks that are no longer running.
    IF (loResult.JsonTypeOf("Results[i].TaskHandle") = 1) THEN
        ? "Task handle: " + loResult.StringOf("Results[i].TaskHandle")
    ENDIF

    IF (loResult.JsonTypeOf("Results[i].ApproximateNumberOfMessagesMoved") = 2) THEN
        *  Use text to preserve counts beyond a 32-bit int.
        ? "Approximate moved count: " + loResult.StringOf("Results[i].ApproximateNumberOfMessagesMoved")
    ENDIF

    IF (loResult.JsonTypeOf("Results[i].ApproximateNumberOfMessagesToMove") = 2) THEN
        ? "Approximate total: " + loResult.StringOf("Results[i].ApproximateNumberOfMessagesToMove")
    ENDIF

    IF (loResult.JsonTypeOf("Results[i].StartedTimestamp") = 2) THEN
        ? "Started timestamp: " + loResult.StringOf("Results[i].StartedTimestamp")
    ENDIF

    *  FailureReason can contain sensitive details; inspect privately if status is FAILED.
    i = i + 1
ENDDO
? "Recent redrive tasks inspected. This API returns at most ten recent tasks."

RELEASE loTextCompare
RELEASE loCredentials
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loBody
RELEASE loResult
RELEASE loRequestBody
RELEASE loResponseBody