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

Amazon SQS: Send FIFO Messages with Grouping and Deduplication

See more Amazon SQS Examples

Send to an existing FIFO queue with a MessageGroupId and explicit MessageDeduplicationId. Preserve the group, deduplication ID, and body for retries of the same logical message; generate a new deduplication ID for new work. The constant values here illustrate one logical send and must not be reused for every new message.

SQS suppresses repeat sends with the same deduplication ID within its five-minute interval. A retry after that interval can create another message. Content-based deduplication, when enabled instead, hashes the body but not attributes. Consumers still need idempotent processing because a message can be delivered again after visibility expires. The response body MD5 is checked. MessageId and the potentially 128-bit SequenceNumber remain strings.

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 IAM action: sqs:SendMessage. Scope access to the intended queue. Customer-managed KMS encryption can require additional KMS permissions for producers and consumers.

AWS SendMessage reference

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_TextCompare
string ls_MessageGroupId
string ls_DeduplicationId
string ls_MessageBody
oleobject loo_Credentials
long li_SameText1
long li_SameText2
string ls_Region
oleobject loo_Endpoint
string ls_QueueUrl
long li_SameText3
oleobject loo_ExpectedPrefix
oleobject loo_QueueText
oleobject loo_Aws
oleobject loo_Rest
long li_SameText4
oleobject loo_Connection
oleobject loo_Body
oleobject loo_Result
oleobject loo_RequestBody
oleobject loo_ResponseBody
long li_SameText5
long li_SameText6
long li_SameText7
oleobject loo_Digest
string ls_LocalMd5
oleobject loo_ReturnedMd5

li_Success = 0

//  StringBuilder compares text by value in every generated language.
loo_TextCompare = create oleobject
li_rc = loo_TextCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_TextCompare
    MessageBox("Error","Connecting to COM object failed")
    return
end if
//  Persist these with the logical message. Reuse them only when retrying that SAME send.
ls_MessageGroupId = "group-1"
ls_DeduplicationId = "logical-message-0001"
ls_MessageBody = "Example FIFO message."
//  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 SQS operation.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")

loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/sqs.json")
if li_Success <> 1 then
    Write-Debug "Could not load private AWS credentials."
    destroy loo_TextCompare
    destroy loo_Credentials
    return
end if

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

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

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

li_Success = loo_Endpoint.Append("sqs.")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append(ls_Region)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

li_Success = loo_Endpoint.Append(".amazonaws.com")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

//  Use the exact QueueUrl returned by CreateQueue, GetQueueUrl, or ListQueues.
//  This example targets a standard commercial regional endpoint, not a custom endpoint.
ls_QueueUrl = loo_Credentials.StringOf("queue_url")
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_QueueUrl)
li_SameText3 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("queue_url") <> 1) OR (li_SameText3 = 1) then
    Write-Debug "Missing or invalid queue_url in sqs.json."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    return
end if

loo_ExpectedPrefix = create oleobject
li_rc = loo_ExpectedPrefix.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_ExpectedPrefix.Append("https://")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    return
end if

li_Success = loo_ExpectedPrefix.Append(loo_Endpoint.GetAsString())
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    return
end if

li_Success = loo_ExpectedPrefix.Append("/")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    return
end if

loo_QueueText = create oleobject
li_rc = loo_QueueText.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_QueueText.Append(ls_QueueUrl)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    return
end if

if loo_QueueText.StartsWith(loo_ExpectedPrefix.GetAsString(),1) <> 1 then
    Write-Debug "Queue URL must use HTTPS and the configured SQS region."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    return
end if

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 = "sqs"
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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    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_TextCompare
        destroy loo_Credentials
        destroy loo_Endpoint
        destroy loo_ExpectedPrefix
        destroy loo_QueueText
        destroy loo_Aws
        destroy loo_Rest
        return
    end if

    loo_TextCompare.Clear()
    loo_TextCompare.SetString(loo_Credentials.StringOf("session_token"))
    li_SameText4 = loo_TextCompare.ContentsEqual("",1)
    if li_SameText4 <> 1 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_TextCompare
            destroy loo_Credentials
            destroy loo_Endpoint
            destroy loo_ExpectedPrefix
            destroy loo_QueueText
            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_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

//  Do not automatically reconnect/replay a write or receive after a lost response.
li_Success = loo_Rest.UseConnection(loo_Connection,0)
if li_Success <> 1 then
    Write-Debug "Could not attach the TLS connection."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

li_Success = loo_Rest.AddHeader("Content-Type","application/x-amz-json-1.0")
if li_Success <> 1 then
    Write-Debug "Could not set the content type."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

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

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

loo_Result.Strict = 1
loo_RequestBody = create oleobject
li_rc = loo_RequestBody.ConnectToNewObject("Chilkat.StringBuilder")

loo_ResponseBody = create oleobject
li_rc = loo_ResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Rest.AddHeader("X-Amz-Target","AmazonSQS.SendMessage")
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

li_Success = loo_Body.UpdateString("QueueUrl",ls_QueueUrl)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

//  Use the queue URL from Create a FIFO Queue. No per-message DelaySeconds for FIFO.
li_Success = loo_Body.UpdateString("MessageGroupId",ls_MessageGroupId)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

li_Success = loo_Body.UpdateString("MessageDeduplicationId",ls_DeduplicationId)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

li_Success = loo_Body.UpdateString("MessageBody",ls_MessageBody)
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

//  EmitSb appends, so clear both builders before each request/page.
loo_RequestBody.Clear()
loo_ResponseBody.Clear()
li_Success = loo_Body.EmitSb(loo_RequestBody)
if li_Success <> 1 then
    Write-Debug "Could not serialize JSON."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

li_Success = loo_Rest.FullRequestSb("POST","/",loo_RequestBody,loo_ResponseBody)
if li_Success <> 1 then
    Write-Debug "Request did not complete; inspect diagnostics privately."
    //  A write or receive may have reached SQS. Do not blindly replay it.
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "SQS HTTP status: " + string(loo_Rest.ResponseStatusCode)
    Write-Debug "AWS request ID: " + loo_Rest.ResponseHdrByName("x-amzn-RequestId")
    li_Success = loo_Result.LoadSb(loo_ResponseBody)
    if (li_Success = 1) AND (loo_Result.JsonTypeOf("__type") = 1) then
        Write-Debug "AWS error type: " + loo_Result.StringOf("__type")
    else
        Write-Debug "AWS error type: " + loo_Rest.ResponseHdrByName("x-amzn-errortype")
    end if

    //  Error messages, payloads, receipt handles and signed URLs can be private.
    //  HTTP 400 can be throttling, expired credentials, or invalid parameters.
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

li_Success = loo_ResponseBody.Trim()
if li_Success <> 1 then
    Write-Debug "Could not inspect response."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

if loo_ResponseBody.StartsWith("{",1) <> 1 then
    Write-Debug "Expected a JSON object response."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

li_Success = loo_Result.LoadSb(loo_ResponseBody)
if li_Success <> 1 then
    Write-Debug "Expected well-formed JSON."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Result.StringOf("MessageId"))
li_SameText5 = loo_TextCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("MessageId") <> 1) OR (li_SameText5 = 1) then
    Write-Debug "Missing or invalid MessageId."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Result.StringOf("SequenceNumber"))
li_SameText6 = loo_TextCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("SequenceNumber") <> 1) OR (li_SameText6 = 1) then
    Write-Debug "Missing or invalid SequenceNumber."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Result.StringOf("MD5OfMessageBody"))
li_SameText7 = loo_TextCompare.ContentsEqual("",1)
if (loo_Result.JsonTypeOf("MD5OfMessageBody") <> 1) OR (li_SameText7 = 1) then
    Write-Debug "Missing or invalid MD5OfMessageBody."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    return
end if

loo_Digest = create oleobject
li_rc = loo_Digest.ConnectToNewObject("Chilkat.Crypt2")

loo_Digest.Charset = "utf-8"
loo_Digest.HashAlgorithm = "md5"
loo_Digest.EncodingMode = "hex"
ls_LocalMd5 = loo_Digest.HashStringENC(ls_MessageBody)
if loo_Digest.LastMethodSuccess <> 1 then
    Write-Debug "Could not hash the message body."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    destroy loo_Digest
    return
end if

loo_ReturnedMd5 = create oleobject
li_rc = loo_ReturnedMd5.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_ReturnedMd5.Append(loo_Result.StringOf("MD5OfMessageBody"))
if li_Success <> 1 then
    Write-Debug "Could not prepare the request."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    destroy loo_Digest
    destroy loo_ReturnedMd5
    return
end if

if loo_ReturnedMd5.ContentsEqual(ls_LocalMd5,0) <> 1 then
    Write-Debug "Body MD5 mismatch; reconcile this send before retrying."
    destroy loo_TextCompare
    destroy loo_Credentials
    destroy loo_Endpoint
    destroy loo_ExpectedPrefix
    destroy loo_QueueText
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Body
    destroy loo_Result
    destroy loo_RequestBody
    destroy loo_ResponseBody
    destroy loo_Digest
    destroy loo_ReturnedMd5
    return
end if

Write-Debug "FIFO message accepted. Message ID: " + loo_Result.StringOf("MessageId")
//  FIFO sequence numbers can exceed 64 bits. Keep the exact string.
Write-Debug "Sequence number: " + loo_Result.StringOf("SequenceNumber")


destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_ExpectedPrefix
destroy loo_QueueText
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Body
destroy loo_Result
destroy loo_RequestBody
destroy loo_ResponseBody
destroy loo_Digest
destroy loo_ReturnedMd5