|
| 1 | +/** |
| 2 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 5 | + * with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 10 | + * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions |
| 11 | + * and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +import * as cdk from '@aws-cdk/core'; |
| 15 | +import * as sqs from '@aws-cdk/aws-sqs'; |
| 16 | +import * as iot from '@aws-cdk/aws-iot'; |
| 17 | +import * as kms from '@aws-cdk/aws-kms'; |
| 18 | +import * as iam from '@aws-cdk/aws-iam'; |
| 19 | +import * as defaults from '@aws-solutions-constructs/core'; |
| 20 | + |
| 21 | +/** |
| 22 | + * @summary The properties for the IotToSqs class. |
| 23 | + */ |
| 24 | +export interface IotToSqsProps { |
| 25 | + /** |
| 26 | + * Existing instance of SQS queue object, providing both this and queueProps will cause an error. |
| 27 | + * |
| 28 | + * @default - None |
| 29 | + */ |
| 30 | + readonly existingQueueObj?: sqs.Queue; |
| 31 | + |
| 32 | + /** |
| 33 | + * User provided props to override the default props for the SQS queue. |
| 34 | + * |
| 35 | + * @default - Default props are used |
| 36 | + */ |
| 37 | + readonly queueProps?: sqs.QueueProps; |
| 38 | + |
| 39 | + /** |
| 40 | + * Optional user provided properties for the dead letter queue |
| 41 | + * |
| 42 | + * @default - Default props are used |
| 43 | + */ |
| 44 | + readonly deadLetterQueueProps?: sqs.QueueProps; |
| 45 | + |
| 46 | + /** |
| 47 | + * Whether to deploy a secondary queue to be used as a dead letter queue. |
| 48 | + * |
| 49 | + * @default - true. |
| 50 | + */ |
| 51 | + readonly deployDeadLetterQueue?: boolean; |
| 52 | + |
| 53 | + /** |
| 54 | + * The number of times a message can be unsuccessfully dequeued before being moved to the dead-letter queue. |
| 55 | + * |
| 56 | + * @default - required field if deployDeadLetterQueue=true. |
| 57 | + */ |
| 58 | + readonly maxReceiveCount?: number; |
| 59 | + |
| 60 | + /** |
| 61 | + * Use a KMS Key, either managed by this CDK app, or imported. If importing an encryption key, it must be specified in |
| 62 | + * the encryptionKey property for this construct. |
| 63 | + * |
| 64 | + * @default - true (encryption enabled, managed by this CDK app). |
| 65 | + */ |
| 66 | + readonly enableEncryptionWithCustomerManagedKey?: boolean; |
| 67 | + |
| 68 | + /** |
| 69 | + * An optional, imported encryption key to encrypt the SQS queue, and SNS Topic. |
| 70 | + * |
| 71 | + * @default - not specified. |
| 72 | + */ |
| 73 | + readonly encryptionKey?: kms.Key; |
| 74 | + |
| 75 | + /** |
| 76 | + * Optional user-provided props to override the default props for the encryption key. |
| 77 | + * |
| 78 | + * @default - Default props are used. |
| 79 | + */ |
| 80 | + readonly encryptionKeyProps?: kms.KeyProps; |
| 81 | + |
| 82 | + /** |
| 83 | + * User provided CfnTopicRuleProps to override the defaults |
| 84 | + * |
| 85 | + * @default - None |
| 86 | + */ |
| 87 | + readonly iotTopicRuleProps: iot.CfnTopicRuleProps; |
| 88 | +} |
| 89 | + |
| 90 | +export class IotToSqs extends cdk.Construct { |
| 91 | + public readonly sqsQueue: sqs.Queue; |
| 92 | + public readonly deadLetterQueue?: sqs.DeadLetterQueue; |
| 93 | + public readonly encryptionKey?: kms.IKey; |
| 94 | + public readonly iotActionsRole: iam.Role; |
| 95 | + public readonly iotTopicRule: iot.CfnTopicRule; |
| 96 | + |
| 97 | + /** |
| 98 | + * @summary Constructs a new instance of the IotToSqs class. |
| 99 | + * @param {cdk.App} scope - represents the scope for all the resources. |
| 100 | + * @param {string} id - this is a a scope-unique id. |
| 101 | + * @param {IotToSqsProps} props - user provided props for the construct |
| 102 | + * @access public |
| 103 | + */ |
| 104 | + constructor(scope: cdk.Construct, id: string, props: IotToSqsProps) { |
| 105 | + super(scope, id); |
| 106 | + defaults.CheckProps(props); |
| 107 | + |
| 108 | + // Setup the dead letter queue, if applicable |
| 109 | + this.deadLetterQueue = defaults.buildDeadLetterQueue(this, { |
| 110 | + existingQueueObj: props.existingQueueObj, |
| 111 | + deployDeadLetterQueue: props.deployDeadLetterQueue, |
| 112 | + deadLetterQueueProps: props.deadLetterQueueProps, |
| 113 | + maxReceiveCount: props.maxReceiveCount |
| 114 | + }); |
| 115 | + |
| 116 | + // Default to `true` if `enableEncryptionWithCustomerManagedKey` is undefined |
| 117 | + let enableEncryptionWithCustomerManagedKey = props.enableEncryptionWithCustomerManagedKey; |
| 118 | + if (enableEncryptionWithCustomerManagedKey === undefined) { |
| 119 | + enableEncryptionWithCustomerManagedKey = true; |
| 120 | + } |
| 121 | + |
| 122 | + // Setup the queue |
| 123 | + [this.sqsQueue, this.encryptionKey] = defaults.buildQueue(this, 'queue', { |
| 124 | + existingQueueObj: props.existingQueueObj, |
| 125 | + queueProps: props.queueProps, |
| 126 | + deadLetterQueue: this.deadLetterQueue, |
| 127 | + enableEncryptionWithCustomerManagedKey, |
| 128 | + encryptionKey: props.encryptionKey, |
| 129 | + encryptionKeyProps: props.encryptionKeyProps |
| 130 | + }); |
| 131 | + |
| 132 | + if (this.sqsQueue.fifo) { |
| 133 | + throw new Error('The IoT SQS action doesn\'t support Amazon SQS FIFO (First-In-First-Out) queues'); |
| 134 | + } |
| 135 | + |
| 136 | + // Role to allow IoT to send messages to the SQS Queue |
| 137 | + this.iotActionsRole = new iam.Role(this, 'iot-actions-role', { |
| 138 | + assumedBy: new iam.ServicePrincipal('iot.amazonaws.com') |
| 139 | + }); |
| 140 | + this.sqsQueue.grantSendMessages(this.iotActionsRole); |
| 141 | + |
| 142 | + if (this.encryptionKey) { |
| 143 | + this.encryptionKey.grantEncrypt(this.iotActionsRole); |
| 144 | + } |
| 145 | + |
| 146 | + const defaultIotTopicProps = defaults.DefaultCfnTopicRuleProps([{ |
| 147 | + sqs: { |
| 148 | + queueUrl: this.sqsQueue.queueUrl, |
| 149 | + roleArn: this.iotActionsRole.roleArn |
| 150 | + } |
| 151 | + }]); |
| 152 | + const iotTopicProps = defaults.overrideProps(defaultIotTopicProps, props.iotTopicRuleProps, true); |
| 153 | + |
| 154 | + // Create the IoT topic rule |
| 155 | + this.iotTopicRule = new iot.CfnTopicRule(this, 'IotTopicRule', iotTopicProps); |
| 156 | + } |
| 157 | +} |
0 commit comments