@@ -23,24 +23,26 @@ class ServerlessLambdaEdgePreExistingCloudFront {
23
23
const events = functionObj . events . filter (
24
24
( event ) => event . preExistingCloudFront && this . checkAllowedDeployStage ( )
25
25
)
26
-
27
26
for ( let idx = 0 ; idx < events . length ; idx += 1 ) {
28
27
const event = events [ idx ]
29
28
30
29
if ( event . preExistingCloudFront . stage !== undefined &&
31
30
event . preExistingCloudFront . stage != `${ serverless . service . provider . stage } ` ) { continue }
32
31
33
32
const functionArn = await this . getlatestVersionLambdaArn ( functionObj . name )
34
-
33
+ const resolvedDistributionId = await ( event . preExistingCloudFront . distributionId [ 'Fn::ImportValue' ]
34
+ ? this . resolveCfImportValue ( this . provider , event . preExistingCloudFront . distributionId [ 'Fn::ImportValue' ] )
35
+ : event . preExistingCloudFront . distributionId
36
+ )
35
37
this . serverless . cli . consoleLog (
36
- `${ functionArn } is associating to ${ event . preExistingCloudFront . distributionId } CloudFront Distribution. waiting for deployed status.`
38
+ `${ functionArn } (Event: ${ event . preExistingCloudFront . eventType } , pathPattern: ${ event . preExistingCloudFront . pathPattern } ) is associating to ${ resolvedDistributionId } CloudFront Distribution. waiting for deployed status.`
37
39
)
38
40
39
41
let retryCount = 5
40
42
41
43
const updateDistribution = async ( ) => {
42
44
const config = await this . provider . request ( 'CloudFront' , 'getDistribution' , {
43
- Id : event . preExistingCloudFront . distributionId
45
+ Id : resolvedDistributionId
44
46
} )
45
47
46
48
if ( event . preExistingCloudFront . pathPattern === '*' ) {
@@ -61,7 +63,7 @@ class ServerlessLambdaEdgePreExistingCloudFront {
61
63
62
64
await this . provider
63
65
. request ( 'CloudFront' , 'updateDistribution' , {
64
- Id : event . preExistingCloudFront . distributionId ,
66
+ Id : resolvedDistributionId ,
65
67
IfMatch : config . ETag ,
66
68
DistributionConfig : config . DistributionConfig
67
69
} )
@@ -106,7 +108,9 @@ class ServerlessLambdaEdgePreExistingCloudFront {
106
108
this . serverless . configSchemaHandler . defineFunctionEvent ( 'aws' , 'preExistingCloudFront' , {
107
109
type : 'object' ,
108
110
properties : {
109
- distributionId : { type : 'string' } ,
111
+ distributionId : {
112
+ anyOf : [ { type : 'string' } , { type : 'object' } ] ,
113
+ } ,
110
114
eventType : { type : 'string' } ,
111
115
pathPattern : { type : 'string' } ,
112
116
includeBody : { type : 'boolean' } ,
@@ -183,5 +187,19 @@ class ServerlessLambdaEdgePreExistingCloudFront {
183
187
} )
184
188
return arn
185
189
}
190
+
191
+ resolveCfImportValue ( provider , name , sdkParams = { } ) {
192
+ return provider . request ( 'CloudFormation' , 'listExports' , sdkParams ) . then ( result => {
193
+ const targetExportMeta = result . Exports . find ( exportMeta => exportMeta . Name === name ) ;
194
+ if ( targetExportMeta ) return targetExportMeta . Value ;
195
+ if ( result . NextToken ) {
196
+ return this . resolveCfImportValue ( provider , name , { NextToken : result . NextToken } ) ;
197
+ }
198
+
199
+ throw new Error (
200
+ `Could not resolve Fn::ImportValue with name ${ name } . Are you sure this value is exported ?`
201
+ ) ;
202
+ } ) ;
203
+ }
186
204
}
187
205
module . exports = ServerlessLambdaEdgePreExistingCloudFront
0 commit comments