@@ -31,16 +31,19 @@ class ServerlessLambdaEdgePreExistingCloudFront {
31
31
event . preExistingCloudFront . stage != `${ serverless . service . provider . stage } ` ) { continue }
32
32
33
33
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
+ )
35
38
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.`
37
40
)
38
41
39
42
let retryCount = 5
40
43
41
44
const updateDistribution = async ( ) => {
42
45
const config = await this . provider . request ( 'CloudFront' , 'getDistribution' , {
43
- Id : event . preExistingCloudFront . distributionId
46
+ Id : resolvedDistributionId
44
47
} )
45
48
46
49
if ( event . preExistingCloudFront . pathPattern === '*' ) {
@@ -61,7 +64,7 @@ class ServerlessLambdaEdgePreExistingCloudFront {
61
64
62
65
await this . provider
63
66
. request ( 'CloudFront' , 'updateDistribution' , {
64
- Id : event . preExistingCloudFront . distributionId ,
67
+ Id : resolvedDistributionId ,
65
68
IfMatch : config . ETag ,
66
69
DistributionConfig : config . DistributionConfig
67
70
} )
@@ -80,6 +83,7 @@ class ServerlessLambdaEdgePreExistingCloudFront {
80
83
}
81
84
82
85
await updateDistribution ( )
86
+ this . serverless . cli . consoleLog ( `${ functionArn } has been successfully associated to ${ resolvedDistributionId } CloudFront Distribution.` )
83
87
}
84
88
} )
85
89
} , Promise . resolve ( ) )
@@ -106,7 +110,9 @@ class ServerlessLambdaEdgePreExistingCloudFront {
106
110
this . serverless . configSchemaHandler . defineFunctionEvent ( 'aws' , 'preExistingCloudFront' , {
107
111
type : 'object' ,
108
112
properties : {
109
- distributionId : { type : 'string' } ,
113
+ distributionId : {
114
+ anyOf : [ { type : 'string' } , { type : 'object' } ] ,
115
+ } ,
110
116
eventType : { type : 'string' } ,
111
117
pathPattern : { type : 'string' } ,
112
118
includeBody : { type : 'boolean' } ,
@@ -183,5 +189,19 @@ class ServerlessLambdaEdgePreExistingCloudFront {
183
189
} )
184
190
return arn
185
191
}
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
+ }
186
206
}
187
207
module . exports = ServerlessLambdaEdgePreExistingCloudFront
0 commit comments