Skip to main content

Queue

caution

This is the SST v0.x Constructs doc. SST v1 is now released. If you are using v1, see the v1 Constructs doc. If you are looking to upgrade to v1, check out the migration steps.

The Queue construct is a higher level CDK construct that makes it easy to create a SQS Queues. You can create a queue by specifying a consumer function. And then publish to the queue from any part of your serverless app.

This construct makes it easier to define a queue and a consumer. It also internally connects the consumer and queue together.

Initializer

new Queue(scope: Construct, id: string, props: QueueProps)

Parameters

Examples

Using the minimal config

import { Queue } from "@serverless-stack/resources";

new Queue(this, "Queue", {
consumer: "src/queueConsumer.main",
});

Lazily adding consumer

Create an empty queue and lazily add the consumer.

const queue = new Queue(this, "Queue");

queue.addConsumer(this, "src/queueConsumer.main");

Giving the consumer some permissions

Allow the consumer function to access S3.

const queue = new Queue(this, "Queue", {
consumer: "src/queueConsumer.main",
});

queue.attachPermissions(["s3"]);

Creating a FIFO queue

new Queue(this, "Queue", {
consumer: "src/queueConsumer.main",
sqsQueue: {
fifo: true,
},
});

Configuring the SQS queue

Configure the internally created CDK Queue instance.

import { Duration } from "aws-cdk-lib";

new Queue(this, "Queue", {
consumer: "src/queueConsumer.main",
sqsQueue: {
queueName: "my-queue",
visibilityTimeout: Duration.seconds(5),
},
});

Configuring the consumer

Configuring the function props

new Queue(this, "Queue", {
consumer: {
function: {
handler: "src/queueConsumer.main",
timeout: 10,
environment: { bucketName: bucket.bucketName },
permissions: [bucket],
},
},
});

Configuring the consumption props

Configure the internally created CDK Event Source.

new Queue(this, "Queue", {
consumer: {
function: "src/queueConsumer.main",
consumerProps: {
batchSize: 5,
},
},
});

Importing an existing queue

Override the internally created CDK Queue instance.

import { Queue } from "aws-cdk-lib/aws-sqs";

new Queue(this, "Queue", {
consumer: "src/queueConsumer.main",
sqsQueue: Queue.fromQueueArn(this, "MySqsQueue", queueArn),
});

Properties

An instance of Queue contains the following properties.

sqsQueue

Type : cdk.aws-sqs.Queue

The internally created CDK Queue instance.

consumerFunction

Type : Function

The internally created consumer Function instance.

Methods

An instance of Queue contains the following methods.

addConsumer

addConsumer(scope: cdk.Construct, consumer: FunctionDefinition | QueueConsumerProps)

Parameters

  • scope cdk.Construct
  • consumer FunctionDefinition | TopicSubscriberProps

Takes FunctionDefinition or QueueConsumerProps object that'll be used to create the consumer for the queue.

Note that, only 1 consumer can be added to a queue.

attachPermissions

attachPermissions(permissions: Permissions)

Parameters

Attaches the given list of permissions to the consumerFunction. This allows the consumer to access other AWS resources.

Internally calls Function.attachPermissions.

QueueProps

consumer?

Type : FunctionDefinition | QueueConsumerProps, defaults to undefined

Takes FunctionDefinition or QueueConsumerProps object used to create the consumer for the queue.

sqsQueue?

Type : cdk.aws-sqs.Queue | cdk.aws-sqs.QueueProps, defaults to undefined

Or optionally pass in a CDK cdk.aws-sqs.QueueProps or a cdk.aws-sqs.Queue instance. This allows you to override the default settings this construct uses internally to create the queue.

QueueConsumerProps

function

Type : FunctionDefinition

A FunctionDefinition object that'll be used to create the consumer function for the queue.

consumerProps?

Type : cdk.aws-lambda-event-sources.lambdaEventSources.SqsEventSourceProps, defaults to undefined

Or optionally pass in a CDK SqsEventSourceProps. This allows you to override the default settings this construct uses internally to create the consumer.