mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
Add this check so that the forEach loop won't fail if the user has not setup an S3 event.
96 lines
2.8 KiB
JavaScript
96 lines
2.8 KiB
JavaScript
'use strict';
|
|
|
|
const merge = require('lodash').merge;
|
|
|
|
class AwsCompileS3Events {
|
|
constructor(serverless, options) {
|
|
this.serverless = serverless;
|
|
this.options = options;
|
|
|
|
this.hooks = {
|
|
'deploy:compileEvents': this.compileS3Events.bind(this),
|
|
};
|
|
}
|
|
|
|
compileS3Events() {
|
|
if (!this.serverless.service.resources.aws.Resources) {
|
|
throw new this.serverless.Error(
|
|
'This plugin needs access to Resources section of the AWS CloudFormation template');
|
|
}
|
|
|
|
const bucketTemplate = `
|
|
{
|
|
"Type": "AWS::S3::Bucket",
|
|
"Properties": {
|
|
"BucketName": "BucketName",
|
|
"NotificationConfiguration": "NotificationConfiguration"
|
|
}
|
|
}
|
|
`;
|
|
|
|
const permissionTemplate = `
|
|
{
|
|
"Type": "AWS::Lambda::Permission",
|
|
"Properties": {
|
|
"FunctionName": "FunctionName",
|
|
"Action": "lambda:InvokeFunction",
|
|
"Principal": "s3.amazonaws.com"
|
|
}
|
|
}
|
|
`;
|
|
|
|
// iterate over all defined functions
|
|
this.serverless.service.getAllFunctions().forEach((functionName) => {
|
|
const s3BucketObject = this.serverless.service.getFunction(functionName);
|
|
|
|
if (s3BucketObject.events.aws.s3) {
|
|
// iterate over all defined buckets
|
|
s3BucketObject.events.aws.s3.forEach((bucketName) => {
|
|
// 1. create the S3 bucket with the corresponding notification
|
|
const newS3Bucket = JSON.parse(bucketTemplate);
|
|
newS3Bucket.Properties.BucketName = `${this.serverless.service.service}-${bucketName}-${
|
|
this.options.stage}-${this.options.region}`;
|
|
newS3Bucket.Properties.NotificationConfiguration = {
|
|
LambdaConfigurations: [
|
|
{
|
|
Event: 's3:ObjectCreated:*',
|
|
Function: {
|
|
'Fn::GetAtt': [
|
|
functionName,
|
|
'Arn',
|
|
],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
const bucketResourceKey = bucketName.replace(/-/g, '');
|
|
|
|
const newBucketObject = {
|
|
[bucketResourceKey]: newS3Bucket,
|
|
};
|
|
|
|
// 2. create the corresponding Lambda permissions
|
|
const newPermission = JSON.parse(permissionTemplate);
|
|
newPermission.Properties.FunctionName = {
|
|
'Fn::GetAtt': [
|
|
functionName,
|
|
'Arn',
|
|
],
|
|
};
|
|
|
|
const newPermissionObject = {
|
|
[`${bucketResourceKey}Permission`]: newPermission,
|
|
};
|
|
|
|
// merge the new bucket and permission objects into the Resources section
|
|
merge(this.serverless.service.resources.aws.Resources,
|
|
newBucketObject, newPermissionObject);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = AwsCompileS3Events;
|