AWS CDK SQS
The `aws-cdk-aws-sqs` library provides AWS Cloud Development Kit (CDK) constructs for defining Amazon SQS (Simple Queue Service) resources in your AWS infrastructure as code. As part of the AWS CDK v1 ecosystem, it allows developers to programmatically create and configure SQS queues, dead-letter queues, and related settings using familiar programming languages. The AWS CDK project releases frequently, often with minor version updates every few weeks for both v1 and v2 lines.
Common errors
-
jsii.errors.JavaScriptError: Error: There are still resources remaining in the stack 'MySqsQueueStack'.
cause Attempting to `cdk destroy` a stack that contains SQS queues with messages still in them, or a queue that is configured with a dead-letter queue that also contains messages.fixEmpty the SQS queue (and its associated DLQ if present) manually before attempting to destroy the stack again. For production, consider retaining queues or configuring deletion policies (`removal_policy=cdk.RemovalPolicy.RETAIN`) to avoid data loss. -
jsii.errors.JavaScriptError: 'MyQueue' has no associated Dead-Letter Queue (DLQ). Messages that fail to process will be lost.
cause This is a warning during CDK synthesis (not a deployment error) indicating that a best practice for message durability and error handling has not been followed.fixDefine a separate SQS queue (e.g., `sqs.Queue(self, 'MyDLQ')`) and assign it as the `dead_letter_queue` to your main queue, also specifying `max_receive_count` for retry logic, e.g., `dead_letter_queue={ 'queue': my_dlq, 'max_receive_count': 3 }`. -
AttributeError: 'Queue' object has no attribute 'queue_arn'
cause This error typically indicates a typo in accessing the attribute, or attempting to access a property that only becomes available after synthesis or specific context.fixEnsure you are using the correct attribute name, which is `queue.queue_arn` (or `queue.queue_url`). These properties are typically resolved during synthesis, so direct access within the stack definition usually works as expected. If passing across stacks, ensure appropriate methods like `export_value` are used.
Warnings
- breaking Migrating from AWS CDK v1 (`aws-cdk-aws-sqs`) to v2 (`aws-cdk-lib.aws_sqs`) involves significant breaking changes, primarily around import paths and package structure. V2 consolidates all constructs into a single `aws-cdk-lib` package.
- gotcha By default, SQS queues are encrypted with SQS-managed server-side encryption. If you require AWS KMS encryption with your own customer-managed key (CMK), you must explicitly configure the `encryption` and `encryption_master_key` properties.
- gotcha Missing or misconfigured Dead-Letter Queues (DLQs) can lead to lost messages when message processing fails repeatedly. While optional, it's highly recommended for production queues.
- gotcha CDK automatically generates unique physical names for resources. If you need a specific, fixed queue name, set the `queue_name` property, but be aware of potential naming conflicts when deploying multiple instances or in shared accounts.
Install
-
pip install aws-cdk-aws-sqs
Imports
- Queue
from aws_cdk.aws_sqs import Queue
from aws_cdk import aws_sqs as sqs
- Duration
import aws_cdk as cdk
Quickstart
import aws_cdk as cdk
import aws_cdk.aws_sqs as sqs
import os
class MySqsStack(cdk.Stack):
def __init__(self, scope: cdk.App, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Define an SQS Queue
queue = sqs.Queue(
self, "MySimpleQueue",
visibility_timeout=cdk.Duration.seconds(300),
queue_name="MyApplicationQueue" # Optional: give it a specific name
)
# Output the queue URL
cdk.CfnOutput(
self, "QueueUrl",
value=queue.queue_url,
description="The URL of the SQS queue",
)
# For authentication, CDK usually relies on AWS CLI configuration
# or environment variables (e.g., AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION).
# No explicit auth needed in code for basic synth.
app = cdk.App()
MySqsStack(app, "MySqsQueueStack",
env=cdk.Environment(
account=os.environ.get("CDK_DEFAULT_ACCOUNT"),
region=os.environ.get("CDK_DEFAULT_REGION")
)
)
app.synth()