Skip to content

Commit

Permalink
feat(sns): addSubscription returns the created Subscription (aws#16785)
Browse files Browse the repository at this point in the history
Allow creating dependencies on the subscription by returning it from `addSubscription()`. Before this change the subscription resource was inaccessible. Creating a dependency required manually creating the subscription resource.

Our current workaround is:

```
        topic = sns.Topic(self, "topic")
        subscription_req = subs.SqsSubscription(queue)
        subscription_data = subscription_req.bind(topic)
        subscription = sns.Subscription(
            self,
            "Subscription",
            topic=topic,
            endpoint=subscription_data.endpoint,
            protocol=subscription_data.protocol,
            region=subscription_data.region,
            raw_message_delivery=subscription_data.raw_message_delivery,
            filter_policy=subscription_data.filter_policy,
            dead_letter_queue=subscription_data.dead_letter_queue,
        )

        something.add_dependency(subscription)
```

And it would be much simpler as:

```
topic = sns.Topic(self, "topic")
subscription = topic.add_subscription(subs.SqsSubscription(queue))
something.add_dependency(subscription)
```

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
amirfireeye authored and TikiTDO committed Feb 21, 2022
1 parent af07810 commit 663a666
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-sns/lib/topic-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface ITopic extends IResource, notifications.INotificationRuleTarget
/**
* Subscribe some endpoint to this topic
*/
addSubscription(subscription: ITopicSubscription): void;
addSubscription(subscription: ITopicSubscription): Subscription;

/**
* Adds a statement to the IAM resource policy associated with this topic.
Expand Down Expand Up @@ -68,7 +68,7 @@ export abstract class TopicBase extends Resource implements ITopic {
/**
* Subscribe some endpoint to this topic
*/
public addSubscription(subscription: ITopicSubscription) {
public addSubscription(subscription: ITopicSubscription): Subscription {
const subscriptionConfig = subscription.bind(this);

const scope = subscriptionConfig.subscriberScope || this;
Expand All @@ -83,7 +83,7 @@ export abstract class TopicBase extends Resource implements ITopic {
throw new Error(`A subscription with id "${id}" already exists under the scope ${scope.node.path}`);
}

new Subscription(scope, id, {
return new Subscription(scope, id, {
topic: this,
...subscriptionConfig,
});
Expand Down
22 changes: 22 additions & 0 deletions packages/@aws-cdk/aws-sns/test/sns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,26 @@ describe('Topic', () => {


});

test('result of addSubscription() can be used as a dependency', () => {
// GIVEN
const stack = new cdk.Stack();
const topic = new sns.Topic(stack, 'Topic');
const user = new iam.User(stack, 'User');

// WHEN
const subscription = topic.addSubscription({
bind: () => ({
protocol: sns.SubscriptionProtocol.HTTP,
endpoint: 'http://foo/bar',
subscriberId: 'my-subscription',
}),
});
user.node.addDependency(subscription);

// THEN
Template.fromStack(stack).hasResource('AWS::IAM::User', {
DependsOn: ['Topicmysubscription1E605DD7'],
});
});
});

0 comments on commit 663a666

Please sign in to comment.