Skip to main content

Triggers

The ballerinax/aws.sqs connector supports event-driven message consumption through a built-in polling Listener. The Listener periodically retrieves messages from an SQS queue and dispatches them to your service callbacks, eliminating the need for manual polling loops.

Three components work together:

ComponentRole
sqs:ListenerPolls the SQS queue at a configurable interval and dispatches messages to attached services.
sqs:ServiceDefines onMessage and onError callbacks invoked when messages arrive or errors occur.
sqs:CallerProvides a delete() method to manually acknowledge and delete a message within the callback.
sqs:MessageThe message payload passed to the onMessage callback.

For action-based record operations, see the Action Reference.


Listener

The aws.sqs:Listener establishes the connection and manages event subscriptions.

Configuration

The listener supports the following connection strategies:

Config TypeDescription
ConnectionConfigAWS connection configuration for authenticating with SQS.
PollingConfigControls how the Listener polls the SQS queue for messages.

ConnectionConfig fields:

FieldTypeDefaultDescription
regionRegionRequiredAWS region where the SQS queue is located.
authStaticAuthConfig|ProfileAuthConfig|DEFAULT_CREDENTIALSRequiredAuthentication configuration: static credentials, AWS profile, or default credential chain.

PollingConfig fields:

FieldTypeDefaultDescription
pollIntervaldecimal1Interval in seconds between poll requests.
waitTimeint20Long poll wait time in seconds (0–20).
visibilityTimeoutint30Visibility timeout in seconds for received messages.

Initializing the listener

Using static credentials:

import ballerinax/aws.sqs;

configurable string accessKeyId = ?;
configurable string secretAccessKey = ?;

listener sqs:Listener sqsListener = new ({
region: sqs:US_EAST_1,
auth: {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey
}
});

With custom polling configuration:

import ballerinax/aws.sqs;

configurable string accessKeyId = ?;
configurable string secretAccessKey = ?;

listener sqs:Listener sqsListener = new (
{
region: sqs:US_EAST_1,
auth: {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey
}
},
pollingConfig = {
pollInterval: 5,
waitTime: 10,
visibilityTimeout: 60
}
);

Service

An sqs:Service is a Ballerina service attached to an sqs:Listener. It is annotated with @sqs:ServiceConfig to specify the queue URL and optional settings, and implements callback functions for message processing and error handling.

Callback signatures

FunctionSignatureDescription
onMessageremote function onMessage(sqs:Message message, sqs:Caller caller) returns error?Invoked for each message received from the queue.
onErrorremote function onError(sqs:Error err) returns error?Invoked when an error occurs during polling or message processing.
note

The sqs:Caller parameter in onMessage is optional. If autoDelete is set to true (default) in @sqs:ServiceConfig, messages are automatically deleted after onMessage returns successfully.

Full usage example

import ballerina/log;
import ballerinax/aws.sqs;

configurable string accessKeyId = ?;
configurable string secretAccessKey = ?;
configurable string queueUrl = ?;

listener sqs:Listener sqsListener = new ({
region: sqs:US_EAST_1,
auth: {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey
}
});

@sqs:ServiceConfig {
queueUrl: queueUrl
}
service sqs:Service on sqsListener {

remote function onMessage(sqs:Message message, sqs:Caller caller) returns error? {
log:printInfo("Message received",
messageId = message.messageId,
body = message.body
);
// Manually delete the message after processing
check caller->delete();
}

remote function onError(sqs:Error err) returns error? {
log:printError("Error occurred while polling", 'error = err);
}
}
note

You can set autoDelete: false in @sqs:ServiceConfig to manually control message deletion using caller->delete(), which is useful for implementing custom acknowledgment logic.


Supporting types

Message

FieldTypeDescription
messageIdstring?The unique identifier for the message.
bodystring?The message body content.
receiptHandlestring?The receipt handle used to delete or change visibility of the message.
md5OfBodystring?MD5 digest of the message body for integrity verification.
messageAttributesmap<MessageAttributeValue>?Custom message attributes as key-value pairs.
md5OfMessageAttributesstring?MD5 digest of the message attributes.
messageSystemAttributesMessageAttributes?System-level attributes (sender ID, timestamp, etc.).

ServiceConfig

FieldTypeDescription
queueUrlstringThe URL of the SQS queue to consume messages from.
configPollingConfig?Optional per-service polling configuration that overrides the listener-level config.
autoDeletebooleanWhether to automatically delete messages after successful processing (default true).