Skip to content

Commit

Permalink
Merge pull request #1824 from youcandanch/fix-0.48-issue
Browse files Browse the repository at this point in the history
Fix an issue with ALB aliases and already-deployed functions.
  • Loading branch information
jneves authored Mar 19, 2019
2 parents c4c173d + 8a9253a commit 84e8047
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
9 changes: 9 additions & 0 deletions tests/placebo/TestZappa.test_cli_aws/lambda.GetAlias_1.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
3 changes: 2 additions & 1 deletion zappa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,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)
Expand Down
52 changes: 35 additions & 17 deletions zappa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 84e8047

Please sign in to comment.