@@ -152,147 +152,7 @@ module.exports = {
152
152
return BbPromise . resolve ( [ currentTemplate , aliasStackTemplates , currentAliasStackTemplate ] ) ;
153
153
} ,
154
154
155
- aliasHandleApiGateway ( currentTemplate , aliasStackTemplates , currentAliasStackTemplate ) {
156
-
157
- const stackName = this . _provider . naming . getStackName ( ) ;
158
- const stageStack = this . _serverless . service . provider . compiledCloudFormationTemplate ;
159
- const aliasStack = this . _serverless . service . provider . compiledCloudFormationAliasTemplate ;
160
- const userResources = _ . get ( this . _serverless . service , 'resources' , { Resources : { } , Outputs : { } } ) ;
161
-
162
- // Check if our current deployment includes an API deployment
163
- let exposeApi = _ . includes ( _ . keys ( stageStack . Resources ) , 'ApiGatewayRestApi' ) ;
164
- const aliasResources = [ ] ;
165
-
166
- if ( ! exposeApi ) {
167
- // Check if we have any aliases deployed that reference the API.
168
- if ( _ . some ( aliasStackTemplates , template => _ . find ( template . Resources , [ 'Type' , 'AWS::ApiGateway::Deployment' ] ) ) ) {
169
- // Fetch the Api resource from the current stack
170
- stageStack . Resources . ApiGatewayRestApi = currentTemplate . Resources . ApiGatewayRestApi ;
171
- exposeApi = true ;
172
- }
173
- }
174
-
175
- if ( exposeApi ) {
176
-
177
- this . options . verbose && this . _serverless . cli . log ( 'Processing API' ) ;
178
-
179
- // Export the API for the alias stacks
180
- stageStack . Outputs . ApiGatewayRestApi = {
181
- Description : 'API Gateway API' ,
182
- Value : { Ref : 'ApiGatewayRestApi' } ,
183
- Export : {
184
- Name : `${ stackName } -ApiGatewayRestApi`
185
- }
186
- } ;
187
-
188
- // Export the root resource for the API
189
- stageStack . Outputs . ApiGatewayRestApiRootResource = {
190
- Description : 'API Gateway API root resource' ,
191
- Value : { 'Fn::GetAtt' : [ 'ApiGatewayRestApi' , 'RootResourceId' ] } ,
192
- Export : {
193
- Name : `${ stackName } -ApiGatewayRestApiRootResource`
194
- }
195
- } ;
196
-
197
- // Move the API deployment into the alias stack. The alias is the owner of the APIG stage.
198
- const deployment = _ . assign ( { } , _ . pickBy ( stageStack . Resources , [ 'Type' , 'AWS::ApiGateway::Deployment' ] ) ) ;
199
- if ( ! _ . isEmpty ( deployment ) ) {
200
- const deploymentName = _ . keys ( deployment ) [ 0 ] ;
201
- const obj = deployment [ deploymentName ] ;
202
- obj . Properties . StageName = this . _alias ;
203
- obj . Properties . RestApiId = { 'Fn::ImportValue' : `${ stackName } -ApiGatewayRestApi` } ;
204
- aliasResources . push ( deployment ) ;
205
- delete stageStack . Resources [ deploymentName ] ;
206
- }
207
-
208
- // Fetch lambda permissions, methods and resources. These have to be updated later to allow the aliased functions.
209
- const apiLambdaPermissions = _ . assign ( { } , _ . pickBy ( stageStack . Resources , [ 'Type' , 'AWS::Lambda::Permission' ] ) ) ;
210
- const apiMethods = _ . assign ( { } , _ . pickBy ( stageStack . Resources , [ 'Type' , 'AWS::ApiGateway::Method' ] ) ) ;
211
- const apiResources = _ . assign ( { } , _ . pickBy ( stageStack . Resources , [ 'Type' , 'AWS::ApiGateway::Resource' ] ) ) ;
212
- const aliases = _ . assign ( { } , _ . pickBy ( aliasStack . Resources , [ 'Type' , 'AWS::Lambda::Alias' ] ) ) ;
213
- const versions = _ . assign ( { } , _ . pickBy ( aliasStack . Resources , [ 'Type' , 'AWS::Lambda::Version' ] ) ) ;
214
-
215
- // Adjust resources
216
- _ . forOwn ( apiResources , ( resource , name ) => {
217
- resource . Properties . RestApiId = { 'Fn::ImportValue' : `${ stackName } -ApiGatewayRestApi` } ;
218
- // Check parent id. If it references the API root, use the imported api root resource id.
219
- if ( _ . has ( resource , 'Properties.ParentId.Fn::GetAtt' ) && resource . Properties . ParentId [ 'Fn::GetAtt' ] [ 0 ] === 'ApiGatewayRestApi' ) {
220
- resource . Properties . ParentId = { 'Fn::ImportValue' : `${ stackName } -ApiGatewayRestApiRootResource` } ;
221
- }
222
-
223
- delete stageStack . Resources [ name ] ;
224
- } ) ;
225
-
226
- // Adjust method API and target function
227
- _ . forOwn ( apiMethods , ( method , name ) => {
228
-
229
- // Relink to function alias in case we have a lambda endpoint
230
- if ( _ . includes ( [ 'AWS' , 'AWS_PROXY' ] , _ . get ( method , 'Properties.Integration.Type' ) ) ) {
231
- // For methods it is a bit tricky to find the related function name. There is no direct link.
232
- const uriParts = method . Properties . Integration . Uri [ 'Fn::Join' ] [ 1 ] ;
233
- const funcIndex = _ . findIndex ( uriParts , part => _ . has ( part , 'Fn::GetAtt' ) ) ;
234
- const functionName = uriParts [ funcIndex ] [ 'Fn::GetAtt' ] [ 0 ] . replace ( / L a m b d a F u n c t i o n $ / , '' ) ;
235
- const aliasName = _ . find ( _ . keys ( aliases ) , alias => _ . startsWith ( alias , functionName ) ) ;
236
-
237
- uriParts [ funcIndex ] = { Ref : aliasName } ;
238
- }
239
-
240
- // If the method is located on the root resource, set the parent correctly
241
- if ( _ . has ( method , 'Properties.ResourceId.Fn::GetAtt' ) && method . Properties . ResourceId [ 'Fn::GetAtt' ] [ 0 ] === 'ApiGatewayRestApi' ) {
242
- method . Properties . ResourceId = { 'Fn::ImportValue' : `${ stackName } -ApiGatewayRestApiRootResource` } ;
243
- }
244
-
245
- method . Properties . RestApiId = { 'Fn::ImportValue' : `${ stackName } -ApiGatewayRestApi` } ;
246
-
247
- // Check for user resource overrides
248
- if ( _ . has ( userResources . Resources , name ) ) {
249
- _ . merge ( method , userResources . Resources [ name ] ) ;
250
- delete userResources . Resources [ name ] ;
251
- }
252
-
253
- delete stageStack . Resources [ name ] ;
254
- } ) ;
255
-
256
- // Adjust permission to reference the function aliases
257
- _ . forOwn ( apiLambdaPermissions , ( permission , name ) => {
258
- const functionName = name . replace ( / L a m b d a P e r m i s s i o n A p i G a t e w a y $ / , '' ) ;
259
- const versionName = _ . find ( _ . keys ( versions ) , version => _ . startsWith ( version , functionName ) ) ;
260
- const aliasName = _ . find ( _ . keys ( aliases ) , alias => _ . startsWith ( alias , functionName ) ) ;
261
-
262
- // Adjust references and alias permissions
263
- permission . Properties . FunctionName = { Ref : aliasName } ;
264
- permission . Properties . SourceArn = {
265
- 'Fn::Join' : [
266
- '' ,
267
- [
268
- 'arn:aws:execute-api:' ,
269
- { Ref : 'AWS::Region' } ,
270
- ':' ,
271
- { Ref : 'AWS::AccountId' } ,
272
- ':' ,
273
- { 'Fn::ImportValue' : `${ stackName } -ApiGatewayRestApi` } ,
274
- '/*/*'
275
- ]
276
- ]
277
- } ;
278
-
279
- // Add dependency on function version
280
- permission . DependsOn = [ versionName , aliasName ] ;
281
-
282
- delete stageStack . Resources [ name ] ;
283
- } ) ;
284
-
285
- // Add all alias stack owned resources
286
- aliasResources . push ( apiResources ) ;
287
- aliasResources . push ( apiMethods ) ;
288
- aliasResources . push ( apiLambdaPermissions ) ;
289
-
290
- }
291
-
292
- _ . forEach ( aliasResources , resource => _ . assign ( aliasStack . Resources , resource ) ) ;
293
-
294
- return BbPromise . resolve ( [ currentTemplate , aliasStackTemplates , currentAliasStackTemplate ] ) ;
295
- } ,
155
+ aliasHandleApiGateway : require ( './stackops/apiGateway' ) ,
296
156
297
157
aliasHandleUserResources : require ( './stackops/userResources' ) ,
298
158
0 commit comments