Amazon SQS: Update Queue Configuration
See more Amazon SQS Examples
Choose one update: processing sets visibility and long polling, retention sets message lifetime, and kms selects a customer-managed KMS key while disabling SQS-managed encryption. All attribute values are strings. Only the supplied attributes are updated; the example does not replace the queue policy or change queue type.
Most attributes can take up to 60 seconds to propagate; retention can take up to 15 minutes. Reducing retention may delete existing messages that are too old. KMS mode needs a suitable key ARN in the same region, key policy, and producer/consumer permissions. Previously encrypted messages retain their original key requirements. Keep the old key available until those messages are consumed. Reread attributes after propagation; HTTP 200 is acceptance, not proof every consumer sees the new settings.
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:SetQueueAttributes. Scope access to the intended queue. Customer-managed KMS encryption can require additional KMS permissions for producers and consumers.
AWS SetQueueAttributes reference
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_TextCompare
string ls_Operation
long li_SameText1
long li_SameText2
long li_SameText3
oleobject loo_Credentials
long li_SameText4
long li_SameText5
string ls_Region
oleobject loo_Endpoint
string ls_QueueUrl
long li_SameText6
oleobject loo_ExpectedPrefix
oleobject loo_QueueText
oleobject loo_Aws
oleobject loo_Rest
long li_SameText7
oleobject loo_Connection
oleobject loo_Body
oleobject loo_Result
oleobject loo_RequestBody
oleobject loo_ResponseBody
long li_SameText8
long li_SameText9
string ls_KmsKeyArn
long li_SameText10
long li_SameText11
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
// processing: visibility and long polling; retention: message lifetime; kms: encryption.
ls_Operation = "processing"
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_Operation)
li_SameText1 = loo_TextCompare.ContentsEqual("processing",1)
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_Operation)
li_SameText2 = loo_TextCompare.ContentsEqual("retention",1)
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_Operation)
li_SameText3 = loo_TextCompare.ContentsEqual("kms",1)
if (li_SameText1 <> 1) AND (li_SameText2 <> 1) AND (li_SameText3 <> 1) then
Write-Debug "Choose processing, retention, or kms."
destroy loo_TextCompare
return
end if
// 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_SameText4 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_SameText4 = 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_SameText5 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_SameText5 = 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_SameText6 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("queue_url") <> 1) OR (li_SameText6 = 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_SameText7 = loo_TextCompare.ContentsEqual("",1)
if li_SameText7 <> 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.SetQueueAttributes")
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
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_Operation)
li_SameText8 = loo_TextCompare.ContentsEqual("processing",1)
if li_SameText8 = 1 then
li_Success = loo_Body.UpdateString("Attributes.VisibilityTimeout","60")
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("Attributes.ReceiveMessageWaitTimeSeconds","20")
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
else
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_Operation)
li_SameText9 = loo_TextCompare.ContentsEqual("retention",1)
if li_SameText9 = 1 then
// Reducing this below the age of queued messages can expire/delete those messages.
li_Success = loo_Body.UpdateString("Attributes.MessageRetentionPeriod","345600")
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
else
ls_KmsKeyArn = "YOUR_KMS_KEY_ARN"
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_KmsKeyArn)
li_SameText10 = loo_TextCompare.ContentsEqual("",1)
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_KmsKeyArn)
li_SameText11 = loo_TextCompare.ContentsEqual("YOUR_KMS_KEY_ARN",1)
if (li_SameText10 = 1) OR (li_SameText11 = 1) then
Write-Debug "Supply kmsKeyArn from the application before running."
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("Attributes.KmsMasterKeyId",ls_KmsKeyArn)
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("Attributes.KmsDataKeyReusePeriodSeconds","300")
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("Attributes.SqsManagedSseEnabled","false")
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
end if
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
// This operation normally returns an empty HTTP body on success.
// Also accept an empty JSON object; never require XML for this protocol.
if loo_ResponseBody.Length > 0 then
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
if loo_Result.Size <> 0 then
Write-Debug "Unexpected fields in the empty-result 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
end if
Write-Debug "Queue attribute update accepted. Read attributes later to verify propagation."
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