Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-sns-subscriptions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Subscriptions can be added to the following endpoints:
* AWS Lambda
* Email

Subscriptions to Amazon SQS and AWS Lambda can be added on topics across regions.

Create an Amazon SNS Topic to add subscriptions.

```ts
Expand Down
11 changes: 10 additions & 1 deletion packages/@aws-cdk/aws-sns-subscriptions/lib/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import * as sns from '@aws-cdk/aws-sns';
import { Construct } from '@aws-cdk/core';
import { Construct, Stack } from '@aws-cdk/core';
import { SubscriptionProps } from './subscription';

/**
Expand Down Expand Up @@ -35,6 +35,15 @@ export class LambdaSubscription implements sns.ITopicSubscription {
endpoint: this.fn.functionArn,
protocol: sns.SubscriptionProtocol.LAMBDA,
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;
}
}
35 changes: 33 additions & 2 deletions packages/@aws-cdk/aws-sns-subscriptions/test/subs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ test('with filter policy', () => {
});
});

test('region property is present on an imported topic', () => {
test('region property is present on an imported topic - sqs', () => {
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));
Expand All @@ -746,7 +746,7 @@ test('region property is present on an imported topic', () => {
});
});

test('region property on an imported topic as a parameter', () => {
test('region property on an imported topic as a parameter - sqs', () => {
const topicArn = new CfnParameter(stack, 'topicArn');
const imported = sns.Topic.fromTopicArn(stack, 'mytopic', topicArn.valueAsString);
const queue = new sqs.Queue(stack, 'myqueue');
Expand All @@ -758,3 +758,34 @@ test('region property on an imported topic as a parameter', () => {
},
});
});

test('region property is present on an imported topic - lambda', () => {
const imported = sns.Topic.fromTopicArn(stack, 'mytopic', 'arn:aws:sns:us-east-1:1234567890:mytopic');
const func = new lambda.Function(stack, 'MyFunc', {
runtime: lambda.Runtime.NODEJS_10_X,
handler: 'index.handler',
code: lambda.Code.fromInline('exports.handler = function(e, c, cb) { return cb() }'),
});
imported.addSubscription(new subs.LambdaSubscription(func));

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

test('region property on an imported topic as a parameter - lambda', () => {
const topicArn = new CfnParameter(stack, 'topicArn');
const imported = sns.Topic.fromTopicArn(stack, 'mytopic', topicArn.valueAsString);
const func = new lambda.Function(stack, 'MyFunc', {
runtime: lambda.Runtime.NODEJS_10_X,
handler: 'index.handler',
code: lambda.Code.fromInline('exports.handler = function(e, c, cb) { return cb() }'),
});
imported.addSubscription(new subs.LambdaSubscription(func));

expect(stack).toHaveResource('AWS::SNS::Subscription', {
Region: {
'Fn::Select': [ 3, { 'Fn::Split': [ ':', { 'Ref': 'topicArn' } ] } ],
},
});
});