@@ -5,6 +5,7 @@ const _ = require('lodash');
5
5
const utils = require ( './utils' ) ;
6
6
7
7
const NO_UPDATE_MESSAGE = 'No updates are to be performed.' ;
8
+ const TEMPLATE_MIN_SIZE_FOR_UPLOAD = 51200 ;
8
9
9
10
module . exports = {
10
11
@@ -135,7 +136,7 @@ module.exports = {
135
136
} ) ;
136
137
} ,
137
138
138
- aliasApplyStackChanges ( currentTemplate , aliasStackTemplates , currentAliasStackTemplate ) {
139
+ aliasApplyStackChanges ( currentTemplate , aliasStackTemplates , currentAliasStackTemplate , templateUrl ) {
139
140
140
141
const stackName = this . _provider . naming . getStackName ( ) ;
141
142
@@ -155,15 +156,19 @@ module.exports = {
155
156
'CAPABILITY_NAMED_IAM' ,
156
157
] ,
157
158
Parameters : [ ] ,
158
- TemplateBody : JSON . stringify ( currentTemplate ) ,
159
159
Tags : _ . map ( _ . keys ( stackTags ) , key => ( { Key : key , Value : stackTags [ key ] } ) ) ,
160
160
} ;
161
161
162
+ if ( templateUrl ) {
163
+ params . TemplateURL = templateUrl ;
164
+ } else {
165
+ params . TemplateBody = JSON . stringify ( currentTemplate ) ;
166
+ }
167
+
162
168
this . options . verbose && this . _serverless . cli . log ( `Checking stack policy` ) ;
163
169
164
170
// 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 ) {
167
172
params . StackPolicyBody = JSON . stringify ( {
168
173
Statement : this . serverless . service . provider . stackPolicy ,
169
174
} ) ;
@@ -185,8 +190,38 @@ module.exports = {
185
190
186
191
} ,
187
192
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
+
188
222
aliasRemoveAliasStack ( currentTemplate , aliasStackTemplates , currentAliasStackTemplate ) {
189
223
224
+
190
225
const stackName = `${ this . _provider . naming . getStackName ( ) } -${ this . _alias } ` ;
191
226
192
227
this . options . verbose && this . _serverless . cli . log ( `Removing CF stack ${ stackName } ` ) ;
@@ -245,9 +280,8 @@ module.exports = {
245
280
return BbPromise . resolve ( [ currentTemplate , aliasStackTemplates , currentAliasStackTemplate ] ) . bind ( this )
246
281
. spread ( this . aliasCreateStackChanges )
247
282
. spread ( this . aliasRemoveAliasStack )
283
+ . spread ( this . uploadCloudFormationTemplate )
248
284
. spread ( this . aliasApplyStackChanges )
249
285
. then ( ( ) => BbPromise . resolve ( ) ) ;
250
-
251
286
}
252
-
253
287
} ;
0 commit comments