@@ -91,6 +91,12 @@ module.exports = {
9191 }
9292 } ;
9393
94+ // Set SERVERLESS_ALIAS environment variable
95+ _ . forOwn ( stageStack . Resources , resource => {
96+ if ( resource . Type === 'AWS::Lambda::Function' ) {
97+ _ . set ( resource , 'Properties.Environment.Variables.SERVERLESS_ALIAS' , this . _alias ) ;
98+ }
99+ } ) ;
94100
95101 const versions = _ . assign ( { } , _ . pickBy ( stageStack . Resources , [ 'Type' , 'AWS::Lambda::Version' ] ) ) ;
96102 if ( ! _ . isEmpty ( versions ) ) {
@@ -313,6 +319,55 @@ module.exports = {
313319 return BbPromise . resolve ( [ currentTemplate , aliasStackTemplates , currentAliasStackTemplate ] ) ;
314320 } ,
315321
322+ aliasHandleCWEvents ( currentTemplate , aliasStackTemplates , currentAliasStackTemplate ) {
323+
324+ const stageStack = this . _serverless . service . provider . compiledCloudFormationTemplate ;
325+ const aliasStack = this . _serverless . service . provider . compiledCloudFormationAliasTemplate ;
326+
327+ const cwEvents = _ . assign ( { } , _ . pickBy ( _ . get ( stageStack , 'Resources' , { } ) , [ 'Type' , 'AWS::Events::Rule' ] ) ) ;
328+ const cwEventLambdaPermissions =
329+ _ . assign ( { } ,
330+ _ . pickBy ( _ . pickBy ( stageStack . Resources , [ 'Type' , 'AWS::Lambda::Permission' ] ) ,
331+ [ 'Properties.Principal' , 'events.amazonaws.com' ] ) ) ;
332+
333+ _ . forOwn ( cwEvents , ( cwEvent , name ) => {
334+ // Reference alias as FunctionName
335+ const targetRefs = utils . findAllReferences ( _ . get ( cwEvent , 'Properties.Targets' ) ) ;
336+ cwEvent . DependsOn = cwEvent . DependsOn || [ ] ;
337+ _ . forEach ( targetRefs , ref => {
338+ const functionName = _ . replace ( ref . ref , / L a m b d a F u n c t i o n $ / , '' ) ;
339+ _ . set ( cwEvent . Properties . Targets , ref . path , { Ref : `${ functionName } Alias` } ) ;
340+ cwEvent . DependsOn . push ( `${ functionName } Alias` ) ;
341+ } ) ;
342+
343+ // Remove mapping from stage stack
344+ delete stageStack . Resources [ name ] ;
345+ } ) ;
346+
347+ // Move event subscriptions to alias stack
348+ _ . defaults ( aliasStack . Resources , cwEvents ) ;
349+
350+ // Adjust permission to reference the function aliases
351+ _ . forOwn ( cwEventLambdaPermissions , ( permission , name ) => {
352+ const targetFunctionRef = utils . findAllReferences ( _ . get ( permission , 'Properties.FunctionName' ) ) ;
353+ const functionName = _ . replace ( targetFunctionRef [ 0 ] . ref , / L a m b d a F u n c t i o n $ / , '' ) ;
354+
355+ // Adjust references and alias permissions
356+ permission . Properties . FunctionName = { Ref : `${ functionName } Alias` } ;
357+
358+ // Add dependency on function alias
359+ permission . DependsOn = [ `${ functionName } Alias` ] ;
360+
361+ delete stageStack . Resources [ name ] ;
362+ } ) ;
363+
364+ // Add all alias stack owned resources
365+ _ . defaults ( aliasStack . Resources , cwEventLambdaPermissions ) ;
366+
367+ // Forward inputs to the promise chain
368+ return BbPromise . resolve ( [ currentTemplate , aliasStackTemplates , currentAliasStackTemplate ] ) ;
369+ } ,
370+
316371 aliasFinalize ( currentTemplate , aliasStackTemplates , currentAliasStackTemplate ) {
317372 const aliasStack = this . _serverless . service . provider . compiledCloudFormationAliasTemplate ;
318373
@@ -336,6 +391,7 @@ module.exports = {
336391 . spread ( this . aliasHandleFunctions )
337392 . spread ( this . aliasHandleApiGateway )
338393 . spread ( this . aliasHandleEvents )
394+ . spread ( this . aliasHandleCWEvents )
339395 . spread ( this . aliasFinalize )
340396 . then ( ( ) => BbPromise . resolve ( ) ) ;
341397 }
0 commit comments