Skip to content

Commit

Permalink
Fix: Race between layer and Lambda update (#5927)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsotirho-ucsc committed May 17, 2024
1 parent ab66224 commit d126563
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ $(1)terraform: lambdas

.PHONY: $(1)deploy
$(1)deploy: check_python $(1)terraform
python $(project_root)/scripts/delete_lambda_versions.py
python $(project_root)/scripts/post_deploy_tdr.py
endef

Expand Down
40 changes: 40 additions & 0 deletions scripts/delete_lambda_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Delete all published AWS Lambda versions except for "$LATEST" in the current
deployment.
"""

import logging

from azul.deployment import (
aws,
)
from azul.lambdas import (
Lambdas,
)
from azul.logging import (
configure_script_logging,
)

log = logging.getLogger(__name__)


def main():
lambdas = Lambdas().list_lambdas(deployment_only=True)
for lambda_ in lambdas:
log.info('Fetching the published versions of %s', lambda_.name)
response = aws.lambda_.list_versions_by_function(FunctionName=lambda_.name)
for version in response['Versions']:
version = version['Version']
if version == '$LATEST':
pass
else:
log.info('Deleting published version %s of %s', version, lambda_.name)
aws.lambda_.delete_function(
FunctionName=lambda_.name,
Qualifier=version
)


if __name__ == '__main__':
configure_script_logging(log)
main()
4 changes: 4 additions & 0 deletions src/azul/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,10 @@ def tf_config(self, app_name):
for resource in resources['aws_lambda_function'].values():
assert 'layers' not in resource
resource['layers'] = ['${aws_lambda_layer_version.dependencies.arn}']
# Publishing the Lambda function as a new version prevents possible
# race conditions when there's a cyclic dependency between an update
# to the function's configuration and an update to its code
resource['publish'] = True
env = config.es_endpoint_env(
es_endpoint=(
aws.es_endpoint
Expand Down

0 comments on commit d126563

Please sign in to comment.