@@ -31,35 +31,55 @@ class ServerlessLambdaEdgePreExistingCloudFront {
31
31
event . preExistingCloudFront . stage != `${ serverless . service . provider . stage } ` ) { continue }
32
32
33
33
const functionArn = await this . getlatestVersionLambdaArn ( functionObj . name )
34
- const config = await this . provider . request ( 'CloudFront' , 'getDistribution' , {
35
- Id : event . preExistingCloudFront . distributionId
36
- } )
37
-
38
- if ( event . preExistingCloudFront . pathPattern === '*' ) {
39
- config . DistributionConfig . DefaultCacheBehavior . LambdaFunctionAssociations = await this . associateFunction (
40
- config . DistributionConfig . DefaultCacheBehavior . LambdaFunctionAssociations ,
41
- event ,
42
- functionObj . name ,
43
- functionArn
44
- )
45
- } else {
46
- config . DistributionConfig . CacheBehaviors = await this . associateNonDefaultCacheBehaviors (
47
- config . DistributionConfig . CacheBehaviors ,
48
- event ,
49
- functionObj . name ,
50
- functionArn
51
- )
52
- }
53
34
54
35
this . serverless . cli . consoleLog (
55
36
`${ functionArn } is associating to ${ event . preExistingCloudFront . distributionId } CloudFront Distribution. waiting for deployed status.`
56
37
)
57
38
58
- await this . provider . request ( 'CloudFront' , 'updateDistribution' , {
59
- Id : event . preExistingCloudFront . distributionId ,
60
- IfMatch : config . ETag ,
61
- DistributionConfig : config . DistributionConfig
62
- } )
39
+ let retryCount = 5
40
+
41
+ const updateDistribution = async ( ) => {
42
+ const config = await this . provider . request ( 'CloudFront' , 'getDistribution' , {
43
+ Id : event . preExistingCloudFront . distributionId
44
+ } )
45
+
46
+ if ( event . preExistingCloudFront . pathPattern === '*' ) {
47
+ config . DistributionConfig . DefaultCacheBehavior . LambdaFunctionAssociations = await this . associateFunction (
48
+ config . DistributionConfig . DefaultCacheBehavior . LambdaFunctionAssociations ,
49
+ event ,
50
+ functionObj . name ,
51
+ functionArn
52
+ )
53
+ } else {
54
+ config . DistributionConfig . CacheBehaviors = await this . associateNonDefaultCacheBehaviors (
55
+ config . DistributionConfig . CacheBehaviors ,
56
+ event ,
57
+ functionObj . name ,
58
+ functionArn
59
+ )
60
+ }
61
+
62
+ await this . provider
63
+ . request ( 'CloudFront' , 'updateDistribution' , {
64
+ Id : event . preExistingCloudFront . distributionId ,
65
+ IfMatch : config . ETag ,
66
+ DistributionConfig : config . DistributionConfig
67
+ } )
68
+ . catch ( async ( error ) => {
69
+ if ( error . providerError . code === 'PreconditionFailed' && retryCount > 0 ) {
70
+ this . serverless . cli . consoleLog (
71
+ `received precondition failed error, retrying... (${ retryCount } /5)`
72
+ )
73
+ retryCount -= 1
74
+ await new Promise ( ( res ) => setTimeout ( res , 5000 ) )
75
+ return updateDistribution ( )
76
+ }
77
+ this . serverless . cli . consoleLog ( error )
78
+ throw error
79
+ } )
80
+ }
81
+
82
+ await updateDistribution ( )
63
83
}
64
84
} )
65
85
} , Promise . resolve ( ) )
0 commit comments