Skip to main content

Overview

The SQSQueue construct creates an Amazon SQS queue with optional dead-letter queue, FIFO ordering, and encryption. Use it to decouple producers and consumers in event-driven architectures.

Import

import { SQSQueue } from "@fjall/components-infrastructure/lib/resources/aws/messaging/sqs";

Basic Usage

const queue = new SQSQueue(scope, "Notifications", {});

Using the Messaging Factory

The recommended way to add SQS to a Fjall application is through the MessagingFactory:
import { MessagingFactory } from "@fjall/components-infrastructure/lib/patterns/aws/messaging";

const notifications = app.addMessaging(
  MessagingFactory.build("Notifications", {
    type: "queue",
    queueType: "standard",
    deadLetterQueue: { enabled: true, maxReceiveCount: 3 },
  }),
);

Properties

PropertyTypeDefaultDescription
queueNamestringAuto-generatedQueue name
queueType"standard" | "fifo""standard"Queue type
visibilityTimeoutnumber30Seconds a message is hidden after being read (0-43200)
messageRetentionPeriodnumber345600 (4 days)Seconds to retain messages (60-1209600)
receiveMessageWaitTimenumber0Long polling wait time in seconds (0-20)
maxMessageSizenumber262144Maximum message size in bytes (1-262144)
deliveryDelaynumber0Delay before message becomes visible in seconds (0-900)
deadLetterQueue.enabledbooleanfalseCreate a dead-letter queue
deadLetterQueue.maxReceiveCountnumber3Failed deliveries before moving to DLQ (1-1000)
encryption"SSE_SQS" | "SSE_KMS" | "NONE""SSE_SQS"Encryption type
kmsKeyArnstringKMS key ARN (when using SSE_KMS)
contentBasedDeduplicationbooleantrue (FIFO)Enable content-based deduplication. FIFO only, defaults to true. Ignored for standard queues
fifoThroughputLimit"perQueue" | "perMessageGroupId""perQueue"FIFO throughput limit
deduplicationScope"queue" | "messageGroup""queue"FIFO deduplication scope
removalPolicy"DESTROY" | "RETAIN""RETAIN"What happens on stack deletion

Methods

MethodReturnsDescription
getQueueUrl()stringQueue URL
getQueueArn()stringQueue ARN
getQueueName()stringQueue name
getQueue()IQueueCDK Queue construct
getDeadLetterQueue()IQueue | undefinedDLQ construct (if enabled)
grantSend(grantee)GrantGrant permission to send messages
grantConsume(grantee)GrantGrant permission to receive and delete messages
grantPurge(grantee)GrantGrant permission to purge the queue
grantSendAndConsume(grantee)GrantGrant send and consume permissions

Examples

Standard Queue with DLQ

const tasks = app.addMessaging(
  MessagingFactory.build("Tasks", {
    type: "queue",
    queueType: "standard",
    visibilityTimeout: 300,
    deadLetterQueue: {
      enabled: true,
      maxReceiveCount: 5,
    },
  }),
);

tasks.grantSend(apiService);
tasks.grantConsume(workerLambda);

FIFO Queue

FIFO queues guarantee exactly-once processing and preserve message order:
const orders = app.addMessaging(
  MessagingFactory.build("Orders", {
    type: "queue",
    queueType: "fifo",
    contentBasedDeduplication: true,
    fifoThroughputLimit: "perMessageGroupId",
    deduplicationScope: "messageGroup",
  }),
);

Long Polling

Reduce empty responses and API costs with long polling:
const events = app.addMessaging(
  MessagingFactory.build("Events", {
    type: "queue",
    receiveMessageWaitTime: 20,
    messageRetentionPeriod: 1209600, // 14 days
  }),
);

KMS Encryption

const sensitive = app.addMessaging(
  MessagingFactory.build("Sensitive", {
    type: "queue",
    encryption: "SSE_KMS",
    kmsKeyArn: encryptionKey.getKeyArn(),
  }),
);

Connecting to Lambda

Use the Lambda event source integration to process queue messages:
// Lambda automatically polls the queue
processor.addSqsEventSource(tasks);

CloudFormation Outputs

OutputDescription
{id}QueueUrlQueue URL
{id}QueueArnQueue ARN
{id}QueueNameQueue name
{id}DeadLetterQueueUrlDLQ URL (if enabled)
{id}DeadLetterQueueArnDLQ ARN (if enabled)

Next Steps

Messaging Factory

Create messaging resources via the factory pattern

Lambda Function

Process queue messages with Lambda