@@ -5,6 +5,7 @@ const _ = require('lodash');
55const utils = require ( './utils' ) ;
66
77const NO_UPDATE_MESSAGE = 'No updates are to be performed.' ;
8+ const TEMPLATE_MIN_SIZE_FOR_UPLOAD = 51200 ;
89
910module . exports = {
1011
@@ -135,7 +136,7 @@ module.exports = {
135136 } ) ;
136137 } ,
137138
138- aliasApplyStackChanges ( currentTemplate , aliasStackTemplates , currentAliasStackTemplate ) {
139+ aliasApplyStackChanges ( currentTemplate , aliasStackTemplates , currentAliasStackTemplate , templateUrl ) {
139140
140141 const stackName = this . _provider . naming . getStackName ( ) ;
141142
@@ -155,15 +156,19 @@ module.exports = {
155156 'CAPABILITY_NAMED_IAM' ,
156157 ] ,
157158 Parameters : [ ] ,
158- TemplateBody : JSON . stringify ( currentTemplate ) ,
159159 Tags : _ . map ( _ . keys ( stackTags ) , key => ( { Key : key , Value : stackTags [ key ] } ) ) ,
160160 } ;
161161
162+ if ( templateUrl ) {
163+ params . TemplateURL = templateUrl ;
164+ } else {
165+ params . TemplateBody = JSON . stringify ( currentTemplate ) ;
166+ }
167+
162168 this . options . verbose && this . _serverless . cli . log ( `Checking stack policy` ) ;
163169
164170 // Policy must have at least one statement, otherwise no updates would be possible at all
165- if ( this . serverless . service . provider . stackPolicy &&
166- this . serverless . service . provider . stackPolicy . length ) {
171+ if ( this . serverless . service . provider . stackPolicy && this . serverless . service . provider . stackPolicy . length ) {
167172 params . StackPolicyBody = JSON . stringify ( {
168173 Statement : this . serverless . service . provider . stackPolicy ,
169174 } ) ;
@@ -185,8 +190,38 @@ module.exports = {
185190
186191 } ,
187192
193+ uploadCloudFormationTemplate ( currentTemplate , aliasStackTemplates , currentAliasStackTemplate ) {
194+ const templateSize = JSON . stringify ( currentTemplate ) . length ;
195+ if ( templateSize < TEMPLATE_MIN_SIZE_FOR_UPLOAD ) {
196+ this . serverless . cli . log ( `Skipping Upload of CloudFormation alias file to S3, size is only ${ templateSize } ` ) ;
197+ return BbPromise . resolve ( [ currentTemplate , aliasStackTemplates , currentAliasStackTemplate ] ) ;
198+ }
199+ this . serverless . cli . log ( 'Uploading CloudFormation alias file to S3...' ) ;
200+ const body = JSON . stringify ( currentTemplate ) ;
201+
202+ const fileName = 'compiled-cloudformation-template-alias.json' ;
203+
204+ let params = {
205+ Bucket : this . bucketName ,
206+ Key : `${ this . serverless . service . package . artifactDirectoryName } /${ fileName } ` ,
207+ Body : body ,
208+ ContentType : 'application/json' ,
209+ } ;
210+
211+ return this . provider . request ( 'S3' ,
212+ 'putObject' ,
213+ params ,
214+ this . _options . stage ,
215+ this . _options . region )
216+ . then ( ( ) => {
217+ const templateUrl = `https://s3.amazonaws.com/${ this . bucketName } /${ this . _serverless . service . package . artifactDirectoryName } /compiled-cloudformation-template-alias.json` ;
218+ return BbPromise . resolve ( [ currentTemplate , aliasStackTemplates , currentAliasStackTemplate , templateUrl ] ) ;
219+ } ) ;
220+ } ,
221+
188222 aliasRemoveAliasStack ( currentTemplate , aliasStackTemplates , currentAliasStackTemplate ) {
189223
224+
190225 const stackName = `${ this . _provider . naming . getStackName ( ) } -${ this . _alias } ` ;
191226
192227 this . options . verbose && this . _serverless . cli . log ( `Removing CF stack ${ stackName } ` ) ;
@@ -245,9 +280,8 @@ module.exports = {
245280 return BbPromise . resolve ( [ currentTemplate , aliasStackTemplates , currentAliasStackTemplate ] ) . bind ( this )
246281 . spread ( this . aliasCreateStackChanges )
247282 . spread ( this . aliasRemoveAliasStack )
283+ . spread ( this . uploadCloudFormationTemplate )
248284 . spread ( this . aliasApplyStackChanges )
249285 . then ( ( ) => BbPromise . resolve ( ) ) ;
250-
251286 }
252-
253287} ;
0 commit comments