Skip to content

Queue

Reference doc for the `sst.aws.Queue` component.

The Queue component lets you add a serverless queue to your app. It uses Amazon SQS.

Create a queue

const queue = new sst.aws.Queue("MyQueue");

Make it a FIFO queue

You can optionally make it a FIFO queue.

new sst.aws.Queue("MyQueue", {
fifo: true
});

Add a subscriber

queue.subscribe("src/subscriber.handler");

You can link the queue to other resources, like a function or your Next.js app.

new sst.aws.Nextjs("MyWeb", {
link: [queue]
});

Once linked, you can send messages to the queue from your function code.

app/page.tsx
import { Resource } from "sst";
import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";
const sqs = new SQSClient({});
await sqs.send(new SendMessageCommand({
QueueUrl: Resource.MyQueue.url,
MessageBody: "Hello from Next.js!"
}));

Constructor

new Queue(name, args?, opts?)

Parameters

Methods

subscribe

subscribe(subscriber, args?)

Parameters

Returns QueueSubscriber

Subscribe to this queue.

queue.subscribe("src/subscriber.handler");
Add a filter to the subscription.
```js
queue.subscribe("src/subscriber.handler", {
filters: [
{
body: {
RequestCode: ["BBBB"]
}
}
]
});

Customize the subscriber function.

queue.subscribe({
handler: "src/subscriber.handler",
timeout: "60 seconds"
});

static subscribe

Queue.subscribe(queueArn, subscriber, args?)

Parameters

  • queueArn Input<string>

    The ARN of the SQS queue to subscribe to.
  • subscriber string | FunctionArgs

    The function that’ll be notified.
  • args? QueueSubscribeArgs

    Configure the subscription.

Returns QueueSubscriber

Subscribe to an SQS queue that was not created in your app.

For example, let’s say you have an existing SQS queue with the following ARN.

const queueArn = "arn:aws:sqs:us-east-1:123456789012:MyQueue";

You can subscribe to it by passing in the ARN.

sst.aws.Queue.subscribe(queueArn, "src/subscriber.handler");

Add a filter to the subscription.

sst.aws.Queue.subscribe(queueArn, "src/subscriber.handler", {
filters: [
{
body: {
RequestCode: ["BBBB"]
}
}
]
});

Customize the subscriber function.

sst.aws.Queue.subscribe(queueArn, {
handler: "src/subscriber.handler",
timeout: "60 seconds"
});

Properties

arn

Type Output<string>

The ARN of the SQS queue.

nodes

Type Object

The underlying resources this component creates.

nodes.queue

Type Queue

The Amazon SQS queue.

url

Type Output<string>

The SQS queue URL.

The following are accessible through the SDK at runtime.

url

Type string

The SQS queue URL.

QueueArgs

fifo?

Type Input<boolean>

Default false

FIFO or first-in-first-out queues are designed to guarantee that messages are processed exactly once and in the order that they are sent.

{
fifo: true
}

transform?

Type Object

Transform how this component creates its underlying resources.

transform.queue?

Type QueueArgs | (args: QueueArgs => QueueArgs | void)

Transform the SQS queue resource.

QueueSubscribeArgs

filters?

Type Input<Input<Record<string, any>>[]>

Filter the records that’ll be processed by the subscriber function.

You can pass in up to 5 different filter policies. These will logically ORed together. Meaning that if any single policy matches, the record will be processed. Learn more about the filter rule syntax.

For example, if you Queue contains records in this JSON format.

{
RecordNumber: 0000,
RequestCode: "AAAA",
TimeStamp: "yyyy-mm-ddThh:mm:ss"
}

To process only those records where the RequestCode is BBBB.

{
filters: [
{
body: {
RequestCode: ["BBBB"]
}
}
]
}

And to process only those records where RecordNumber greater than 9999.

{
filters: [
{
body: {
RecordNumber: [{ numeric: [ ">", 9999 ] }]
}
}
]
}

transform?

Type Object

Transform how this component creates its underlying resources.

transform.eventSourceMapping?

Type EventSourceMappingArgs | (args: EventSourceMappingArgs => EventSourceMappingArgs | void)

Transform the Lambda Event Source Mapping resource.

QueueSubscriber

eventSourceMapping

Type Output<EventSourceMapping>

The Lambda event source mapping.

function

Type Output<Function>

The Lambda function that’ll be notified.