Skip to content

Commit 87b12a3

Browse files
authored
Merge pull request #95 from zachboyd/sns-subscription-fix
Added support to have an alias subscribe to an existing SNS topic #94
2 parents dde216e + 24992eb commit 87b12a3

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

lib/stackops/snsEvents.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
4646
delete stageStack.Resources[name];
4747
});
4848

49+
const snsSubscriptions =
50+
_.assign({},
51+
_.pickBy(stageStack.Resources, [ 'Type', 'AWS::SNS::Subscription' ]));
52+
53+
_.forOwn(snsSubscriptions, (subscription, name) => {
54+
55+
const functionNameRef = utils.findAllReferences(_.get(subscription.Properties, 'Endpoint'));
56+
const functionName = _.replace(_.get(functionNameRef, '[0].ref', ''), /LambdaFunction$/, '');
57+
const versionName = _.find(_.keys(versions), version => _.startsWith(version, functionName));
58+
const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, functionName));
59+
60+
subscription.Properties.Endpoint = { Ref: aliasName };
61+
subscription.DependsOn = [ versionName, aliasName ];
62+
63+
delete stageStack.Resources[name];
64+
});
65+
4966
// Fetch lambda permissions. These have to be updated later to allow the aliased functions.
5067
const snsLambdaPermissions =
5168
_.assign({},
@@ -71,6 +88,7 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
7188

7289
// Add all alias stack owned resources
7390
aliasResources.push(snsTopics);
91+
aliasResources.push(snsSubscriptions);
7492
aliasResources.push(snsLambdaPermissions);
7593

7694
_.forEach(aliasResources, resource => _.assign(aliasStack.Resources, resource));

test/data/sns-stack.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,24 @@
250250
]
251251
}
252252
},
253+
"SNSTopicSubscriptionSlstestprojecttopic": {
254+
"Type" : "AWS::SNS::Subscription",
255+
"Properties": {
256+
"Endpoint": {
257+
"Fn::GetAtt": [
258+
"Testfct1LambdaFunction",
259+
"Arn"
260+
]
261+
},
262+
"Protocol": "lambda",
263+
"TopicArn": {
264+
"Fn::GetAtt": [
265+
"SNSTopicSlstestprojecttopic",
266+
"Arn"
267+
]
268+
}
269+
}
270+
},
253271
"Testfct1LambdaPermissionSlstestprojecttopicSNS": {
254272
"Type": "AWS::Lambda::Permission",
255273
"Properties": {

test/stackops/snsEvents.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ describe('SNS Events', () => {
7575
return expect(awsAlias.aliasHandleSNSEvents({}, [], {})).to.be.fulfilled
7676
.then(() => BbPromise.all([
7777
expect(snsStack).to.not.have.a.nested.property('Resources.SNSTopicSlstestprojecttopic'),
78+
expect(snsStack).to.not.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'),
7879
expect(snsStack).to.not.have.a.nested.property('Resources.Testfct1LambdaPermissionSlstestprojecttopicSNS'),
7980
expect(aliasStack).to.have.a.nested.property('Resources.SNSTopicSlstestprojecttopic'),
81+
expect(aliasStack).to.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'),
8082
expect(aliasStack).to.have.a.nested.property('Resources.Testfct1LambdaPermissionSlstestprojecttopicSNS'),
8183
]));
8284
});

0 commit comments

Comments
 (0)