/
index.js
433 lines (395 loc) · 15.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
'use strict';
const _ = require('lodash');
const BbPromise = require('bluebird');
const { addCustomResourceToService } = require('../../../../customResources');
class AwsCompileS3Events {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider('aws');
// TODO: Complete schema, see https://github.com/serverless/serverless/issues/8031
this.serverless.configSchemaHandler.defineFunctionEvent('aws', 's3', {
anyOf: [{ type: 'string' }, { type: 'object' }],
});
this.allowedBucketProperties = new Set([
'accelerateConfiguration',
'accessControl',
'analyticsConfigurations',
'bucketEncryption',
'bucketName',
'corsConfiguration',
'inventoryConfigurations',
'lifecycleConfiguration',
'loggingConfiguration',
'metricsConfigurations',
'notificationConfiguration',
'objectLockConfiguration',
'objectLockEnabled',
'publicAccessBlockConfiguration',
'replicationConfiguration',
'tags',
'versioningConfiguration',
'websiteConfiguration',
]);
this.hooks = {
'package:compileEvents': () => {
return BbPromise.bind(this)
.then(this.newS3Buckets)
.then(this.existingS3Buckets);
},
};
}
newS3Buckets() {
const bucketsLambdaConfigurations = {};
const bucketsMeta = {};
const s3EnabledFunctions = [];
const providerS3 = this.serverless.service.provider.s3 || {};
const errorMessageBucketSyntax =
'The correct syntax is: "s3: bucketName or bucketRef or an object with "bucket" property.';
this.serverless.service.getAllFunctions().forEach(functionName => {
const functionObj = this.serverless.service.getFunction(functionName);
if (functionObj.events) {
functionObj.events.forEach(event => {
if (event.s3) {
// return immediately if it's an existing S3 event since we treat them differently
if (event.s3.existing) return null;
let bucketRef;
let notificationEvent = 's3:ObjectCreated:*';
let filter = {};
if (typeof event.s3 === 'object') {
if (!event.s3.bucket) {
const errorMessage = [
`Missing "bucket" property for s3 event in function ${functionName}.`,
errorMessageBucketSyntax,
'Please check the docs for more info.',
].join(' ');
throw new this.serverless.classes.Error(errorMessage);
}
bucketRef = event.s3.bucket;
if (event.s3.event) {
notificationEvent = event.s3.event;
}
if (event.s3.rules) {
if (!Array.isArray(event.s3.rules)) {
const errorMessage = [
`S3 filter rules of function ${functionName} is not an array`,
' The correct syntax is: rules: [{ Name: Value }]',
' Please check the docs for more info.',
].join('');
throw new this.serverless.classes.Error(errorMessage);
}
const rules = [];
event.s3.rules.forEach(rule => {
if (!_.isPlainObject(rule)) {
const errorMessage = [
`S3 filter rule ${rule} of function ${functionName} is not an object`,
' The correct syntax is: { Name: Value }',
' Please check the docs for more info.',
].join('');
throw new this.serverless.classes.Error(errorMessage);
}
const name = Object.keys(rule)[0];
const value = rule[name];
rules.push({ Name: name, Value: value });
});
filter = { Filter: { S3Key: { Rules: rules } } };
}
} else if (typeof event.s3 === 'string') {
bucketRef = event.s3;
} else {
const errorMessage = [
`S3 event of function ${functionName} is not an object nor a string.`,
errorMessageBucketSyntax,
'Please check the docs for more info.',
].join(' ');
throw new this.serverless.classes.Error(errorMessage);
}
const lambdaLogicalId = this.provider.naming.getLambdaLogicalId(functionName);
let bucketName;
if (providerS3[bucketRef]) {
bucketName = providerS3[bucketRef].name || bucketRef;
const logicalId = this.provider.naming.getBucketLogicalId(bucketRef);
bucketsMeta[bucketName] = { logicalId, bucketRef };
} else {
bucketName = bucketRef;
}
if (/[A-Z]/.test(bucketName)) {
const errorMessage = [
`${bucketName} - Bucket name should not contain uppercase characters.`,
`Please check provider.s3.${bucketRef} and/or s3 events of function "${functionName}".`,
].join(' ');
throw new this.serverless.classes.Error(errorMessage);
}
if (bucketsLambdaConfigurations[bucketName]) {
// check if the bucket already defined
// in another S3 event in the service
let newLambdaConfiguration = {
Event: notificationEvent,
Function: {
'Fn::GetAtt': [lambdaLogicalId, 'Arn'],
},
};
// Assign 'filter' if not empty
newLambdaConfiguration = Object.assign(newLambdaConfiguration, filter);
bucketsLambdaConfigurations[bucketName].push(newLambdaConfiguration);
} else {
bucketsLambdaConfigurations[bucketName] = [
{
Event: notificationEvent,
Function: {
'Fn::GetAtt': [lambdaLogicalId, 'Arn'],
},
},
];
// Assign 'filter' if not empty
bucketsLambdaConfigurations[bucketName][0] = Object.assign(
bucketsLambdaConfigurations[bucketName][0],
filter
);
}
const s3EnabledFunction = { functionName, bucketName };
s3EnabledFunctions.push(s3EnabledFunction);
}
return null;
});
}
});
// iterate over all buckets to be created
// and compile them to CF resources
_.forEach(bucketsLambdaConfigurations, (bucketLambdaConfiguration, bucketName) => {
let bucketConf = null;
if (bucketsMeta[bucketName]) {
const providerBucket = providerS3[bucketsMeta[bucketName].bucketRef];
bucketConf = {};
for (const [key, value] of Object.entries(providerBucket)) {
if (key !== 'name') {
if (!this.allowedBucketProperties.has(key)) {
const errorMessage = [
`"${key}" is not a valid bucket property.`,
'A bucket could only be configured with the following properties:\n',
['name'].concat(Array.from(this.allowedBucketProperties)).join(', '),
].join('');
throw new this.serverless.classes.Error(errorMessage);
}
const property = _.upperFirst(key);
bucketConf[property] = value;
}
}
}
let providedNotificationConfiguration = {};
if (bucketConf && Object.keys(bucketConf).length > 0) {
providedNotificationConfiguration = bucketConf.NotificationConfiguration;
delete bucketConf.NotificationConfiguration;
}
const bucketTemplate = {
Type: 'AWS::S3::Bucket',
Properties: Object.assign(
{
BucketName: bucketName,
NotificationConfiguration: Object.assign(
{
LambdaConfigurations: bucketLambdaConfiguration,
},
providedNotificationConfiguration
),
},
bucketConf
),
DependsOn: [],
};
// create the DependsOn properties for the buckets permissions (which are created later on)
const dependsOnToCreate = s3EnabledFunctions.filter(func => func.bucketName === bucketName);
dependsOnToCreate.forEach(item => {
const lambdaPermissionLogicalId = this.provider.naming.getLambdaS3PermissionLogicalId(
item.functionName,
(bucketsMeta[item.bucketName] && bucketsMeta[item.bucketName].bucketRef) ||
item.bucketName
);
bucketTemplate.DependsOn.push(lambdaPermissionLogicalId);
});
const bucketLogicalId =
(bucketsMeta[bucketName] && bucketsMeta[bucketName].logicalId) ||
this.provider.naming.getBucketLogicalId(bucketName);
const bucketCFResource = {
[bucketLogicalId]: bucketTemplate,
};
_.merge(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
bucketCFResource
);
});
// iterate over all functions with S3 events
// and give S3 permission to invoke them all
// by adding Lambda::Permission resource for each
s3EnabledFunctions.forEach(s3EnabledFunction => {
const lambdaLogicalId = this.provider.naming.getLambdaLogicalId(
s3EnabledFunction.functionName
);
const permissionTemplate = {
Type: 'AWS::Lambda::Permission',
Properties: {
FunctionName: {
'Fn::GetAtt': [lambdaLogicalId, 'Arn'],
},
Action: 'lambda:InvokeFunction',
Principal: 's3.amazonaws.com',
SourceArn: {
'Fn::Join': [
'',
['arn:', { Ref: 'AWS::Partition' }, `:s3:::${s3EnabledFunction.bucketName}`],
],
},
SourceAccount: { Ref: 'AWS::AccountId' },
},
};
const lambdaPermissionLogicalId = this.provider.naming.getLambdaS3PermissionLogicalId(
s3EnabledFunction.functionName,
(bucketsMeta[s3EnabledFunction.bucketName] &&
bucketsMeta[s3EnabledFunction.bucketName].bucketRef) ||
s3EnabledFunction.bucketName
);
const permissionCFResource = {
[lambdaPermissionLogicalId]: permissionTemplate,
};
_.merge(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
permissionCFResource
);
});
}
existingS3Buckets() {
const { service } = this.serverless;
const { provider } = service;
const { compiledCloudFormationTemplate } = provider;
const { Resources } = compiledCloudFormationTemplate;
const iamRoleStatements = [];
// used to keep track of the custom resources created for each bucket
const bucketResources = {};
service.getAllFunctions().forEach(functionName => {
let numEventsForFunc = 0;
let currentBucketName = null;
let funcUsesExistingS3Bucket = false;
const functionObj = service.getFunction(functionName);
const FunctionName = functionObj.name;
if (functionObj.events) {
functionObj.events.forEach(event => {
if (event.s3 && _.isObject(event.s3) && event.s3.existing) {
numEventsForFunc++;
let rules = null;
const bucket = event.s3.bucket;
const notificationEvent = event.s3.event || 's3:ObjectCreated:*';
funcUsesExistingS3Bucket = true;
if (!currentBucketName) {
currentBucketName = bucket;
}
if (bucket !== currentBucketName) {
const errorMessage = [
'Only one S3 Bucket can be configured per function.',
` In "${FunctionName}" you're attempting to configure "${currentBucketName}" and "${bucket}" at the same time.`,
].join('');
throw new this.serverless.classes.Error(errorMessage);
}
rules = (event.s3.rules || []).map(rule => {
const key = Object.keys(rule)[0];
const value = rule[key];
return {
[_.startCase(key)]: value,
};
});
const eventFunctionLogicalId = this.provider.naming.getLambdaLogicalId(functionName);
const customResourceFunctionLogicalId = this.provider.naming.getCustomResourceS3HandlerFunctionLogicalId();
const customS3ResourceLogicalId = this.provider.naming.getCustomResourceS3ResourceLogicalId(
functionName
);
// store how often the custom S3 resource is used
if (bucketResources[bucket]) {
bucketResources[bucket] = _.union(bucketResources[bucket], [
customS3ResourceLogicalId,
]);
} else {
Object.assign(bucketResources, {
[bucket]: [customS3ResourceLogicalId],
});
}
let customS3Resource;
if (numEventsForFunc === 1) {
customS3Resource = {
[customS3ResourceLogicalId]: {
Type: 'Custom::S3',
Version: 1.0,
DependsOn: [eventFunctionLogicalId, customResourceFunctionLogicalId],
Properties: {
ServiceToken: {
'Fn::GetAtt': [customResourceFunctionLogicalId, 'Arn'],
},
FunctionName,
BucketName: bucket,
BucketConfigs: [
{
Event: notificationEvent,
Rules: rules,
},
],
},
},
};
iamRoleStatements.push({
Effect: 'Allow',
Resource: {
'Fn::Join': [':', ['arn', { Ref: 'AWS::Partition' }, 's3', '', '', bucket]],
},
Action: ['s3:PutBucketNotification', 's3:GetBucketNotification'],
});
} else {
Resources[customS3ResourceLogicalId].Properties.BucketConfigs.push({
Event: notificationEvent,
Rules: rules,
});
}
_.merge(Resources, customS3Resource);
}
});
}
if (funcUsesExistingS3Bucket) {
iamRoleStatements.push({
Effect: 'Allow',
Resource: {
'Fn::Join': [
':',
[
'arn',
{ Ref: 'AWS::Partition' },
'lambda',
{ Ref: 'AWS::Region' },
{ Ref: 'AWS::AccountId' },
'function',
FunctionName,
],
],
},
Action: ['lambda:AddPermission', 'lambda:RemovePermission'],
});
}
});
// check if we need to add DependsOn clauses in case more than 1
// custom resources are created for one bucket (to avoid race conditions)
if (Object.keys(bucketResources).length > 0) {
Object.keys(bucketResources).forEach(bucket => {
const resources = bucketResources[bucket];
if (resources.length > 1) {
resources.forEach((currResourceLogicalId, idx) => {
if (idx > 0) {
const prevResourceLogicalId = resources[idx - 1];
Resources[currResourceLogicalId].DependsOn.push(prevResourceLogicalId);
}
});
}
});
}
if (iamRoleStatements.length) {
return addCustomResourceToService(this.provider, 's3', iamRoleStatements);
}
return null;
}
}
module.exports = AwsCompileS3Events;