From 8a9253a09979ae1bca168070534ace48acd4d400 Mon Sep 17 00:00:00 2001 From: Alex Dancho Date: Tue, 19 Mar 2019 12:44:08 -0400 Subject: [PATCH] Fix an issue with ALB aliases and already-deployed functions. --- .../lambda.GetAlias_1.json | 9 ++++ .../lambda.GetAlias_1.json | 9 ++++ .../lambda.GetAlias_1.json | 9 ++++ zappa/cli.py | 3 +- zappa/core.py | 52 +++++++++++++------ 5 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 tests/placebo/TestZappa.test_cli_aws/lambda.GetAlias_1.json create mode 100644 tests/placebo/TestZappa.test_create_lambda_function_local/lambda.GetAlias_1.json create mode 100644 tests/placebo/TestZappa.test_create_lambda_function_s3/lambda.GetAlias_1.json diff --git a/tests/placebo/TestZappa.test_cli_aws/lambda.GetAlias_1.json b/tests/placebo/TestZappa.test_cli_aws/lambda.GetAlias_1.json new file mode 100644 index 000000000..cf839c230 --- /dev/null +++ b/tests/placebo/TestZappa.test_cli_aws/lambda.GetAlias_1.json @@ -0,0 +1,9 @@ +{ + "status_code": 200, + "data": { + "AliasArn": "arn:aws:lambda:us-east-1:12345:function:test_lmbda_function55:current-alb-version", + "Description": "Zappa Deployment", + "FunctionVersion": "1", + "Name": "current-alb-version" + } +} diff --git a/tests/placebo/TestZappa.test_create_lambda_function_local/lambda.GetAlias_1.json b/tests/placebo/TestZappa.test_create_lambda_function_local/lambda.GetAlias_1.json new file mode 100644 index 000000000..cf839c230 --- /dev/null +++ b/tests/placebo/TestZappa.test_create_lambda_function_local/lambda.GetAlias_1.json @@ -0,0 +1,9 @@ +{ + "status_code": 200, + "data": { + "AliasArn": "arn:aws:lambda:us-east-1:12345:function:test_lmbda_function55:current-alb-version", + "Description": "Zappa Deployment", + "FunctionVersion": "1", + "Name": "current-alb-version" + } +} diff --git a/tests/placebo/TestZappa.test_create_lambda_function_s3/lambda.GetAlias_1.json b/tests/placebo/TestZappa.test_create_lambda_function_s3/lambda.GetAlias_1.json new file mode 100644 index 000000000..cf839c230 --- /dev/null +++ b/tests/placebo/TestZappa.test_create_lambda_function_s3/lambda.GetAlias_1.json @@ -0,0 +1,9 @@ +{ + "status_code": 200, + "data": { + "AliasArn": "arn:aws:lambda:us-east-1:12345:function:test_lmbda_function55:current-alb-version", + "Description": "Zappa Deployment", + "FunctionVersion": "1", + "Name": "current-alb-version" + } +} diff --git a/zappa/cli.py b/zappa/cli.py index 24a680330..3a173d5aa 100755 --- a/zappa/cli.py +++ b/zappa/cli.py @@ -757,7 +757,8 @@ def deploy(self, source_zip=None): memory_size=self.memory_size, runtime=self.runtime, aws_environment_variables=self.aws_environment_variables, - aws_kms_key_arn=self.aws_kms_key_arn + aws_kms_key_arn=self.aws_kms_key_arn, + use_alb=self.use_alb ) if source_zip and source_zip.startswith('s3://'): bucket, key_name = parse_s3_url(source_zip) diff --git a/zappa/core.py b/zappa/core.py index dae3e596f..c8a54612a 100644 --- a/zappa/core.py +++ b/zappa/core.py @@ -280,7 +280,7 @@ def __init__(self, self.manylinux_wheel_file_suffix = 'cp36m-manylinux1_x86_64.whl' else: self.manylinux_wheel_file_suffix = 'cp37m-manylinux1_x86_64.whl' - + self.endpoint_urls = endpoint_urls self.xray_tracing = xray_tracing @@ -1042,7 +1042,8 @@ def create_lambda_function( self, aws_environment_variables=None, aws_kms_key_arn=None, xray_tracing=False, - local_zip=None + local_zip=None, + use_alb=False, ): """ Given a bucket and key (or a local path) of a valid Lambda-zip, a function name and a handler, register that Lambda function. @@ -1090,15 +1091,17 @@ def create_lambda_function( self, resource_arn = response['FunctionArn'] version = response['Version'] - # Let's create an alias mapped to the newly created function. - # This allows clean, no downtime association when using application - # load balancers as an event source. + # If we're using an ALB, let's create an alias mapped to the newly + # created function. This allows clean, no downtime association when + # using application load balancers as an event source. # See: https://github.com/Miserlou/Zappa/pull/1730 - self.lambda_client.create_alias( - FunctionName=resource_arn, - FunctionVersion=version, - Name=ALB_LAMBDA_ALIAS, - ) + # https://github.com/Miserlou/Zappa/issues/1823 + if use_alb: + self.lambda_client.create_alias( + FunctionName=resource_arn, + FunctionVersion=version, + Name=ALB_LAMBDA_ALIAS, + ) if self.tags: self.lambda_client.tag_resource(Resource=resource_arn, Tags=self.tags) @@ -1126,14 +1129,29 @@ def update_lambda_function(self, bucket, function_name, s3_key=None, publish=Tru resource_arn = response['FunctionArn'] version = response['Version'] - # Given an alias name, let's update that alias to map to this newly - # updated version. + # If the lambda has an ALB alias, let's update the alias + # to point to the newest version of the function. We have to use a GET + # here, as there's no HEAD-esque call to retrieve metadata about a + # function alias. # Related: https://github.com/Miserlou/Zappa/pull/1730 - self.lambda_client.update_alias( - FunctionName=function_name, - FunctionVersion=version, - Name=ALB_LAMBDA_ALIAS, - ) + # https://github.com/Miserlou/Zappa/issues/1823 + try: + response = self.lambda_client.get_alias( + FunctionName=function_name, + Name=ALB_LAMBDA_ALIAS, + ) + alias_exists = True + except botocore.exceptions.ClientError as e: # pragma: no cover + if "ResourceNotFoundException" not in e.response["Error"]["Code"]: + raise e + alias_exists = False + + if alias_exists: + self.lambda_client.update_alias( + FunctionName=function_name, + FunctionVersion=version, + Name=ALB_LAMBDA_ALIAS, + ) if num_revisions: # Find the existing revision IDs for the given function