Skip to content

Check if more than 1 existing bucket is configured #6506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/plugins/aws/package/compile/events/s3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class AwsCompileS3Events {

service.getAllFunctions().forEach(functionName => {
let numEventsForFunc = 0;
let currentBucketName = null;
let funcUsesExistingS3Bucket = false;
const functionObj = service.getFunction(functionName);
const FunctionName = functionObj.name;
Expand All @@ -221,6 +222,17 @@ class AwsCompileS3Events {
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 = _.map(event.s3.rules, rule => {
const key = Object.keys(rule)[0];
const value = rule[key];
Expand Down
24 changes: 24 additions & 0 deletions lib/plugins/aws/package/compile/events/s3/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,5 +741,29 @@ describe('AwsCompileS3Events', () => {
});
});
});

it('should throw if more than 1 S3 bucket is configured per function', () => {
awsCompileS3Events.serverless.service.functions = {
first: {
name: 'second',
events: [
{
s3: {
bucket: 'existing-s3-bucket',
existing: true,
},
},
{
s3: {
bucket: 'existing-s3-bucket-2',
existing: true,
},
},
],
},
};

return expect(() => awsCompileS3Events.existingS3Buckets()).to.throw('Only one S3 Bucket');
});
});
});