|
| 1 | +import { |
| 2 | + dataAwsIamPolicyDocument, |
| 3 | + snsTopicSubscription, |
| 4 | + sqsQueue, |
| 5 | + sqsQueuePolicy, |
| 6 | + dataAwsSqsQueue, |
| 7 | +} from '@cdktf/provider-aws'; |
| 8 | +import { type SnsTopicSubscriptionConfig } from '@cdktf/provider-aws/lib/sns-topic-subscription'; |
| 9 | +import { TerraformMetaArguments, TerraformResource } from 'cdktf'; |
| 10 | +import { Construct } from 'constructs'; |
| 11 | + |
| 12 | +export interface SnsSqsSubscriptionProps { |
| 13 | + name: string; |
| 14 | + snsTopicArn: string; |
| 15 | + snsDlq?: sqsQueue.SqsQueue; |
| 16 | + filterPolicy?: SnsTopicSubscriptionConfig['filterPolicy']; |
| 17 | + filterPolicyScope?: SnsTopicSubscriptionConfig['filterPolicyScope']; |
| 18 | +} |
| 19 | + |
| 20 | +export interface ApplicationSqsSnsTopicsSubscriptionProps |
| 21 | + extends TerraformMetaArguments { |
| 22 | + subscriptions: SnsSqsSubscriptionProps[]; |
| 23 | + name: string; |
| 24 | + sqsQueue: sqsQueue.SqsQueue | dataAwsSqsQueue.DataAwsSqsQueue; |
| 25 | + tags?: { [key: string]: string }; |
| 26 | + dependsOn?: TerraformResource[]; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Creates an SNS to SQS subscription, allowing an SQS queue to |
| 31 | + * subscribe to multiple topics (in the rare case where this pattern |
| 32 | + * is useful) |
| 33 | + */ |
| 34 | +export class ApplicationSqsSnsTopicsSubscription extends Construct { |
| 35 | + public readonly snsTopicSubscriptions: snsTopicSubscription.SnsTopicSubscription[]; |
| 36 | + |
| 37 | + constructor( |
| 38 | + scope: Construct, |
| 39 | + name: string, |
| 40 | + private config: ApplicationSqsSnsTopicsSubscriptionProps, |
| 41 | + ) { |
| 42 | + super(scope, name); |
| 43 | + const subscriptions = config.subscriptions.map((sub) => ({ |
| 44 | + ...sub, |
| 45 | + snsDlq: sub.snsDlq ?? this.createSqsSubscriptionDlq(sub.name), |
| 46 | + })); |
| 47 | + this.snsTopicSubscriptions = subscriptions.map((sub) => { |
| 48 | + return this.createSnsTopicSubscription(sub); |
| 49 | + }); |
| 50 | + this.createPoliciesForSnsToSQS(subscriptions); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Create a dead-letter queue for failed SNS messages |
| 55 | + * @private |
| 56 | + */ |
| 57 | + private createSqsSubscriptionDlq(name: string): sqsQueue.SqsQueue { |
| 58 | + return new sqsQueue.SqsQueue(this, `${name}-sns-topic-dql`, { |
| 59 | + name: `${name}-SNS-Topic-DLQ`, |
| 60 | + tags: this.config.tags, |
| 61 | + provider: this.config.provider, |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Create an SNS subscription for SQS |
| 67 | + * @param snsTopicDlq |
| 68 | + * @private |
| 69 | + */ |
| 70 | + private createSnsTopicSubscription( |
| 71 | + properties: Omit<SnsSqsSubscriptionProps, 'snsDlq'> & { |
| 72 | + snsDlq: sqsQueue.SqsQueue; |
| 73 | + }, |
| 74 | + ): snsTopicSubscription.SnsTopicSubscription { |
| 75 | + return new snsTopicSubscription.SnsTopicSubscription( |
| 76 | + this, |
| 77 | + `${properties.name}-sns-subscription`, |
| 78 | + { |
| 79 | + topicArn: properties.snsTopicArn, |
| 80 | + protocol: 'sqs', |
| 81 | + endpoint: this.config.sqsQueue.arn, |
| 82 | + redrivePolicy: JSON.stringify({ |
| 83 | + deadLetterTargetArn: properties.snsDlq.arn, |
| 84 | + }), |
| 85 | + filterPolicy: properties.filterPolicy, |
| 86 | + filterPolicyScope: properties.filterPolicyScope, |
| 87 | + dependsOn: [ |
| 88 | + properties.snsDlq, |
| 89 | + ...(this.config.dependsOn ? this.config.dependsOn : []), |
| 90 | + ], |
| 91 | + provider: this.config.provider, |
| 92 | + } as snsTopicSubscription.SnsTopicSubscriptionConfig, |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Create IAM policies to allow SNS to write to the target SQS queue and a |
| 98 | + * dead-letter queue |
| 99 | + * @param snsTopicDlq |
| 100 | + * @private |
| 101 | + */ |
| 102 | + private createPoliciesForSnsToSQS( |
| 103 | + subscriptions: Array< |
| 104 | + Omit<SnsSqsSubscriptionProps, 'snsDlq'> & { |
| 105 | + snsDlq: sqsQueue.SqsQueue; |
| 106 | + } |
| 107 | + >, |
| 108 | + ): void { |
| 109 | + // Make DLQ policies first since they are separate |
| 110 | + subscriptions.forEach((sub) => { |
| 111 | + const policy = new dataAwsIamPolicyDocument.DataAwsIamPolicyDocument( |
| 112 | + this, |
| 113 | + `${sub.name}-sns-dlq-policy-document`, |
| 114 | + { |
| 115 | + statement: [ |
| 116 | + { |
| 117 | + effect: 'Allow', |
| 118 | + actions: ['sqs:SendMessage'], |
| 119 | + resources: [sub.snsDlq.arn], |
| 120 | + principals: [ |
| 121 | + { |
| 122 | + identifiers: ['sns.amazonaws.com'], |
| 123 | + type: 'Service', |
| 124 | + }, |
| 125 | + ], |
| 126 | + condition: [ |
| 127 | + { |
| 128 | + test: 'ArnEquals', |
| 129 | + variable: 'aws:SourceArn', |
| 130 | + values: [sub.snsTopicArn], |
| 131 | + }, |
| 132 | + ], |
| 133 | + }, |
| 134 | + ], |
| 135 | + dependsOn: [sub.snsDlq] as TerraformResource[], |
| 136 | + provider: this.config.provider, |
| 137 | + }, |
| 138 | + ).json; |
| 139 | + |
| 140 | + return new sqsQueuePolicy.SqsQueuePolicy( |
| 141 | + this, |
| 142 | + `${sub.name}-sns-dlq-policy`, |
| 143 | + { |
| 144 | + queueUrl: sub.snsDlq.url, |
| 145 | + policy: policy, |
| 146 | + provider: this.config.provider, |
| 147 | + }, |
| 148 | + ); |
| 149 | + }); |
| 150 | + |
| 151 | + const queuePolicyDoc = |
| 152 | + new dataAwsIamPolicyDocument.DataAwsIamPolicyDocument( |
| 153 | + this, |
| 154 | + `${this.config.name}-sns-sqs-policy-document`, |
| 155 | + { |
| 156 | + statement: [ |
| 157 | + { |
| 158 | + effect: 'Allow', |
| 159 | + actions: ['sqs:SendMessage'], |
| 160 | + resources: [this.config.sqsQueue.arn], |
| 161 | + principals: [ |
| 162 | + { |
| 163 | + identifiers: ['sns.amazonaws.com'], |
| 164 | + type: 'Service', |
| 165 | + }, |
| 166 | + ], |
| 167 | + condition: [ |
| 168 | + { |
| 169 | + test: 'ArnEquals', |
| 170 | + variable: 'aws:SourceArn', |
| 171 | + values: subscriptions.map((sub) => sub.snsTopicArn), |
| 172 | + }, |
| 173 | + ], |
| 174 | + }, |
| 175 | + ], |
| 176 | + dependsOn: [this.config.sqsQueue] as TerraformResource[], |
| 177 | + provider: this.config.provider, |
| 178 | + }, |
| 179 | + ).json; |
| 180 | + |
| 181 | + new sqsQueuePolicy.SqsQueuePolicy( |
| 182 | + this, |
| 183 | + `${this.config.name}-sns-sqs-policy`, |
| 184 | + { |
| 185 | + queueUrl: this.config.sqsQueue.url, |
| 186 | + policy: queuePolicyDoc, |
| 187 | + provider: this.config.provider, |
| 188 | + }, |
| 189 | + ); |
| 190 | + } |
| 191 | +} |
0 commit comments