Skip to content

Commit

Permalink
Merge pull request #95 from zachboyd/sns-subscription-fix
Browse files Browse the repository at this point in the history
Added support to have an alias subscribe to an existing SNS topic #94
  • Loading branch information
HyperBrain authored Jan 25, 2018
2 parents dde216e + 24992eb commit 87b12a3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/stackops/snsEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
delete stageStack.Resources[name];
});

const snsSubscriptions =
_.assign({},
_.pickBy(stageStack.Resources, [ 'Type', 'AWS::SNS::Subscription' ]));

_.forOwn(snsSubscriptions, (subscription, name) => {

const functionNameRef = utils.findAllReferences(_.get(subscription.Properties, 'Endpoint'));
const functionName = _.replace(_.get(functionNameRef, '[0].ref', ''), /LambdaFunction$/, '');
const versionName = _.find(_.keys(versions), version => _.startsWith(version, functionName));
const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, functionName));

subscription.Properties.Endpoint = { Ref: aliasName };
subscription.DependsOn = [ versionName, aliasName ];

delete stageStack.Resources[name];
});

// Fetch lambda permissions. These have to be updated later to allow the aliased functions.
const snsLambdaPermissions =
_.assign({},
Expand All @@ -71,6 +88,7 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac

// Add all alias stack owned resources
aliasResources.push(snsTopics);
aliasResources.push(snsSubscriptions);
aliasResources.push(snsLambdaPermissions);

_.forEach(aliasResources, resource => _.assign(aliasStack.Resources, resource));
Expand Down
18 changes: 18 additions & 0 deletions test/data/sns-stack.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,24 @@
]
}
},
"SNSTopicSubscriptionSlstestprojecttopic": {
"Type" : "AWS::SNS::Subscription",
"Properties": {
"Endpoint": {
"Fn::GetAtt": [
"Testfct1LambdaFunction",
"Arn"
]
},
"Protocol": "lambda",
"TopicArn": {
"Fn::GetAtt": [
"SNSTopicSlstestprojecttopic",
"Arn"
]
}
}
},
"Testfct1LambdaPermissionSlstestprojecttopicSNS": {
"Type": "AWS::Lambda::Permission",
"Properties": {
Expand Down
2 changes: 2 additions & 0 deletions test/stackops/snsEvents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ describe('SNS Events', () => {
return expect(awsAlias.aliasHandleSNSEvents({}, [], {})).to.be.fulfilled
.then(() => BbPromise.all([
expect(snsStack).to.not.have.a.nested.property('Resources.SNSTopicSlstestprojecttopic'),
expect(snsStack).to.not.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'),
expect(snsStack).to.not.have.a.nested.property('Resources.Testfct1LambdaPermissionSlstestprojecttopicSNS'),
expect(aliasStack).to.have.a.nested.property('Resources.SNSTopicSlstestprojecttopic'),
expect(aliasStack).to.have.a.nested.property('Resources.SNSTopicSubscriptionSlstestprojecttopic'),
expect(aliasStack).to.have.a.nested.property('Resources.Testfct1LambdaPermissionSlstestprojecttopicSNS'),
]));
});
Expand Down

0 comments on commit 87b12a3

Please sign in to comment.