Skip to content

Commit

Permalink
chore(sqs): use ICfnQueue wherever possible (#28232)
Browse files Browse the repository at this point in the history
Migrate inputs of `IQueue` to `ICfnQueue` wherever possible. 

----

*By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license*
  • Loading branch information
kaizencc authored Dec 15, 2023
1 parent 6f9d970 commit 6b70f1f
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 60 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-appconfig-alpha/lib/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export class SqsDestination implements IEventDestination {
public readonly type: SourceType;
public readonly policyDocument?: iam.PolicyDocument;

constructor(queue: sqs.IQueue) {
this.extensionUri = queue.queueArn;
constructor(queue: sqs.ICfnQueue) {
this.extensionUri = queue.attrArn;
this.type = SourceType.SQS;
const policy = new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
Expand Down
10 changes: 5 additions & 5 deletions packages/@aws-cdk/aws-iot-actions-alpha/lib/sqs-queue-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ export interface SqsQueueActionProps extends CommonActionProps {
*
* @default false
*/
readonly useBase64?: boolean
readonly useBase64?: boolean;
}

/**
* The action to write the data from an MQTT message to an Amazon SQS queue.
*/
export class SqsQueueAction implements iot.IAction {
private readonly role?: iam.IRole;
private readonly queue: sqs.IQueue;
private readonly queue: sqs.ICfnQueue;
private readonly useBase64?: boolean;

/**
* @param queue The Amazon SQS queue to which to write data.
* @param props Optional properties to not use default
*/
constructor(queue: sqs.IQueue, props: SqsQueueActionProps = {}) {
constructor(queue: sqs.ICfnQueue, props: SqsQueueActionProps = {}) {
this.queue = queue;
this.role = props.role;
this.useBase64 = props.useBase64;
Expand All @@ -41,13 +41,13 @@ export class SqsQueueAction implements iot.IAction {
const role = this.role ?? singletonActionRole(rule);
role.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['sqs:SendMessage'],
resources: [this.queue.queueArn],
resources: [this.queue.attrArn],
}));

return {
configuration: {
sqs: {
queueUrl: this.queue.queueUrl,
queueUrl: this.queue.attrQueueUrl,
useBase64: this.useBase64,
roleArn: role.roleArn,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import * as sqs from '../../aws-sqs';
* Use an SQS queue as a hook target
*/
export class QueueHook implements autoscaling.ILifecycleHookTarget {
constructor(private readonly queue: sqs.IQueue) {
private readonly _queue: sqs.IQueue;
constructor(queue: sqs.ICfnQueue) {
this._queue = sqs.Queue.fromCfnQueue(queue);
}

/**
Expand All @@ -18,10 +20,10 @@ export class QueueHook implements autoscaling.ILifecycleHookTarget {
*/
public bind(_scope: Construct, options: autoscaling.BindHookTargetOptions): autoscaling.LifecycleHookTargetConfig {
const role = createRole(_scope, options.role);
this.queue.grantSendMessages(role);
this._queue.grantSendMessages(role);

return {
notificationTargetArn: this.queue.queueArn,
notificationTargetArn: this._queue.attrArn,
createdRole: role,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
AwsLogDriver, BaseService, CapacityProviderStrategy, Cluster, ContainerImage, DeploymentController, DeploymentCircuitBreaker,
ICluster, LogDriver, PropagatedTagSource, Secret,
} from '../../../aws-ecs';
import { IQueue, Queue } from '../../../aws-sqs';
import * as sqs from '../../../aws-sqs';
import { CfnOutput, Duration, FeatureFlags, Stack } from '../../../core';
import * as cxapi from '../../../cx-api';

Expand Down Expand Up @@ -90,9 +90,9 @@ export interface QueueProcessingServiceBaseProps {
* If specified and this is a FIFO queue, the queue name must end in the string '.fifo'. See
* [CreateQueue](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_CreateQueue.html)
*
* @default 'SQSQueue with CloudFormation-generated name'
* @default - SQS Queue with CloudFormation-generated name
*/
readonly queue?: IQueue;
readonly queue?: sqs.ICfnQueue;

/**
* The maximum number of times that a message can be received by consumers.
Expand Down Expand Up @@ -125,14 +125,14 @@ export interface QueueProcessingServiceBaseProps {
*
* @default - If the feature flag, ECS_REMOVE_DEFAULT_DESIRED_COUNT is false, the default is (desiredTaskCount * 2); if true, the default is 2.
*/
readonly maxScalingCapacity?: number
readonly maxScalingCapacity?: number;

/**
* Minimum capacity to scale to.
*
* @default - If the feature flag, ECS_REMOVE_DEFAULT_DESIRED_COUNT is false, the default is the desiredTaskCount; if true, the default is 1.
*/
readonly minScalingCapacity?: number
readonly minScalingCapacity?: number;

/**
* The intervals for scaling based on the SQS queue's ApproximateNumberOfMessagesVisible metric.
Expand Down Expand Up @@ -230,12 +230,12 @@ export abstract class QueueProcessingServiceBase extends Construct {
/**
* The SQS queue that the service will process from
*/
public readonly sqsQueue: IQueue;
public readonly sqsQueue: sqs.IQueue;

/**
* The dead letter queue for the primary SQS queue
*/
public readonly deadLetterQueue?: IQueue;
public readonly deadLetterQueue?: sqs.IQueue;

/**
* The cluster where your service will be deployed
Expand Down Expand Up @@ -297,12 +297,12 @@ export abstract class QueueProcessingServiceBase extends Construct {
}
// Create the SQS queue and it's corresponding DLQ if one is not provided
if (props.queue) {
this.sqsQueue = props.queue;
this.sqsQueue = sqs.Queue.fromCfnQueue(props.queue);
} else {
this.deadLetterQueue = new Queue(this, 'EcsProcessingDeadLetterQueue', {
this.deadLetterQueue = new sqs.Queue(this, 'EcsProcessingDeadLetterQueue', {
retentionPeriod: props.retentionPeriod || Duration.days(14),
});
this.sqsQueue = new Queue(this, 'EcsProcessingQueue', {
this.sqsQueue = new sqs.Queue(this, 'EcsProcessingQueue', {
visibilityTimeout: props.visibilityTimeout,
deadLetterQueue: {
queue: this.deadLetterQueue,
Expand Down
5 changes: 3 additions & 2 deletions packages/aws-cdk-lib/aws-events-targets/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export interface TargetBaseProps {
*
* @default - no dead-letter queue
*/
readonly deadLetterQueue?: sqs.IQueue;
readonly deadLetterQueue?: sqs.ICfnQueue;

/**
* The maximum age of a request that Lambda sends to a function for
* processing.
Expand Down Expand Up @@ -50,7 +51,7 @@ export function bindBaseTargetConfig(props: TargetBaseProps) {
let { deadLetterQueue, retryAttempts, maxEventAge } = props;

return {
deadLetterConfig: deadLetterQueue ? { arn: deadLetterQueue?.queueArn } : undefined,
deadLetterConfig: deadLetterQueue ? { arn: deadLetterQueue?.attrArn } : undefined,
retryPolicy: (retryAttempts !== undefined && retryAttempts >= 0) || maxEventAge
? {
maximumRetryAttempts: retryAttempts,
Expand Down
8 changes: 5 additions & 3 deletions packages/aws-cdk-lib/aws-lambda-destinations/lib/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import * as sqs from '../../aws-sqs';
* Use a SQS queue as a Lambda destination
*/
export class SqsDestination implements lambda.IDestination {
constructor(private readonly queue: sqs.IQueue) {
private readonly _queue: sqs.IQueue;
constructor(queue: sqs.ICfnQueue) {
this._queue = sqs.Queue.fromCfnQueue(queue);
}

/**
* Returns a destination configuration
*/
public bind(_scope: Construct, fn: lambda.IFunction, _options?: lambda.DestinationOptions): lambda.DestinationConfig {
// deduplicated automatically
this.queue.grantSendMessages(fn);
this._queue.grantSendMessages(fn);

return {
destination: this.queue.queueArn,
destination: this._queue.attrArn,
};
}
}
8 changes: 5 additions & 3 deletions packages/aws-cdk-lib/aws-lambda-event-sources/lib/sqs-dlq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import * as sqs from '../../aws-sqs';
* An SQS dead letter queue destination configuration for a Lambda event source
*/
export class SqsDlq implements IEventSourceDlq {
constructor(private readonly queue: sqs.IQueue) {
private readonly _queue: sqs.IQueue;
constructor(queue: sqs.ICfnQueue) {
this._queue = sqs.Queue.fromCfnQueue(queue);
}

/**
* Returns a destination configuration for the DLQ
*/
public bind(_target: IEventSourceMapping, targetHandler: IFunction): DlqDestinationConfig {
this.queue.grantSendMessages(targetHandler);
this._queue.grantSendMessages(targetHandler);

return {
destination: this.queue.queueArn,
destination: this._queue.attrArn,
};
}
}
12 changes: 7 additions & 5 deletions packages/aws-cdk-lib/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export interface FunctionOptions extends EventInvokeConfigOptions {
*
* @default - SQS queue with 14 day retention period if `deadLetterQueueEnabled` is `true`
*/
readonly deadLetterQueue?: sqs.IQueue;
readonly deadLetterQueue?: sqs.ICfnQueue;

/**
* The SNS topic to use as a DLQ.
Expand Down Expand Up @@ -1490,12 +1490,14 @@ Environment variables can be marked for removal when used in Lambda@Edge by sett
resources: [deadLetterQueue.topicArn],
}));
} else {
deadLetterQueue = props.deadLetterQueue || new sqs.Queue(this, 'DeadLetterQueue', {
retentionPeriod: Duration.days(14),
});
deadLetterQueue = props.deadLetterQueue ?
sqs.Queue.fromCfnQueue(props.deadLetterQueue) :
new sqs.Queue(this, 'DeadLetterQueue', {
retentionPeriod: Duration.days(14),
});
this.addToRolePolicy(new iam.PolicyStatement({
actions: ['sqs:SendMessage'],
resources: [deadLetterQueue.queueArn],
resources: [deadLetterQueue.attrArn],
}));
}

Expand Down
5 changes: 4 additions & 1 deletion packages/aws-cdk-lib/aws-s3-notifications/lib/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { Annotations } from '../../core';
* Use an SQS queue as a bucket notification destination
*/
export class SqsDestination implements s3.IBucketNotificationDestination {
constructor(private readonly queue: sqs.IQueue) {
private readonly _queue: sqs.IQueue;

constructor(queue: sqs.ICfnQueue) {
this._queue = sqs.Queue.fromCfnQueue(queue);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ export interface SubscriptionProps {
* @default - all messages are delivered
*/
readonly filterPolicy?: { [attribute: string]: sns.SubscriptionFilter };

/**
* The filter policy that is applied on the message body.
* To apply a filter policy to the message attributes, use `filterPolicy`. A maximum of one of `filterPolicyWithMessageBody` and `filterPolicy` may be used.
*
* @default - all messages are delivered
*/
readonly filterPolicyWithMessageBody?: { [attribute: string]: sns.FilterOrPolicy };

/**
* Queue to be used as dead letter queue.
* If not passed no dead letter queue is enabled.
Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-sns/lib/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export interface SubscriptionProps extends SubscriptionOptions {
* this class.
*/
export class Subscription extends Resource {

/**
* The DLQ associated with this subscription if present.
*/
Expand Down
7 changes: 3 additions & 4 deletions packages/aws-cdk-lib/aws-sqs/lib/policy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Construct } from 'constructs';
import { IQueue } from './queue-base';
import { CfnQueuePolicy } from './sqs.generated';
import { CfnQueuePolicy, ICfnQueue } from './sqs.generated';
import { PolicyDocument } from '../../aws-iam';
import { Resource } from '../../core';

Expand All @@ -11,7 +10,7 @@ export interface QueuePolicyProps {
/**
* The set of queues this policy applies to.
*/
readonly queues: IQueue[];
readonly queues: ICfnQueue[];
}

/**
Expand Down Expand Up @@ -39,7 +38,7 @@ export class QueuePolicy extends Resource {

new CfnQueuePolicy(this, 'Resource', {
policyDocument: this.document,
queues: props.queues.map(q => q.queueUrl),
queues: props.queues.map(q => q.attrQueueUrl),
});
}

Expand Down
24 changes: 21 additions & 3 deletions packages/aws-cdk-lib/aws-sqs/lib/queue-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { QueuePolicy } from './policy';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import { IResource, Resource, ResourceProps } from '../../core';
import { ICfnQueue } from './sqs.generated';

/**
* Represents an SQS queue
*/
export interface IQueue extends IResource {
export interface IQueue extends IResource, ICfnQueue {
/**
* The ARN of this queue
* @attribute
Expand Down Expand Up @@ -105,14 +106,31 @@ export interface IQueue extends IResource {
* Reference to a new or existing Amazon SQS queue
*/
export abstract class QueueBase extends Resource implements IQueue {
/**
* The ARN of this queue
*
* @attribute
*/
public abstract readonly attrArn: string;

/**
* The ARN of this queue
*
* Deprecated: use attrArn
*/
public abstract readonly queueArn: string;

/**
* The URL of this queue
*
* @attribute
*/
public abstract readonly attrQueueUrl: string;

/**
* The URL of this queue
*
* Deprecated: use attrQueueUrl
*/
public abstract readonly queueUrl: string;

Expand Down Expand Up @@ -339,5 +357,5 @@ export enum QueueEncryption {
* To learn more about SSE-SQS on Amazon SQS, please visit the
* [Amazon SQS documentation](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-server-side-encryption.html).
*/
SQS_MANAGED = 'SQS_MANAGED'
}
SQS_MANAGED = 'SQS_MANAGED',
}
Loading

0 comments on commit 6b70f1f

Please sign in to comment.