Skip to main content

AWS SQS

AWS SQS event integrations poll an Amazon SQS queue and trigger a handler function for every message the queue delivers, with a separate handler for listener-level errors. Use them to process jobs from a queue-based pipeline, decouple producer and consumer services, or react to asynchronous work items without writing your own polling loop.

Creating an AWS SQS service​

Listener configuration​

sqs:Listener accepts a sqs:ConnectionConfig and an optional sqs:PollingConfig:

listener sqs:Listener sqsListener = new (connectionConfig, pollingConfig);

sqs:ConnectionConfig

FieldTypeDefaultDescription
authaws:AuthConfigRequiredAWS credential source. Accepts static access key/secret credentials, an AWS profile, STS assume-role, web identity (OIDC), IAM Identity Center (SSO), an external credential process, or the default credential provider chain
regionaws:Region|stringRequiredAWS region the queue belongs to, for example aws:US_EAST_1 or "us-east-1"
endpointaws:EndpointConfig?—Custom AWS endpoint configuration, for example a FIPS or dual-stack endpoint

sqs:PollingConfig

FieldTypeDefaultDescription
pollIntervaldecimal1Seconds to wait between polling attempts
waitTimeint20Long-polling wait time, in seconds, for each receive request
visibilityTimeoutint30Seconds a received message stays invisible to other consumers while its handler runs

Each service attached to the listener targets a specific queue through the @sqs:ServiceConfig annotation:

FieldTypeDefaultDescription
queueUrlstringRequiredURL of the SQS queue this service consumes messages from
configsqs:PollingConfig?—Per-service polling overrides
autoDeletebooleantrueAutomatically deletes a message from the queue once onMessage returns without an error. Set to false to delete messages manually with a sqs:Caller

Event handling​

An AWS SQS service implements two remote functions:

FunctionTriggered whenParameters
onMessageA message is received from the queuesqs:Message message, plus an optional sqs:Caller caller when manual deletion is needed
onErrorThe listener fails while polling the queue or processing a messagesqs:Error err

The sqs:Message value passed to onMessage carries the message content and metadata:

FieldTypeDescription
messageIdstring?Unique identifier assigned to the message
bodystring?Message content
receiptHandlestring?Token used to delete the message or change its visibility timeout
messageAttributesmap<sqs:MessageAttributeValue>?Custom attributes sent with the message
messageSystemAttributessqs:MessageAttributes?AWS system attributes, such as sender ID, sent timestamp, and approximate receive count
md5OfBodystring?MD5 checksum of the message body
md5OfMessageAttributesstring?MD5 checksum of the message attributes

When autoDelete is set to false on the @sqs:ServiceConfig annotation, add a sqs:Caller parameter to onMessage and call caller->delete() once processing succeeds, so the message is only removed from the queue after your logic completes:

remote function onMessage(sqs:Message message, sqs:Caller caller) returns error? {
log:printInfo("Processing message", body = message.body ?: "");
check caller->delete();
}

If onMessage returns without deleting the message, the message becomes visible again once visibilityTimeout elapses and is redelivered to the listener.

What's next​

  • Kafka — consume messages from Apache Kafka topics
  • RabbitMQ — consume messages from RabbitMQ queues
  • Connections — reuse credentials across services