aws-cdk-lib.aws_sqs.Queue

class Queue (construct)

LanguageType name
.NETAmazon.CDK.AWS.SQS.Queue
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awssqs#Queue
Javasoftware.amazon.awscdk.services.sqs.Queue
Pythonaws_cdk.aws_sqs.Queue
TypeScript (source)aws-cdk-lib » aws_sqs » Queue

Implements IConstruct, IDependable, IResource, IQueue

A new Amazon SQS queue.

Example

// An sqs queue for unsuccessful invocations of a lambda function
import * as sqs from 'aws-cdk-lib/aws-sqs';

const deadLetterQueue = new sqs.Queue(this, 'DeadLetterQueue');

const myFn = new lambda.Function(this, 'Fn', {
  runtime: lambda.Runtime.NODEJS_14_X,
  handler: 'index.handler',
  code: lambda.Code.fromInline('// your code'),
  // sqs queue for unsuccessful invocations
  onFailure: new destinations.SqsDestination(deadLetterQueue),
});

Initializer

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

Parameters

  • scope Construct
  • id string
  • props QueueProps

Construct Props

NameTypeDescription
contentBasedDeduplication?booleanSpecifies whether to enable content-based deduplication.
dataKeyReuse?DurationThe length of time that Amazon SQS reuses a data key before calling KMS again.
deadLetterQueue?DeadLetterQueueSend messages to this queue if they were unsuccessfully dequeued a number of times.
deduplicationScope?DeduplicationScopeFor high throughput for FIFO queues, specifies whether message deduplication occurs at the message group or queue level.
deliveryDelay?DurationThe time in seconds that the delivery of all messages in the queue is delayed.
encryption?QueueEncryptionWhether the contents of the queue are encrypted, and by what type of key.
encryptionMasterKey?IKeyExternal KMS key to use for queue encryption.
enforceSSL?booleanEnforce encryption of data in transit.
fifo?booleanWhether this a first-in-first-out (FIFO) queue.
fifoThroughputLimit?FifoThroughputLimitFor high throughput for FIFO queues, specifies whether the FIFO queue throughput quota applies to the entire queue or per message group.
maxMessageSizeBytes?numberThe limit of how many bytes that a message can contain before Amazon SQS rejects it.
queueName?stringA name for the queue.
receiveMessageWaitTime?DurationDefault wait time for ReceiveMessage calls.
removalPolicy?RemovalPolicyPolicy to apply when the queue is removed from the stack.
retentionPeriod?DurationThe number of seconds that Amazon SQS retains a message.
visibilityTimeout?DurationTimeout of processing a single message.

contentBasedDeduplication?

Type: boolean (optional, default: false)

Specifies whether to enable content-based deduplication.

During the deduplication interval (5 minutes), Amazon SQS treats messages that are sent with identical content (excluding attributes) as duplicates and delivers only one copy of the message.

If you don't enable content-based deduplication and you want to deduplicate messages, provide an explicit deduplication ID in your SendMessage() call.

(Only applies to FIFO queues.)


dataKeyReuse?

Type: Duration (optional, default: Duration.minutes(5))

The length of time that Amazon SQS reuses a data key before calling KMS again.

The value must be an integer between 60 (1 minute) and 86,400 (24 hours). The default is 300 (5 minutes).


deadLetterQueue?

Type: DeadLetterQueue (optional, default: no dead-letter queue)

Send messages to this queue if they were unsuccessfully dequeued a number of times.


deduplicationScope?

Type: DeduplicationScope (optional, default: DeduplicationScope.QUEUE)

For high throughput for FIFO queues, specifies whether message deduplication occurs at the message group or queue level.

(Only applies to FIFO queues.)


deliveryDelay?

Type: Duration (optional, default: 0)

The time in seconds that the delivery of all messages in the queue is delayed.

You can specify an integer value of 0 to 900 (15 minutes). The default value is 0.


encryption?

Type: QueueEncryption (optional, default: SQS_MANAGED (SSE-SQS) for newly created queues)

Whether the contents of the queue are encrypted, and by what type of key.

Be aware that encryption is not available in all regions, please see the docs for current availability details.


encryptionMasterKey?

Type: IKey (optional, default: If encryption is set to KMS and not specified, a key will be created.)

External KMS key to use for queue encryption.

Individual messages will be encrypted using data keys. The data keys in turn will be encrypted using this key, and reused for a maximum of dataKeyReuseSecs seconds.

If the 'encryptionMasterKey' property is set, 'encryption' type will be implicitly set to "KMS".


enforceSSL?

Type: boolean (optional, default: false)

Enforce encryption of data in transit.

See also: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-security-best-practices.html#enforce-encryption-data-in-transit


fifo?

Type: boolean (optional, default: false, unless queueName ends in '.fifo' or 'contentBasedDeduplication' is true.)

Whether this a first-in-first-out (FIFO) queue.


fifoThroughputLimit?

Type: FifoThroughputLimit (optional, default: FifoThroughputLimit.PER_QUEUE)

For high throughput for FIFO queues, specifies whether the FIFO queue throughput quota applies to the entire queue or per message group.

(Only applies to FIFO queues.)


maxMessageSizeBytes?

Type: number (optional, default: 256KiB)

The limit of how many bytes that a message can contain before Amazon SQS rejects it.

You can specify an integer value from 1024 bytes (1 KiB) to 262144 bytes (256 KiB). The default value is 262144 (256 KiB).


queueName?

Type: string (optional, default: CloudFormation-generated name)

A name for the queue.

If specified and this is a FIFO queue, must end in the string '.fifo'.


receiveMessageWaitTime?

Type: Duration (optional, default: 0)

Default wait time for ReceiveMessage calls.

Does not wait if set to 0, otherwise waits this amount of seconds by default for messages to arrive.

For more information, see Amazon SQS Long Poll.


removalPolicy?

Type: RemovalPolicy (optional, default: RemovalPolicy.DESTROY)

Policy to apply when the queue is removed from the stack.

Even though queues are technically stateful, their contents are transient and it is common to add and remove Queues while rearchitecting your application. The default is therefore DESTROY. Change it to RETAIN if the messages are so valuable that accidentally losing them would be unacceptable.


retentionPeriod?

Type: Duration (optional, default: Duration.days(4))

The number of seconds that Amazon SQS retains a message.

You can specify an integer value from 60 seconds (1 minute) to 1209600 seconds (14 days). The default value is 345600 seconds (4 days).


visibilityTimeout?

Type: Duration (optional, default: Duration.seconds(30))

Timeout of processing a single message.

After dequeuing, the processor has this much time to handle the message and delete it from the queue before it becomes visible again for dequeueing by another processor.

Values must be from 0 to 43200 seconds (12 hours). If you don't specify a value, AWS CloudFormation uses the default value of 30 seconds.

Properties

NameTypeDescription
autoCreatePolicybooleanControls automatic creation of policy objects.
envResourceEnvironmentThe environment this resource belongs to.
fifobooleanWhether this queue is an Amazon SQS FIFO queue.
nodeNodeThe tree node.
queueArnstringThe ARN of this queue.
queueNamestringThe name of this queue.
queueUrlstringThe URL of this queue.
stackStackThe stack in which this resource is defined.
deadLetterQueue?DeadLetterQueueIf this queue is configured with a dead-letter queue, this is the dead-letter queue settings.
encryptionMasterKey?IKeyIf this queue is encrypted, this is the KMS key.

autoCreatePolicy

Type: boolean

Controls automatic creation of policy objects.

Set by subclasses.


env

Type: ResourceEnvironment

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.


fifo

Type: boolean

Whether this queue is an Amazon SQS FIFO queue.

If false, this is a standard queue.


node

Type: Node

The tree node.


queueArn

Type: string

The ARN of this queue.


queueName

Type: string

The name of this queue.


queueUrl

Type: string

The URL of this queue.


stack

Type: Stack

The stack in which this resource is defined.


deadLetterQueue?

Type: DeadLetterQueue (optional)

If this queue is configured with a dead-letter queue, this is the dead-letter queue settings.


encryptionMasterKey?

Type: IKey (optional)

If this queue is encrypted, this is the KMS key.

Methods

NameDescription
addToResourcePolicy(statement)Adds a statement to the IAM resource policy associated with this queue.
applyRemovalPolicy(policy)Apply the given removal policy to this resource.
grant(grantee, ...actions)Grant the actions defined in queueActions to the identity Principal given on this SQS queue resource.
grantConsumeMessages(grantee)Grant permissions to consume messages from a queue.
grantPurge(grantee)Grant an IAM principal permissions to purge all messages from the queue.
grantSendMessages(grantee)Grant access to send messages to a queue to the given identity.
metric(metricName, props?)Return the given named metric for this Queue.
metricApproximateAgeOfOldestMessage(props?)The approximate age of the oldest non-deleted message in the queue.
metricApproximateNumberOfMessagesDelayed(props?)The number of messages in the queue that are delayed and not available for reading immediately.
metricApproximateNumberOfMessagesNotVisible(props?)The number of messages that are in flight.
metricApproximateNumberOfMessagesVisible(props?)The number of messages available for retrieval from the queue.
metricNumberOfEmptyReceives(props?)The number of ReceiveMessage API calls that did not return a message.
metricNumberOfMessagesDeleted(props?)The number of messages deleted from the queue.
metricNumberOfMessagesReceived(props?)The number of messages returned by calls to the ReceiveMessage action.
metricNumberOfMessagesSent(props?)The number of messages added to a queue.
metricSentMessageSize(props?)The size of messages added to a queue.
toString()Returns a string representation of this construct.
static fromQueueArn(scope, id, queueArn)Import an existing SQS queue provided an ARN.
static fromQueueAttributes(scope, id, attrs)Import an existing queue.

addToResourcePolicy(statement)

public addToResourcePolicy(statement: PolicyStatement): AddToResourcePolicyResult

Parameters

  • statement PolicyStatement

Returns

  • AddToResourcePolicyResult

Adds a statement to the IAM resource policy associated with this queue.

If this queue was created in this stack (new Queue), a queue policy will be automatically created upon the first call to addToPolicy. If the queue is imported (Queue.import), then this is a no-op.


applyRemovalPolicy(policy)

public applyRemovalPolicy(policy: RemovalPolicy): void

Parameters

  • policy RemovalPolicy

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).


grant(grantee, ...actions)

public grant(grantee: IGrantable, ...actions: string[]): Grant

Parameters

  • grantee IGrantable — Principal to grant right to.
  • actions string — The actions to grant.

Returns

  • Grant

Grant the actions defined in queueActions to the identity Principal given on this SQS queue resource.


grantConsumeMessages(grantee)

public grantConsumeMessages(grantee: IGrantable): Grant

Parameters

  • grantee IGrantable — Principal to grant consume rights to.

Returns

  • Grant

Grant permissions to consume messages from a queue.

This will grant the following permissions:

  • sqs:ChangeMessageVisibility
  • sqs:DeleteMessage
  • sqs:ReceiveMessage
  • sqs:GetQueueAttributes
  • sqs:GetQueueUrl

If encryption is used, permission to use the key to decrypt the contents of the queue will also be granted to the same principal.

This will grant the following KMS permissions:

  • kms:Decrypt

grantPurge(grantee)

public grantPurge(grantee: IGrantable): Grant

Parameters

  • grantee IGrantable — Principal to grant send rights to.

Returns

  • Grant

Grant an IAM principal permissions to purge all messages from the queue.

This will grant the following permissions:

  • sqs:PurgeQueue
  • sqs:GetQueueAttributes
  • sqs:GetQueueUrl

grantSendMessages(grantee)

public grantSendMessages(grantee: IGrantable): Grant

Parameters

  • grantee IGrantable — Principal to grant send rights to.

Returns

  • Grant

Grant access to send messages to a queue to the given identity.

This will grant the following permissions:

  • sqs:SendMessage
  • sqs:GetQueueAttributes
  • sqs:GetQueueUrl

If encryption is used, permission to use the key to encrypt/decrypt the contents of the queue will also be granted to the same principal.

This will grant the following KMS permissions:

  • kms:Decrypt
  • kms:Encrypt
  • kms:ReEncrypt*
  • kms:GenerateDataKey*

metric(metricName, props?)

public metric(metricName: string, props?: MetricOptions): Metric

Parameters

  • metricName string
  • props MetricOptions

Returns

  • Metric

Return the given named metric for this Queue.


metricApproximateAgeOfOldestMessage(props?)

public metricApproximateAgeOfOldestMessage(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The approximate age of the oldest non-deleted message in the queue.

Maximum over 5 minutes


metricApproximateNumberOfMessagesDelayed(props?)

public metricApproximateNumberOfMessagesDelayed(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of messages in the queue that are delayed and not available for reading immediately.

Maximum over 5 minutes


metricApproximateNumberOfMessagesNotVisible(props?)

public metricApproximateNumberOfMessagesNotVisible(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of messages that are in flight.

Maximum over 5 minutes


metricApproximateNumberOfMessagesVisible(props?)

public metricApproximateNumberOfMessagesVisible(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of messages available for retrieval from the queue.

Maximum over 5 minutes


metricNumberOfEmptyReceives(props?)

public metricNumberOfEmptyReceives(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of ReceiveMessage API calls that did not return a message.

Sum over 5 minutes


metricNumberOfMessagesDeleted(props?)

public metricNumberOfMessagesDeleted(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of messages deleted from the queue.

Sum over 5 minutes


metricNumberOfMessagesReceived(props?)

public metricNumberOfMessagesReceived(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of messages returned by calls to the ReceiveMessage action.

Sum over 5 minutes


metricNumberOfMessagesSent(props?)

public metricNumberOfMessagesSent(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of messages added to a queue.

Sum over 5 minutes


metricSentMessageSize(props?)

public metricSentMessageSize(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The size of messages added to a queue.

Average over 5 minutes


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


static fromQueueArn(scope, id, queueArn)

public static fromQueueArn(scope: Construct, id: string, queueArn: string): IQueue

Parameters

  • scope Construct — The parent creating construct.
  • id string — The construct's name.
  • queueArn string — queue ARN (i.e. arn:aws:sqs:us-east-2:444455556666:queue1).

Returns

  • IQueue

Import an existing SQS queue provided an ARN.


static fromQueueAttributes(scope, id, attrs)

public static fromQueueAttributes(scope: Construct, id: string, attrs: QueueAttributes): IQueue

Parameters

  • scope Construct
  • id string
  • attrs QueueAttributes

Returns

  • IQueue

Import an existing queue.