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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoTextCompare
String sOperation
String sSourceArn
String sDestinationArn
String sTaskHandle
Boolean iSameText1
Boolean iSameText2
Boolean iSameText3
Handle hoCredentials
Boolean iSameText4
Boolean iSameText5
String sRegion
Handle hoEndpoint
Variant vAws
Handle hoAws
Handle hoRest
Boolean iSameText6
Variant vConnection
Handle hoConnection
Handle hoBody
Handle hoResult
Variant vRequestBody
Handle hoRequestBody
Variant vResponseBody
Handle hoResponseBody
Boolean iSameText7
Boolean iSameText8
Boolean iSameText9
Boolean iSameText10
Boolean iSameText11
Boolean iSameText12
Integer iResultsType
Integer i
Boolean iSameText13
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// StringBuilder compares text by value in every generated language.
Get Create (RefClass(cComChilkatStringBuilder)) To hoTextCompare
If (Not(IsComObjectCreated(hoTextCompare))) Begin
Send CreateComObject of hoTextCompare
End
// list is read-only. Choose start or cancel deliberately to change message movement.
Move "list" To sOperation
Move "arn:aws:sqs:us-west-2:123456789012:ExampleDeadLetterQueue" To sSourceArn
Move "" To sDestinationArn
Move "TASK_HANDLE_FROM_START_OR_LIST" To sTaskHandle
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sOperation To iSuccess
Get ComContentsEqual Of hoTextCompare "list" True To iSameText1
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sOperation To iSuccess
Get ComContentsEqual Of hoTextCompare "start" True To iSameText2
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sOperation To iSuccess
Get ComContentsEqual Of hoTextCompare "cancel" True To iSameText3
If ((iSameText1 <> True) And (iSameText2 <> True) And (iSameText3 <> True)) Begin
Showln "Choose list, start, or cancel."
Procedure_Return
End
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Move False To iSuccess
// Private configuration, not a file in public web content or source control.
// Load current IAM credentials authorized for the SQS operation.
Get Create (RefClass(cComChilkatJsonObject)) To hoCredentials
If (Not(IsComObjectCreated(hoCredentials))) Begin
Send CreateComObject of hoCredentials
End
Set ComStrict Of hoCredentials To True
Get ComLoadFile Of hoCredentials "qa_data/aws/sqs.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private AWS credentials."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText4
Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
If ((iTemp1 <> 1) Or (iSameText4 = True)) Begin
Showln "Missing or invalid access_key."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText5
Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
If ((iTemp1 <> 1) Or (iSameText5 = True)) Begin
Showln "Missing or invalid secret_key."
Procedure_Return
End
// Set this once to the region containing the queue.
Move "us-west-2" To sRegion
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint "sqs." To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get ComAppend Of hoEndpoint sRegion To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get Create (RefClass(cComChilkatAuthAws)) To hoAws
If (Not(IsComObjectCreated(hoAws))) Begin
Send CreateComObject of hoAws
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Set ComAccessKey Of hoAws To sTemp1
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Set ComSecretKey Of hoAws To sTemp1
Set ComRegion Of hoAws To sRegion
Set ComServiceName Of hoAws To "sqs"
Set ComSignatureVersion Of hoAws To 4
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
Set ComConnectTimeoutMs Of hoRest To 20000
Set ComIdleTimeoutMs Of hoRest To 60000
Get pvComObject of hoAws to vAws
Get ComSetAuthAws Of hoRest vAws To iSuccess
If (iSuccess <> True) Begin
Showln "Could not configure AWS signing."
Procedure_Return
End
// 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.
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
If (iTemp1 <> 1) Begin
Showln "session_token must be a string."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText6
If (iSameText6 <> True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComAddQueryParam Of hoRest "X-Amz-Security-Token" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set the session-token parameter."
Procedure_Return
End
End
End
// Supply a Socket to explicitly enable TLS certificate verification.
Get Create (RefClass(cComChilkatSocket)) To hoConnection
If (Not(IsComObjectCreated(hoConnection))) Begin
Send CreateComObject of hoConnection
End
Set ComRequireSslCertVerify Of hoConnection To True
Set ComMaxReadIdleMs Of hoConnection To 60000
Set ComMaxSendIdleMs Of hoConnection To 60000
Get ComGetAsString Of hoEndpoint To sTemp1
Get ComConnect Of hoConnection sTemp1 443 True 20000 To iSuccess
If (iSuccess <> True) Begin
Showln "TLS connection failed; inspect diagnostics privately."
Procedure_Return
End
// Do not automatically reconnect/replay a write or receive after a lost response.
Get pvComObject of hoConnection to vConnection
Get ComUseConnection Of hoRest vConnection False To iSuccess
If (iSuccess <> True) Begin
Showln "Could not attach the TLS connection."
Procedure_Return
End
Get ComAddHeader Of hoRest "Content-Type" "application/x-amz-json-1.0" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set the content type."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoBody
If (Not(IsComObjectCreated(hoBody))) Begin
Send CreateComObject of hoBody
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Set ComStrict Of hoResult To True
Get Create (RefClass(cComChilkatStringBuilder)) To hoRequestBody
If (Not(IsComObjectCreated(hoRequestBody))) Begin
Send CreateComObject of hoRequestBody
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoResponseBody
If (Not(IsComObjectCreated(hoResponseBody))) Begin
Send CreateComObject of hoResponseBody
End
Get ComAddHeader Of hoRest "X-Amz-Target" "AmazonSQS.ListMessageMoveTasks" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sOperation To iSuccess
Get ComContentsEqual Of hoTextCompare "start" True To iSameText7
If (iSameText7 = True) Begin
Get ComAddHeader Of hoRest "X-Amz-Target" "AmazonSQS.StartMessageMoveTask" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get ComUpdateString Of hoBody "SourceArn" sSourceArn To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get ComUpdateInt Of hoBody "MaxNumberOfMessagesPerSecond" 10 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sDestinationArn To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText8
If (iSameText8 <> True) Begin
Get ComUpdateString Of hoBody "DestinationArn" sDestinationArn To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
End
// EmitSb appends, so clear both builders before each request/page.
Send ComClear To hoRequestBody
Send ComClear To hoResponseBody
Get pvComObject of hoRequestBody to vRequestBody
Get ComEmitSb Of hoBody vRequestBody To iSuccess
If (iSuccess <> True) Begin
Showln "Could not serialize JSON."
Procedure_Return
End
Get pvComObject of hoRequestBody to vRequestBody
Get pvComObject of hoResponseBody to vResponseBody
Get ComFullRequestSb Of hoRest "POST" "/" vRequestBody vResponseBody To iSuccess
If (iSuccess <> True) Begin
Showln "Request did not complete; inspect diagnostics privately."
// A write or receive may have reached SQS. Do not blindly replay it.
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "SQS HTTP status: " iTemp1
Get ComResponseHdrByName Of hoRest "x-amzn-RequestId" To sTemp1
Showln "AWS request ID: " sTemp1
Get pvComObject of hoResponseBody to vResponseBody
Get ComLoadSb Of hoResult vResponseBody To iSuccess
Get ComJsonTypeOf Of hoResult "__type" To iTemp1
If ((iSuccess = True) And (iTemp1 = 1)) Begin
Get ComStringOf Of hoResult "__type" To sTemp1
Showln "AWS error type: " sTemp1
End
Else Begin
Get ComResponseHdrByName Of hoRest "x-amzn-errortype" To sTemp1
Showln "AWS error type: " sTemp1
End
// Error messages, payloads, receipt handles and signed URLs can be private.
// HTTP 400 can be throttling, expired credentials, or invalid parameters.
Procedure_Return
End
Get ComTrim Of hoResponseBody To iSuccess
If (iSuccess <> True) Begin
Showln "Could not inspect response."
Procedure_Return
End
Get ComStartsWith Of hoResponseBody "{" True To bTemp1
If (bTemp1 <> True) Begin
Showln "Expected a JSON object response."
Procedure_Return
End
Get pvComObject of hoResponseBody to vResponseBody
Get ComLoadSb Of hoResult vResponseBody To iSuccess
If (iSuccess <> True) Begin
Showln "Expected well-formed JSON."
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComStringOf Of hoResult "TaskHandle" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText9
Get ComJsonTypeOf Of hoResult "TaskHandle" To iTemp1
If ((iTemp1 <> 1) Or (iSameText9 = True)) Begin
Showln "Missing or invalid TaskHandle."
Procedure_Return
End
Get ComStringOf Of hoResult "TaskHandle" To sTemp1
Showln "Redrive started. Task handle: " sTemp1
// Save the handle privately; use list mode later to inspect progress.
Procedure_Return
End
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sOperation To iSuccess
Get ComContentsEqual Of hoTextCompare "cancel" True To iSameText10
If (iSameText10 = True) Begin
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sTaskHandle To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText11
Send ComClear To hoTextCompare
Get ComSetString Of hoTextCompare sTaskHandle To iSuccess
Get ComContentsEqual Of hoTextCompare "TASK_HANDLE_FROM_START_OR_LIST" True To iSameText12
If ((iSameText11 = True) Or (iSameText12 = True)) Begin
Showln "Supply taskHandle from the application before running."
Procedure_Return
End
Get ComAddHeader Of hoRest "X-Amz-Target" "AmazonSQS.CancelMessageMoveTask" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get ComUpdateString Of hoBody "TaskHandle" sTaskHandle To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
// EmitSb appends, so clear both builders before each request/page.
Send ComClear To hoRequestBody
Send ComClear To hoResponseBody
Get pvComObject of hoRequestBody to vRequestBody
Get ComEmitSb Of hoBody vRequestBody To iSuccess
If (iSuccess <> True) Begin
Showln "Could not serialize JSON."
Procedure_Return
End
Get pvComObject of hoRequestBody to vRequestBody
Get pvComObject of hoResponseBody to vResponseBody
Get ComFullRequestSb Of hoRest "POST" "/" vRequestBody vResponseBody To iSuccess
If (iSuccess <> True) Begin
Showln "Request did not complete; inspect diagnostics privately."
// A write or receive may have reached SQS. Do not blindly replay it.
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "SQS HTTP status: " iTemp1
Get ComResponseHdrByName Of hoRest "x-amzn-RequestId" To sTemp1
Showln "AWS request ID: " sTemp1
Get pvComObject of hoResponseBody to vResponseBody
Get ComLoadSb Of hoResult vResponseBody To iSuccess
Get ComJsonTypeOf Of hoResult "__type" To iTemp1
If ((iSuccess = True) And (iTemp1 = 1)) Begin
Get ComStringOf Of hoResult "__type" To sTemp1
Showln "AWS error type: " sTemp1
End
Else Begin
Get ComResponseHdrByName Of hoRest "x-amzn-errortype" To sTemp1
Showln "AWS error type: " sTemp1
End
// Error messages, payloads, receipt handles and signed URLs can be private.
// HTTP 400 can be throttling, expired credentials, or invalid parameters.
Procedure_Return
End
Get ComTrim Of hoResponseBody To iSuccess
If (iSuccess <> True) Begin
Showln "Could not inspect response."
Procedure_Return
End
Get ComStartsWith Of hoResponseBody "{" True To bTemp1
If (bTemp1 <> True) Begin
Showln "Expected a JSON object response."
Procedure_Return
End
Get pvComObject of hoResponseBody to vResponseBody
Get ComLoadSb Of hoResult vResponseBody To iSuccess
If (iSuccess <> True) Begin
Showln "Expected well-formed JSON."
Procedure_Return
End
Get ComJsonTypeOf Of hoResult "ApproximateNumberOfMessagesMoved" To iTemp1
If (iTemp1 <> 2) Begin
Showln "Expected approximate moved count."
Procedure_Return
End
Showln "Cancellation accepted; messages already moved remain at their destination."
Get ComStringOf Of hoResult "ApproximateNumberOfMessagesMoved" To sTemp1
Showln "Approximate messages moved: " sTemp1
Procedure_Return
End
Get ComUpdateString Of hoBody "SourceArn" sSourceArn To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
Get ComUpdateInt Of hoBody "MaxResults" 10 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not prepare the request."
Procedure_Return
End
// EmitSb appends, so clear both builders before each request/page.
Send ComClear To hoRequestBody
Send ComClear To hoResponseBody
Get pvComObject of hoRequestBody to vRequestBody
Get ComEmitSb Of hoBody vRequestBody To iSuccess
If (iSuccess <> True) Begin
Showln "Could not serialize JSON."
Procedure_Return
End
Get pvComObject of hoRequestBody to vRequestBody
Get pvComObject of hoResponseBody to vResponseBody
Get ComFullRequestSb Of hoRest "POST" "/" vRequestBody vResponseBody To iSuccess
If (iSuccess <> True) Begin
Showln "Request did not complete; inspect diagnostics privately."
// A write or receive may have reached SQS. Do not blindly replay it.
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "SQS HTTP status: " iTemp1
Get ComResponseHdrByName Of hoRest "x-amzn-RequestId" To sTemp1
Showln "AWS request ID: " sTemp1
Get pvComObject of hoResponseBody to vResponseBody
Get ComLoadSb Of hoResult vResponseBody To iSuccess
Get ComJsonTypeOf Of hoResult "__type" To iTemp1
If ((iSuccess = True) And (iTemp1 = 1)) Begin
Get ComStringOf Of hoResult "__type" To sTemp1
Showln "AWS error type: " sTemp1
End
Else Begin
Get ComResponseHdrByName Of hoRest "x-amzn-errortype" To sTemp1
Showln "AWS error type: " sTemp1
End
// Error messages, payloads, receipt handles and signed URLs can be private.
// HTTP 400 can be throttling, expired credentials, or invalid parameters.
Procedure_Return
End
Get ComTrim Of hoResponseBody To iSuccess
If (iSuccess <> True) Begin
Showln "Could not inspect response."
Procedure_Return
End
Get ComStartsWith Of hoResponseBody "{" True To bTemp1
If (bTemp1 <> True) Begin
Showln "Expected a JSON object response."
Procedure_Return
End
Get pvComObject of hoResponseBody to vResponseBody
Get ComLoadSb Of hoResult vResponseBody To iSuccess
If (iSuccess <> True) Begin
Showln "Expected well-formed JSON."
Procedure_Return
End
Get ComJsonTypeOf Of hoResult "Results" To iResultsType
If ((iResultsType = -1) Or (iResultsType = 6)) Begin
Showln "No recent redrive tasks returned."
Procedure_Return
End
If (iResultsType <> 4) Begin
Showln "Expected Results array."
Procedure_Return
End
Move 0 To i
While (i < (ComSizeOfArray(hoResult, "Results")))
Set ComI Of hoResult To i
Send ComClear To hoTextCompare
Get ComStringOf Of hoResult "Results[i].Status" To sTemp1
Get ComSetString Of hoTextCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoTextCompare "" True To iSameText13
Get ComJsonTypeOf Of hoResult "Results[i].Status" To iTemp1
If ((iTemp1 <> 1) Or (iSameText13 = True)) Begin
Showln "Missing or invalid Results[i].Status."
Procedure_Return
End
Get ComStringOf Of hoResult "Results[i].Status" To sTemp1
Showln "Status: " sTemp1
// TaskHandle can be absent for tasks that are no longer running.
Get ComJsonTypeOf Of hoResult "Results[i].TaskHandle" To iTemp1
If (iTemp1 = 1) Begin
Get ComStringOf Of hoResult "Results[i].TaskHandle" To sTemp1
Showln "Task handle: " sTemp1
End
Get ComJsonTypeOf Of hoResult "Results[i].ApproximateNumberOfMessagesMoved" To iTemp1
If (iTemp1 = 2) Begin
// Use text to preserve counts beyond a 32-bit int.
Get ComStringOf Of hoResult "Results[i].ApproximateNumberOfMessagesMoved" To sTemp1
Showln "Approximate moved count: " sTemp1
End
Get ComJsonTypeOf Of hoResult "Results[i].ApproximateNumberOfMessagesToMove" To iTemp1
If (iTemp1 = 2) Begin
Get ComStringOf Of hoResult "Results[i].ApproximateNumberOfMessagesToMove" To sTemp1
Showln "Approximate total: " sTemp1
End
Get ComJsonTypeOf Of hoResult "Results[i].StartedTimestamp" To iTemp1
If (iTemp1 = 2) Begin
Get ComStringOf Of hoResult "Results[i].StartedTimestamp" To sTemp1
Showln "Started timestamp: " sTemp1
End
// FailureReason can contain sensitive details; inspect privately if status is FAILED.
Move (i + 1) To i
Loop
Showln "Recent redrive tasks inspected. This API returns at most ten recent tasks."
End_Procedure