2121aws_region = os .getenv ("AWS_REGION" )
2222lambda_arn = os .getenv ("LAMBDA_ARN" )
2323
24+ LAMBDA_EVENT_TYPES = [
25+ "viewer-request" ,
26+ "viewer-response" ,
27+ "origin-request" ,
28+ "origin-response" ,
29+ ]
2430
2531def setup_logging ():
2632 logging .basicConfig (
@@ -102,19 +108,17 @@ def invalidate_cloudfront_cache(cloudfront_client):
102108
103109
104110def update_cloudfront_lambda_association (cloudfront_client ):
105- """Update the CloudFront distribution with the new Lambda function version."""
106111 logger .info (
107112 f"Updating CloudFront distribution: { distribution_id } with Lambda function ARN: { lambda_arn } "
108113 )
109114 try :
110- response = cloudfront_client .get_distribution_config (Id = distribution_id )
111- distribution_config = response ["DistributionConfig" ]
112- etag = response ["ETag" ]
115+ distribution_config , etag = get_distribution_config (cloudfront_client )
113116
114- lambda_associations = distribution_config ["DefaultCacheBehavior" ].get (
117+ default_cache_lambda_associations = distribution_config ["DefaultCacheBehavior" ].get (
115118 "LambdaFunctionAssociations" , {}
116119 )
117- items = lambda_associations .get ("Items" , [])
120+
121+ items = default_cache_lambda_associations .get ("Items" , [])
118122
119123 if not items :
120124 logger .error (
@@ -123,24 +127,18 @@ def update_cloudfront_lambda_association(cloudfront_client):
123127 )
124128 sys .exit (EXIT_LAMBDA_ASSOC_NOT_FOUND )
125129
126- updated = False
127- for item in items :
128- if item ["EventType" ] in [
129- "viewer-request" ,
130- "viewer-response" ,
131- "origin-request" ,
132- "origin-response" ,
133- ]:
134- logger .info (
135- f"Updating LambdaFunctionARN for event type { item ['EventType' ]} "
136- )
137- item ["LambdaFunctionARN" ] = lambda_arn
138- updated = True
130+ updated = update_lambda_associations (items , lambda_arn )
131+
132+ cache_behaviours = distribution_config ["CacheBehaviors" ].get ("Items" , [])
133+
134+ if cache_behaviours and update_cache_behaviours (cache_behaviours , lambda_arn ):
135+ updated = True
139136
140137 if not updated :
141138 logger .warning ("No Lambda associations were updated" )
142139 return False
143140
141+
144142 cloudfront_client .update_distribution (
145143 Id = distribution_id , DistributionConfig = distribution_config , IfMatch = etag
146144 )
@@ -160,6 +158,31 @@ def update_cloudfront_lambda_association(cloudfront_client):
160158 logger .error (f"Error updating distribution: { str (e )} " )
161159 sys .exit (EXIT_DISTRIBUTION_UPDATE_FAILED )
162160
161+ def get_distribution_config (cloudfront_client ):
162+ response = cloudfront_client .get_distribution_config (Id = distribution_id )
163+ return response ["DistributionConfig" ], response ["ETag" ]
164+
165+
166+ def update_lambda_associations (items , new_lambda_arn ):
167+ updated = False
168+ for item in items :
169+ if item .get ("EventType" ) in LAMBDA_EVENT_TYPES :
170+ logger .info (
171+ f"Updating LambdaFunctionARN for event type { item ['EventType' ]} "
172+ )
173+ item ["LambdaFunctionARN" ] = new_lambda_arn
174+ updated = True
175+ return updated
176+
177+ def update_cache_behaviours (cache_behaviours , new_lambda_arn ):
178+ updated = False
179+ for cache_behaviour in cache_behaviours :
180+ lambda_associations = cache_behaviour .get ("LambdaFunctionAssociations" , {})
181+ items = lambda_associations .get ("Items" , [])
182+ if update_lambda_associations (items , new_lambda_arn ):
183+ updated = True
184+ return updated
185+
163186
164187def main ():
165188 try :
0 commit comments