Merge pull request #6652 from serverless/fix-cloudfront-removal-logging

Use hooks to log Lambda@Edge removal reminder
This commit is contained in:
Philipp Muens 2019-09-09 14:00:02 +02:00 committed by GitHub
commit c0ec32b4e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 20 deletions

View File

@ -14,27 +14,27 @@ class AwsCompileCloudFrontEvents {
'package:initialize': this.validate.bind(this),
'before:package:compileFunctions': this.prepareFunctions.bind(this),
'package:compileEvents': this.compileCloudFrontEvents.bind(this),
'before:remove:remove': this.logRemoveReminder.bind(this),
};
if (this.serverless.processedInput.commands[0] === 'remove') {
this.logRemoveReminder.call(this);
}
}
logRemoveReminder() {
let isEventUsed = false;
const funcKeys = this.serverless.service.getAllFunctions();
if (funcKeys.length) {
isEventUsed = funcKeys.some(funcKey => {
const func = this.serverless.service.getFunction(funcKey);
return func.events && func.events.find(e => Object.keys(e)[0] === 'cloudFront');
});
}
if (isEventUsed) {
const message = [
"Don't forget to manually remove your Lambda@Edge functions ",
'once the CloudFront distribution removal is successfully propagated!',
].join('');
this.serverless.cli.log(message, 'Serverless', { color: 'orange' });
if (this.serverless.processedInput.commands[0] === 'remove') {
let isEventUsed = false;
const funcKeys = this.serverless.service.getAllFunctions();
if (funcKeys.length) {
isEventUsed = funcKeys.some(funcKey => {
const func = this.serverless.service.getFunction(funcKey);
return func.events && func.events.find(e => Object.keys(e)[0] === 'cloudFront');
});
}
if (isEventUsed) {
const message = [
"Don't forget to manually remove your Lambda@Edge functions ",
'once the CloudFront distribution removal is successfully propagated!',
].join('');
this.serverless.cli.log(message, 'Serverless', { color: 'orange' });
}
}
}

View File

@ -82,15 +82,15 @@ describe('AwsCompileCloudFrontEvents', () => {
it('should set the provider variable to an instance of AwsProvider', () =>
expect(awsCompileCloudFrontEvents.provider).to.be.instanceof(AwsProvider));
it('should log an info message if the users wants to remove the stack with a cloudFront event', () => {
it('should use "before:remove:remove" hook to log a message before removing the service', () => {
serverless.processedInput.commands = ['remove'];
serverless.service.functions = {
first: {
events: [
{
cloudFront: {
eventType: 'viewer-response',
origin: 's3://some-bucket.s3.amazonaws.com/files',
eventType: 'viewer-request',
origin: 's3://bucketname.s3.amazonaws.com/files',
},
},
],
@ -98,6 +98,7 @@ describe('AwsCompileCloudFrontEvents', () => {
};
const awsCompileCloudFrontEventsRemoval = new AwsCompileCloudFrontEvents(serverless, options);
awsCompileCloudFrontEventsRemoval.hooks['before:remove:remove']();
expect(awsCompileCloudFrontEventsRemoval.serverless.cli.log).to.have.been.calledOnce;
expect(awsCompileCloudFrontEventsRemoval.serverless.cli.log.args[0][0]).to.include(
'remove your Lambda@Edge functions'