Amazon SQS: Create a Standard Queue
See more Amazon SQS Examples
Create a standard queue with a 60-second visibility timeout, 20-second default long poll, SQS-managed encryption, and a 1 MiB maximum message size. Queue attribute values in JSON are strings, while request parameters such as MaxResults are numbers. Store the returned QueueUrl as queue_url for the messaging examples.
The same name and matching attributes can return an existing queue; conflicting attributes cause an error rather than changing it. Wait at least one second before using a newly created queue and at least 60 seconds after deletion before reusing its name. Queue type cannot be changed after creation. FIFO queues require a .fifo name and FifoQueue=true, with different send parameters.
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:CreateQueue. Scope access to the intended queue. Customer-managed KMS encryption can require additional KMS permissions for producers and consumers.
Chilkat Delphi DLL Downloads
var
success: Boolean;
textCompare: HCkStringBuilder;
credentials: HCkJsonObject;
sameText1: Boolean;
sameText2: Boolean;
region: PWideChar;
endpoint: HCkStringBuilder;
aws: HCkAuthAws;
rest: HCkRest;
sameText3: Boolean;
connection: HCkSocket;
body: HCkJsonObject;
result: HCkJsonObject;
requestBody: HCkStringBuilder;
responseBody: HCkStringBuilder;
sameText4: Boolean;
begin
success := False;
// StringBuilder compares text by value in every generated language.
textCompare := CkStringBuilder_Create();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
// Private configuration, not a file in public web content or source control.
// Load current IAM credentials authorized for the SQS operation.
credentials := CkJsonObject_Create();
CkJsonObject_putStrict(credentials,True);
success := CkJsonObject_LoadFile(credentials,'qa_data/aws/sqs.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load private AWS credentials.');
Exit;
end;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkJsonObject__stringOf(credentials,'access_key'));
sameText1 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'access_key') <> 1) or (sameText1 = True)) then
begin
Memo1.Lines.Add('Missing or invalid access_key.');
Exit;
end;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkJsonObject__stringOf(credentials,'secret_key'));
sameText2 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'secret_key') <> 1) or (sameText2 = True)) then
begin
Memo1.Lines.Add('Missing or invalid secret_key.');
Exit;
end;
// Set this once to the region containing the queue.
region := 'us-west-2';
endpoint := CkStringBuilder_Create();
success := CkStringBuilder_Append(endpoint,'sqs.');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkStringBuilder_Append(endpoint,region);
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkStringBuilder_Append(endpoint,'.amazonaws.com');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
aws := CkAuthAws_Create();
CkAuthAws_putAccessKey(aws,CkJsonObject__stringOf(credentials,'access_key'));
CkAuthAws_putSecretKey(aws,CkJsonObject__stringOf(credentials,'secret_key'));
CkAuthAws_putRegion(aws,region);
CkAuthAws_putServiceName(aws,'sqs');
CkAuthAws_putSignatureVersion(aws,4);
rest := CkRest_Create();
CkRest_putConnectTimeoutMs(rest,20000);
CkRest_putIdleTimeoutMs(rest,60000);
success := CkRest_SetAuthAws(rest,aws);
if (success <> True) then
begin
Memo1.Lines.Add('Could not configure AWS signing.');
Exit;
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.
if (CkJsonObject_HasMember(credentials,'session_token') = True) then
begin
if (CkJsonObject_JsonTypeOf(credentials,'session_token') <> 1) then
begin
Memo1.Lines.Add('session_token must be a string.');
Exit;
end;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkJsonObject__stringOf(credentials,'session_token'));
sameText3 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if (sameText3 <> True) then
begin
success := CkRest_AddQueryParam(rest,'X-Amz-Security-Token',CkJsonObject__stringOf(credentials,'session_token'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not set the session-token parameter.');
Exit;
end;
end;
end;
// Supply a Socket to explicitly enable TLS certificate verification.
connection := CkSocket_Create();
CkSocket_putRequireSslCertVerify(connection,True);
CkSocket_putMaxReadIdleMs(connection,60000);
CkSocket_putMaxSendIdleMs(connection,60000);
success := CkSocket_Connect(connection,CkStringBuilder__getAsString(endpoint),443,True,20000);
if (success <> True) then
begin
Memo1.Lines.Add('TLS connection failed; inspect diagnostics privately.');
Exit;
end;
// Do not automatically reconnect/replay a write or receive after a lost response.
success := CkRest_UseConnection(rest,connection,False);
if (success <> True) then
begin
Memo1.Lines.Add('Could not attach the TLS connection.');
Exit;
end;
success := CkRest_AddHeader(rest,'Content-Type','application/x-amz-json-1.0');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set the content type.');
Exit;
end;
body := CkJsonObject_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);
requestBody := CkStringBuilder_Create();
responseBody := CkStringBuilder_Create();
success := CkRest_AddHeader(rest,'X-Amz-Target','AmazonSQS.CreateQueue');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkJsonObject_UpdateString(body,'QueueName','ExampleQueue');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkJsonObject_UpdateString(body,'Attributes.VisibilityTimeout','60');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkJsonObject_UpdateString(body,'Attributes.ReceiveMessageWaitTimeSeconds','20');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkJsonObject_UpdateString(body,'Attributes.SqsManagedSseEnabled','true');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkJsonObject_UpdateString(body,'Attributes.MaximumMessageSize','1048576');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
// Queue Attributes are STRING values, even for numbers and booleans.
// No FifoQueue attribute: this creates a STANDARD queue.
// EmitSb appends, so clear both builders before each request/page.
CkStringBuilder_Clear(requestBody);
CkStringBuilder_Clear(responseBody);
success := CkJsonObject_EmitSb(body,requestBody);
if (success <> True) then
begin
Memo1.Lines.Add('Could not serialize JSON.');
Exit;
end;
success := CkRest_FullRequestSb(rest,'POST','/',requestBody,responseBody);
if (success <> True) then
begin
Memo1.Lines.Add('Request did not complete; inspect diagnostics privately.');
// A write or receive may have reached SQS. Do not blindly replay it.
Exit;
end;
if (CkRest_getResponseStatusCode(rest) <> 200) then
begin
Memo1.Lines.Add('SQS HTTP status: ' + IntToStr(CkRest_getResponseStatusCode(rest)));
Memo1.Lines.Add('AWS request ID: ' + CkRest__responseHdrByName(rest,'x-amzn-RequestId'));
success := CkJsonObject_LoadSb(result,responseBody);
if ((success = True) and (CkJsonObject_JsonTypeOf(result,'__type') = 1)) then
begin
Memo1.Lines.Add('AWS error type: ' + CkJsonObject__stringOf(result,'__type'));
end
else
begin
Memo1.Lines.Add('AWS error type: ' + CkRest__responseHdrByName(rest,'x-amzn-errortype'));
end;
// Error messages, payloads, receipt handles and signed URLs can be private.
// HTTP 400 can be throttling, expired credentials, or invalid parameters.
Exit;
end;
success := CkStringBuilder_Trim(responseBody);
if (success <> True) then
begin
Memo1.Lines.Add('Could not inspect response.');
Exit;
end;
if (CkStringBuilder_StartsWith(responseBody,'{',True) <> True) then
begin
Memo1.Lines.Add('Expected a JSON object response.');
Exit;
end;
success := CkJsonObject_LoadSb(result,responseBody);
if (success <> True) then
begin
Memo1.Lines.Add('Expected well-formed JSON.');
Exit;
end;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkJsonObject__stringOf(result,'QueueUrl'));
sameText4 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if ((CkJsonObject_JsonTypeOf(result,'QueueUrl') <> 1) or (sameText4 = True)) then
begin
Memo1.Lines.Add('Missing or invalid QueueUrl.');
Exit;
end;
Memo1.Lines.Add('Queue URL: ' + CkJsonObject__stringOf(result,'QueueUrl'));
Memo1.Lines.Add('Queue created or matching existing queue returned. Wait at least one second before use.');
CkStringBuilder_Dispose(textCompare);
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkStringBuilder_Dispose(requestBody);
CkStringBuilder_Dispose(responseBody);