@@ -31,16 +31,19 @@ class ServerlessLambdaEdgePreExistingCloudFront {
3131 event . preExistingCloudFront . stage != `${ serverless . service . provider . stage } ` ) { continue }
3232
3333 const functionArn = await this . getlatestVersionLambdaArn ( functionObj . name )
34-
34+ const resolvedDistributionId = await ( event . preExistingCloudFront . distributionId [ 'Fn::ImportValue' ]
35+ ? this . resolveCfImportValue ( this . provider , event . preExistingCloudFront . distributionId [ 'Fn::ImportValue' ] )
36+ : event . preExistingCloudFront . distributionId
37+ )
3538 this . serverless . cli . consoleLog (
36- `${ functionArn } is associating to ${ event . preExistingCloudFront . distributionId } CloudFront Distribution. waiting for deployed status.`
39+ `${ functionArn } is associating to ${ resolvedDistributionId } CloudFront Distribution. waiting for deployed status.`
3740 )
3841
3942 let retryCount = 5
4043
4144 const updateDistribution = async ( ) => {
4245 const config = await this . provider . request ( 'CloudFront' , 'getDistribution' , {
43- Id : event . preExistingCloudFront . distributionId
46+ Id : resolvedDistributionId
4447 } )
4548
4649 if ( event . preExistingCloudFront . pathPattern === '*' ) {
@@ -61,7 +64,7 @@ class ServerlessLambdaEdgePreExistingCloudFront {
6164
6265 await this . provider
6366 . request ( 'CloudFront' , 'updateDistribution' , {
64- Id : event . preExistingCloudFront . distributionId ,
67+ Id : resolvedDistributionId ,
6568 IfMatch : config . ETag ,
6669 DistributionConfig : config . DistributionConfig
6770 } )
@@ -80,6 +83,7 @@ class ServerlessLambdaEdgePreExistingCloudFront {
8083 }
8184
8285 await updateDistribution ( )
86+ this . serverless . cli . consoleLog ( `${ functionArn } has been successfully associated to ${ resolvedDistributionId } CloudFront Distribution.` )
8387 }
8488 } )
8589 } , Promise . resolve ( ) )
@@ -106,7 +110,9 @@ class ServerlessLambdaEdgePreExistingCloudFront {
106110 this . serverless . configSchemaHandler . defineFunctionEvent ( 'aws' , 'preExistingCloudFront' , {
107111 type : 'object' ,
108112 properties : {
109- distributionId : { type : 'string' } ,
113+ distributionId : {
114+ anyOf : [ { type : 'string' } , { type : 'object' } ] ,
115+ } ,
110116 eventType : { type : 'string' } ,
111117 pathPattern : { type : 'string' } ,
112118 includeBody : { type : 'boolean' } ,
@@ -183,5 +189,19 @@ class ServerlessLambdaEdgePreExistingCloudFront {
183189 } )
184190 return arn
185191 }
192+
193+ resolveCfImportValue ( provider , name , sdkParams = { } ) {
194+ return provider . request ( 'CloudFormation' , 'listExports' , sdkParams ) . then ( result => {
195+ const targetExportMeta = result . Exports . find ( exportMeta => exportMeta . Name === name ) ;
196+ if ( targetExportMeta ) return targetExportMeta . Value ;
197+ if ( result . NextToken ) {
198+ return this . resolveCfImportValue ( provider , name , { NextToken : result . NextToken } ) ;
199+ }
200+
201+ throw new Error (
202+ `Could not resolve Fn::ImportValue with name ${ name } . Are you sure this value is exported ?`
203+ ) ;
204+ } ) ;
205+ }
186206}
187207module . exports = ServerlessLambdaEdgePreExistingCloudFront
0 commit comments