File tree Expand file tree Collapse file tree 4 files changed +56
-2
lines changed Expand file tree Collapse file tree 4 files changed +56
-2
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Delete all but the latest published version of every AWS Lambda function in the
3
+ current deployment.
4
+ """
5
+
6
+ import logging
7
+
8
+ from azul import (
9
+ config ,
10
+ require ,
11
+ )
12
+ from azul .lambdas import (
13
+ Lambdas ,
14
+ )
15
+ from azul .logging import (
16
+ configure_script_logging ,
17
+ )
18
+
19
+ log = logging .getLogger (__name__ )
20
+
21
+
22
+ def main ():
23
+ require (config .terraform_component == '' ,
24
+ 'This script cannot be run with a Terraform component selected' ,
25
+ config .terraform_component )
26
+ Lambdas ().delete_stale_function_versions ()
27
+
28
+
29
+ if __name__ == '__main__' :
30
+ configure_script_logging (log )
31
+ main ()
Original file line number Diff line number Diff line change @@ -202,3 +202,18 @@ def reset_lambda_roles(self):
202
202
time .sleep (1 )
203
203
else :
204
204
break
205
+
206
+ def delete_stale_function_versions (self ):
207
+ """
208
+ Delete all but the latest published version of every AWS Lambda function
209
+ in the current deployment.
210
+ """
211
+ log .info ('Deleting stale versions of AWS Lambda functions' )
212
+ for function in self .list_lambdas (deployment = config .deployment_stage ,
213
+ all_versions = True ):
214
+ if function .version == '$LATEST' :
215
+ log .info ('Skipping latest version %r' , function .name )
216
+ else :
217
+ log .info ('Deleting version %r of %r' , function .version , function .name )
218
+ self ._lambda .delete_function (FunctionName = function .name ,
219
+ Qualifier = function .version )
Original file line number Diff line number Diff line change @@ -708,6 +708,10 @@ def tf_config(self, app_name):
708
708
for resource in resources ['aws_lambda_function' ].values ():
709
709
assert 'layers' not in resource
710
710
resource ['layers' ] = ['${aws_lambda_layer_version.dependencies.arn}' ]
711
+ # Publishing the Lambda function as a new version prevents a race
712
+ # condition when there's a dependency between updates to the
713
+ # function's configuration and its code.
714
+ resource ['publish' ] = True
711
715
env = config .es_endpoint_env (
712
716
es_endpoint = (
713
717
aws .es_endpoint
Original file line number Diff line number Diff line change @@ -43,8 +43,12 @@ import_resources: rename_resources
43
43
plan : import_resources
44
44
terraform plan
45
45
46
+ .PHONY : delete_stale_function_versions
47
+ delete_stale_function_versions : import_resources check_python
48
+ python $(project_root ) /scripts/delete_stale_function_versions.py
49
+
46
50
.PHONY : apply
47
- apply : import_resources
51
+ apply : delete_stale_function_versions
48
52
ifeq ($(AZUL_PRIVATE_API ) ,1)
49
53
# For private API we need the VPC endpoints to be created first so that the
50
54
# aws_lb_target_group_attachment can iterate over the network_interface_ids.
53
57
terraform apply
54
58
55
59
.PHONY : auto_apply
56
- auto_apply : import_resources
60
+ auto_apply : delete_stale_function_versions
57
61
ifeq ($(AZUL_PRIVATE_API ) ,1)
58
62
# See `apply` above
59
63
terraform apply -auto-approve -target aws_vpc_endpoint.indexer -target aws_vpc_endpoint.service
You can’t perform that action at this time.
0 commit comments