Skip to content

Commit

Permalink
feat(sns): support cross-region subscription on imported topics (#4917)
Browse files Browse the repository at this point in the history
Use the 'region' property on subscribing to imported SNS topics so that
the user can avail cross-region subscriptions.

fixes #3842
  • Loading branch information
nija-at authored Nov 10, 2019
1 parent 98fb888 commit 3dd194d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/@aws-cdk/aws-sns-subscriptions/lib/sqs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import iam = require('@aws-cdk/aws-iam');
import sns = require('@aws-cdk/aws-sns');
import sqs = require('@aws-cdk/aws-sqs');
import { Construct } from '@aws-cdk/core';
import { Construct, Stack } from '@aws-cdk/core';
import { SubscriptionProps } from './subscription';

/**
Expand Down Expand Up @@ -50,6 +50,15 @@ export class SqsSubscription implements sns.ITopicSubscription {
protocol: sns.SubscriptionProtocol.SQS,
rawMessageDelivery: this.props.rawMessageDelivery,
filterPolicy: this.props.filterPolicy,
region: this.regionFromArn(topic),
};
}

private regionFromArn(topic: sns.ITopic): string | undefined {
// no need to specify `region` for topics defined within the same stack
if (topic instanceof sns.Topic) {
return undefined;
}
return Stack.of(topic).parseArn(topic.topicArn).region;
}
}
25 changes: 24 additions & 1 deletion packages/@aws-cdk/aws-sns-subscriptions/test/subs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import '@aws-cdk/assert/jest';
import lambda = require('@aws-cdk/aws-lambda');
import sns = require('@aws-cdk/aws-sns');
import sqs = require('@aws-cdk/aws-sqs');
import { SecretValue, Stack } from '@aws-cdk/core';
import { CfnParameter, SecretValue, Stack } from '@aws-cdk/core';
import subs = require('../lib');

// tslint:disable:object-literal-key-quotes
Expand Down Expand Up @@ -550,3 +550,26 @@ test('with filter policy', () => {
}
});
});

test('region property is present on an imported topic', () => {
const imported = sns.Topic.fromTopicArn(stack, 'mytopic', 'arn:aws:sns:us-east-1:1234567890:mytopic');
const queue = new sqs.Queue(stack, 'myqueue');
imported.addSubscription(new subs.SqsSubscription(queue));

expect(stack).toHaveResource('AWS::SNS::Subscription', {
Region: 'us-east-1'
});
});

test('region property on an imported topic as a parameter', () => {
const topicArn = new CfnParameter(stack, 'topicArn');
const imported = sns.Topic.fromTopicArn(stack, 'mytopic', topicArn.valueAsString);
const queue = new sqs.Queue(stack, 'myqueue');
imported.addSubscription(new subs.SqsSubscription(queue));

expect(stack).toHaveResource('AWS::SNS::Subscription', {
Region: {
"Fn::Select": [ 3, { "Fn::Split": [ ":", { "Ref": "topicArn" } ] } ]
}
});
});
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-sns/lib/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ export interface SubscriptionOptions {
* @default - all messages are delivered
*/
readonly filterPolicy?: { [attribute: string]: SubscriptionFilter };

/**
* The region where the topic resides, in the case of cross-region subscriptions
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sns-subscription.html#cfn-sns-subscription-region
* @default - the region where the CloudFormation stack is being deployed.
*/
readonly region?: string;
}
/**
* Properties for creating a new subscription
Expand Down Expand Up @@ -85,6 +92,7 @@ export class Subscription extends Resource {
topicArn: props.topic.topicArn,
rawMessageDelivery: props.rawMessageDelivery,
filterPolicy: this.filterPolicy,
region: props.region,
});

}
Expand Down

0 comments on commit 3dd194d

Please sign in to comment.