Skip to content

Commit 6275fec

Browse files
committed
Fix: Race between layer and Lambda update (#5927)
1 parent b048c55 commit 6275fec

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ $(1)terraform: lambdas
9999

100100
.PHONY: $(1)deploy
101101
$(1)deploy: check_python $(1)terraform
102+
python $(project_root)/scripts/delete_lambda_versions.py
102103
python $(project_root)/scripts/post_deploy_tdr.py
103104
endef
104105

scripts/delete_lambda_versions.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Delete all published AWS Lambda versions except for "$LATEST" in the current
3+
deployment.
4+
"""
5+
6+
import logging
7+
8+
from azul.deployment import (
9+
aws,
10+
)
11+
from azul.lambdas import (
12+
Lambdas,
13+
)
14+
from azul.logging import (
15+
configure_script_logging,
16+
)
17+
18+
log = logging.getLogger(__name__)
19+
20+
21+
def main():
22+
lambdas = Lambdas().list_lambdas()
23+
for lambda_ in lambdas:
24+
response = aws.lambda_.list_versions_by_function(
25+
FunctionName=lambda_.name
26+
)
27+
for version in response['Versions']:
28+
if version['Version'] == '$LATEST':
29+
pass
30+
else:
31+
version_number = version['Version']
32+
log.info('Deleting published version %s of %s', version_number, lambda_.name)
33+
aws.lambda_.delete_function(
34+
FunctionName=lambda_.name,
35+
Qualifier=version_number
36+
)
37+
38+
39+
if __name__ == '__main__':
40+
configure_script_logging(log)
41+
main()

src/azul/terraform.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,10 @@ def tf_config(self, app_name):
708708
for resource in resources['aws_lambda_function'].values():
709709
assert 'layers' not in resource
710710
resource['layers'] = ['${aws_lambda_layer_version.dependencies.arn}']
711+
# Publishing the Lambda function as a new version prevents possible
712+
# race conditions when an update to the function's configuration and
713+
# code rely on the update of each other in order to work correctly.
714+
resource['publish'] = True
711715
env = config.es_endpoint_env(
712716
es_endpoint=(
713717
aws.es_endpoint

0 commit comments

Comments
 (0)