Skip to content

Fix issue with alias remove for large templates #150

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class AwsAlias {

'alias:remove:remove': () => BbPromise.bind(this)
.then(this.validate)
.then(this.setBucketName)
.then(this.aliasStackLoadCurrentCFStackAndDependencies)
.spread(this.removeAlias)
};
Expand Down
42 changes: 38 additions & 4 deletions lib/removeAlias.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const _ = require('lodash');
const utils = require('./utils');

const NO_UPDATE_MESSAGE = 'No updates are to be performed.';
const TEMPLATE_MIN_SIZE_FOR_UPLOAD = 51200;

module.exports = {

Expand Down Expand Up @@ -135,7 +136,7 @@ module.exports = {
});
},

aliasApplyStackChanges(currentTemplate, aliasStackTemplates, currentAliasStackTemplate) {
aliasApplyStackChanges(currentTemplate, aliasStackTemplates, currentAliasStackTemplate, templateUrl) {

const stackName = this._provider.naming.getStackName();

Expand All @@ -155,10 +156,15 @@ module.exports = {
'CAPABILITY_NAMED_IAM',
],
Parameters: [],
TemplateBody: JSON.stringify(currentTemplate),
Tags: _.map(_.keys(stackTags), key => ({ Key: key, Value: stackTags[key] })),
};

if (templateUrl) {
params.TemplateURL = templateUrl;
} else {
params.TemplateBody = JSON.stringify(currentTemplate);
}

this.options.verbose && this._serverless.cli.log(`Checking stack policy`);

// Policy must have at least one statement, otherwise no updates would be possible at all
Expand All @@ -185,6 +191,35 @@ module.exports = {

},

uploadCloudFormationTemplate(currentTemplate, aliasStackTemplates, currentAliasStackTemplate) {
const templateSize = JSON.stringify(currentTemplate).length;
if (templateSize < TEMPLATE_MIN_SIZE_FOR_UPLOAD) {
this.serverless.cli.log(`Skipping Upload of CloudFormation alias file to S3, size is only ${templateSize}`);
return BbPromise.resolve([ currentTemplate, aliasStackTemplates, currentAliasStackTemplate]);
}
this.serverless.cli.log('Uploading CloudFormation alias file to S3...');
const body = JSON.stringify(currentTemplate);

const fileName = 'compiled-cloudformation-template-alias.json';

let params = {
Bucket: this.bucketName,
Key: `${this.serverless.service.package.artifactDirectoryName}/${fileName}`,
Body: body,
ContentType: 'application/json',
};

return this.provider.request('S3',
'putObject',
params,
this._options.stage,
this._options.region)
.then(() => {
const templateUrl = `https://s3.amazonaws.com/${this.bucketName}/${this._serverless.service.package.artifactDirectoryName}/compiled-cloudformation-template-alias.json`;
return BbPromise.resolve([ currentTemplate, aliasStackTemplates, currentAliasStackTemplate, templateUrl ]);
});
},

aliasRemoveAliasStack(currentTemplate, aliasStackTemplates, currentAliasStackTemplate) {

const stackName = `${this._provider.naming.getStackName()}-${this._alias}`;
Expand Down Expand Up @@ -245,9 +280,8 @@ module.exports = {
return BbPromise.resolve([ currentTemplate, aliasStackTemplates, currentAliasStackTemplate ]).bind(this)
.spread(this.aliasCreateStackChanges)
.spread(this.aliasRemoveAliasStack)
.spread(this.uploadCloudFormationTemplate)
.spread(this.aliasApplyStackChanges)
.then(() => BbPromise.resolve());

}

};
Loading