Skip to content

Commit ae34a57

Browse files
committed
event sources: adding permissions
1 parent 53a2ef7 commit ae34a57

File tree

5 files changed

+123
-57
lines changed

5 files changed

+123
-57
lines changed

lib/actions/EventDeployS3Lambda.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ module.exports = function(SPlugin, serverlessPath) {
5252
let event = _this.S.state.getEvents({ paths: [_this.evt.options.path] })[0],
5353
populatedEvent = event.getPopulated({stage: _this.evt.options.stage, region: _this.evt.options.region}),
5454
functionName = _this.S.state.getFunctions({paths: [_this.evt.options.path.split('#')[0]]})[0].getDeployedName(_this.evt.options),
55+
statementId = 'sEvents-' + functionName + '-' + event.name + '-' + _this.evt.options.stage,
5556
awsAccountId = _this.S.state.meta.get().stages[_this.evt.options.stage].regions[_this.evt.options.region].variables.iamRoleArnLambda.split('::')[1].split(':')[0],
56-
lambdaArn = 'arn:aws:lambda:' + _this.evt.options.region + ':' + awsAccountId + ':function:' + functionName;
57+
lambdaArn = 'arn:aws:lambda:' + _this.evt.options.region + ':' + awsAccountId + ':function:' + functionName + ':' + _this.evt.options.stage;
5758

5859
let awsConfig = {
5960
region: _this.evt.options.region,
@@ -62,20 +63,47 @@ module.exports = function(SPlugin, serverlessPath) {
6263
};
6364

6465
_this.S3 = require('../utils/aws/S3')(awsConfig);
66+
_this.Lambda = require('../utils/aws/Lambda')(awsConfig);
6567

6668
let params = {
67-
Bucket: populatedEvent.config.bucket,
68-
NotificationConfiguration: {
69-
LambdaFunctionConfigurations: [
70-
{
71-
Events: populatedEvent.config.bucketEvents,
72-
LambdaFunctionArn: lambdaArn
73-
}
74-
]
75-
}
69+
FunctionName: lambdaArn,
70+
StatementId: statementId,
71+
Qualifier: _this.evt.options.stage
7672
};
73+
return _this.Lambda.removePermissionPromised(params)
74+
.then(function(data) {
75+
SUtils.sDebug(`Removed lambda permission with statement ID: ${statementId}`);
76+
})
77+
.catch(function(error) {})
78+
.then(function (data) {
79+
80+
SUtils.sDebug(`Adding lambda permission with statement ID: ${statementId}`);
81+
82+
let params = {
83+
FunctionName: lambdaArn,
84+
StatementId: statementId,
85+
Action: 'lambda:InvokeFunction',
86+
Principal: 's3.amazonaws.com',
87+
SourceArn: 'arn:aws:s3:::' + populatedEvent.config.bucket,
88+
Qualifier: _this.evt.options.stage
89+
};
90+
return _this.Lambda.addPermissionPromised(params);
91+
})
92+
.then(function(data) {
93+
let params = {
94+
Bucket: populatedEvent.config.bucket,
95+
NotificationConfiguration: {
96+
LambdaFunctionConfigurations: [
97+
{
98+
Events: populatedEvent.config.bucketEvents,
99+
LambdaFunctionArn: lambdaArn
100+
}
101+
]
102+
}
103+
};
77104

78-
return _this.S3.putBucketNotificationConfigurationPromised(params)
105+
return _this.S3.putBucketNotificationConfigurationPromised(params)
106+
})
79107
.then(function(data) {
80108

81109
SUtils.sDebug(`Put notification configuration for bucket ${populatedEvent.config.bucket} and lambda ${lambdaArn}`);

lib/actions/EventDeploySNSLambda.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ module.exports = function(SPlugin, serverlessPath) {
5353
let event = _this.S.state.getEvents({ paths: [_this.evt.options.path] })[0],
5454
populatedEvent = event.getPopulated({stage: _this.evt.options.stage, region: _this.evt.options.region}),
5555
functionName = _this.S.state.getFunctions({paths: [_this.evt.options.path.split('#')[0]]})[0].getDeployedName(_this.evt.options),
56+
statementId = 'sEvents-' + functionName + '-' + event.name + '-' + _this.evt.options.stage,
5657
awsAccountId = _this.S.state.meta.get().stages[_this.evt.options.stage].regions[_this.evt.options.region].variables.iamRoleArnLambda.split('::')[1].split(':')[0],
58+
topicArn = 'arn:aws:sns:' + _this.evt.options.region + ':' + awsAccountId + ':' + populatedEvent.config.topicName,
5759
lambdaArn = 'arn:aws:lambda:' + _this.evt.options.region + ':' + awsAccountId + ':function:' + functionName + ':' + _this.evt.options.stage;
5860

5961
let awsConfig = {
@@ -63,23 +65,47 @@ module.exports = function(SPlugin, serverlessPath) {
6365
};
6466

6567
_this.SNS = require('../utils/aws/SNS')(awsConfig);
68+
_this.Lambda = require('../utils/aws/Lambda')(awsConfig);
69+
6670

6771
let params = {
68-
Protocol: 'lambda',
69-
TopicArn: populatedEvent.config.topicArn,
70-
Endpoint: lambdaArn
72+
FunctionName: lambdaArn,
73+
StatementId: statementId,
74+
Qualifier: _this.evt.options.stage
7175
};
72-
73-
return _this.SNS.subscribePromised(params)
76+
return _this.Lambda.removePermissionPromised(params)
77+
.then(function(data) {
78+
SUtils.sDebug(`Removed lambda permission with statement ID: ${statementId}`);
79+
})
80+
.catch(function(error) {})
81+
.then(function (data) {
82+
83+
SUtils.sDebug(`Adding lambda permission with statement ID: ${statementId}`);
84+
85+
let params = {
86+
FunctionName: lambdaArn,
87+
StatementId: statementId,
88+
Action: 'lambda:InvokeFunction',
89+
Principal: 'sns.amazonaws.com',
90+
Qualifier: _this.evt.options.stage
91+
};
92+
return _this.Lambda.addPermissionPromised(params);
93+
})
94+
.then(function(data) {
95+
let params = {
96+
Protocol: 'lambda',
97+
TopicArn: topicArn,
98+
Endpoint: lambdaArn
99+
};
100+
101+
return _this.SNS.subscribePromised(params)
102+
})
74103
.then(function(data){
75-
76-
SUtils.sDebug(`Subscription to SNS topic ${populatedEvent.config.topicArn} added for lambda ${lambdaArn}`);
77-
104+
SUtils.sDebug(`Subscription to SNS topic ${topicArn} added for lambda ${lambdaArn}`);
78105
return BbPromise.resolve(data);
79106
});
80107
}
81108
}
82109

83-
84110
return( EventDeploySNSLambda );
85111
};

lib/actions/EventDeployScheduledLambda.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,17 @@ module.exports = function (SPlugin, serverlessPath) {
6868
_this.CloudWatchEvents = require('../utils/aws/CloudWatchEvents')(awsConfig);
6969
_this.Lambda = require('../utils/aws/Lambda')(awsConfig);
7070

71+
SUtils.sDebug(`Putting CloudWatchEvents Rule ${ruleName}`);
72+
7173
var params = {
7274
Name: ruleName,
7375
ScheduleExpression: populatedEvent.config.schedule,
7476
State: populatedEvent.config.enabled
7577
};
7678

7779
return _this.CloudWatchEvents.putRuleAsync(params)
78-
.then(function (data) {
7980

81+
.then(function (data) {
8082
// First remove permissions so we can set them again
8183
let params = {
8284
FunctionName: lambdaArn,
@@ -85,19 +87,18 @@ module.exports = function (SPlugin, serverlessPath) {
8587
};
8688
return _this.Lambda.removePermissionPromised(params)
8789
.then(function(data) {
88-
89-
SUtils.sDebug(
90-
'"'
91-
+ _this.evt.options.stage + ' - '
92-
+ _this.evt.options.region
93-
+ ' - ' + ruleName + '": '
94-
+ 'removed existing lambda access policy statement');
90+
SUtils.sDebug(
91+
'"'
92+
+ _this.evt.options.stage + ' - '
93+
+ _this.evt.options.region
94+
+ ' - ' + ruleName + '": '
95+
+ 'removed existing lambda access policy statement');
9596
})
96-
.catch(function(error) {});
97+
.catch(function(error) {})
9798
})
9899
.then(function (data) {
99100

100-
SUtils.sDebug(`Add Permissions for Event Rule ${ruleName}`);
101+
SUtils.sDebug(`Adding Permissions for Event Rule ${ruleName}`);
101102

102103
let params = {
103104
FunctionName: lambdaArn,
@@ -110,7 +111,7 @@ module.exports = function (SPlugin, serverlessPath) {
110111
})
111112
.then(function (data) {
112113

113-
SUtils.sDebug(`Put CloudWatchEvents Rule ${ruleName}`);
114+
SUtils.sDebug(`Setting lambda ${lambdaArn}:${stage} as target for rule ${ruleName} for lambda ${functionName}`);
114115

115116
let params = {
116117
Rule: ruleName,
@@ -121,15 +122,26 @@ module.exports = function (SPlugin, serverlessPath) {
121122
}
122123
]
123124
};
124-
return _this.CloudWatchEvents.putTargetsAsync(params);
125-
})
126-
.then(function (data) {
127-
SUtils.sDebug(`Set lambda ${lambdaArn}:${stage} as target for rule ${ruleName} for lambda ${functionName}`);
128-
129-
return BbPromise.resolve(data);
125+
return _this.CloudWatchEvents.putTargetsAsync(params)
126+
.then(function(data){
127+
return BbPromise.resolve(data);
128+
});
130129
});
131130
}
132131
}
133132

134133
return ( EventDeployScheduledLambda );
135-
};
134+
};
135+
136+
/*
137+
aws lambda add-permission \
138+
--function-name serverless-v193k-js-fun \
139+
--qualifier dev \
140+
--region us-east-1 \
141+
--statement-id qwertyjmb \
142+
--action "lambda:InvokeFunction" \
143+
--principal events.amazonaws.com \
144+
--source-arn arn:aws:events:us-east-1:552750238299:rule/serverless-v193k-js-fun-schedule \
145+
--source-account 552750238299 \
146+
--profile default
147+
*/

tests/all.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ describe('All Tests', function() {
1515
require('./tests/classes/ServerlessComponentTest');
1616
require('./tests/classes/ServerlessFunctionTest');
1717
require('./tests/classes/ServerlessEndpointTest');
18-
//require('./tests/actions/TestPluginCustom');
19-
//require('./tests/actions/TestDefaultActionHook');
20-
//require('./tests/actions/StageCreate');
21-
//require('./tests/actions/RegionCreate');
22-
//require('./tests/actions/ComponentCreate');
23-
//require('./tests/actions/FunctionCreate');
24-
//require('./tests/actions/EnvList');
25-
//require('./tests/actions/EnvGet');
26-
//require('./tests/actions/EnvSetUnset');
27-
//require('./tests/actions/ResourcesDeploy');
28-
//require('./tests/actions/FunctionRun');
29-
//require('./tests/actions/FunctionLogs');
30-
//require('./tests/actions/FunctionDeploy');
31-
//require('./tests/actions/EndpointDeploy');
32-
//require('./tests/actions/EventDeploy');
33-
//require('./tests/actions/ProjectInit');
34-
//require('./tests/actions/ProjectInstall');
35-
//require('./tests/actions/ProjectLifeCycle.js');
18+
require('./tests/actions/TestPluginCustom');
19+
require('./tests/actions/TestDefaultActionHook');
20+
require('./tests/actions/StageCreate');
21+
require('./tests/actions/RegionCreate');
22+
require('./tests/actions/ComponentCreate');
23+
require('./tests/actions/FunctionCreate');
24+
require('./tests/actions/EnvList');
25+
require('./tests/actions/EnvGet');
26+
require('./tests/actions/EnvSetUnset');
27+
require('./tests/actions/ResourcesDeploy');
28+
require('./tests/actions/FunctionRun');
29+
require('./tests/actions/FunctionLogs');
30+
require('./tests/actions/FunctionDeploy');
31+
require('./tests/actions/EndpointDeploy');
32+
require('./tests/actions/EventDeploy');
33+
require('./tests/actions/ProjectInit');
34+
require('./tests/actions/ProjectInstall');
35+
require('./tests/actions/ProjectLifeCycle.js');
3636
});

tests/test-prj/nodejscomponent/group1/function1/s-function.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"name": "sns",
2828
"type": "sns",
2929
"config": {
30-
"topicArn": "${topicArn}"
30+
"topicName": "test-event-source"
3131
}
3232
},
3333
{

0 commit comments

Comments
 (0)