Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/422-update-exclude-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdaly authored Oct 4, 2021
2 parents d6cf529 + faa7f47 commit 18dd896
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {
"RequestId": "8cd3e440-6d74-11e7-9ebd-693a5cc9b121",
"HTTPStatusCode": 200,
"HTTPHeaders": {
"content-type": "application/json",
"date": "Thu, 20 Jul 2017 17:54:59 GMT",
"x-amzn-requestid": "8cd3e440-6d74-11e7-9ebd-693a5cc9b121",
"content-length": "609",
"connection": "keep-alive"
},
"RetryAttempts": 0
},
"FunctionName": "zappa-ttt888",
"FunctionArn": "arn:aws:lambda:us-east-1:004396165043:function:zappa-ttt888",
"Runtime": "python3.6",
"Role": "arn:aws:iam::004396165043:role/zappa-ttt888-ZappaLambdaExecutionRole",
"Handler": "handler.lambda_handler",
"CodeSize": 16975308,
"Description": "Zappa Deployment",
"Timeout": 30,
"MemorySize": 512,
"LastModified": "2017-07-20T17:54:59.235+0000",
"CodeSha256": "0pOcmP7sDoO6mLbZKmtH5z0XyjT8wuu/1VenHLgu/MU=",
"Version": "$LATEST",
"Environment": {
"Variables": {}
},
"PackageType": "Zip"
"State": "Active",
"LastUpdateStatus": "Successful"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {
"RequestId": "8cd3e440-6d74-11e7-9ebd-693a5cc9b121",
"HTTPStatusCode": 200,
"HTTPHeaders": {
"content-type": "application/json",
"date": "Thu, 20 Jul 2017 17:54:59 GMT",
"x-amzn-requestid": "8cd3e440-6d74-11e7-9ebd-693a5cc9b121",
"content-length": "609",
"connection": "keep-alive"
},
"RetryAttempts": 0
},
"FunctionName": "zappa-ttt888",
"FunctionArn": "arn:aws:lambda:us-east-1:004396165043:function:zappa-ttt888",
"Runtime": "python3.6",
"Role": "arn:aws:iam::004396165043:role/zappa-ttt888-ZappaLambdaExecutionRole",
"Handler": "handler.lambda_handler",
"CodeSize": 16975308,
"Description": "Zappa Deployment",
"Timeout": 30,
"MemorySize": 512,
"LastModified": "2017-07-20T17:54:59.235+0000",
"CodeSha256": "0pOcmP7sDoO6mLbZKmtH5z0XyjT8wuu/1VenHLgu/MU=",
"Version": "$LATEST",
"Environment": {
"Variables": {}
},
"PackageType": "Zip"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"State": "Active",
"LastUpdateStatus": "Successful"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"State": "Active",
"LastUpdateStatus": "Successful"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"State": "Active",
"LastUpdateStatus": "Successful"
}
}
4 changes: 3 additions & 1 deletion tests/test_bad_stage_name_settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"ttt-888": {
"apigateway_enabled": true,
"touch": false,
"s3_bucket": "lmbda",
"app_function": "tests.test_app.hello_world",
Expand Down Expand Up @@ -29,6 +30,7 @@
]
},
"devor": {
"apigateway_enabled": true,
"s3_bucket": "lmbda",
"app_function": "tests.test_app.hello_world",
"callbacks": {
Expand All @@ -45,4 +47,4 @@
"expression": "rate(1 minute)"
}]
}
}
}
8 changes: 3 additions & 5 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,11 +1424,9 @@ def test_bad_json_catch(self):

def test_bad_stage_name_catch(self):
zappa_cli = ZappaCLI()
self.assertRaises(
ValueError,
zappa_cli.load_settings,
"tests/test_bad_stage_name_settings.json",
)
zappa_cli.api_stage = "ttt-888"
zappa_cli.load_settings("tests/test_bad_stage_name_settings.json")
self.assertRaises(ValueError, zappa_cli.dispatch_command, "deploy", "ttt-888")

def test_bad_environment_vars_catch(self):
zappa_cli = ZappaCLI()
Expand Down
7 changes: 0 additions & 7 deletions tests/tests_placebo.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,6 @@ def test_rollback_lambda_function_version_docker(self, session):
with self.assertRaises(NotImplementedError):
z.rollback_lambda_function_version(function_name)

@placebo_session
def test_is_lambda_function_ready(self, session):
z = Zappa(session)
z.credentials_arn = "arn:aws:iam::724336686645:role/ZappaLambdaExecution"
function_name = "django-helloworld-unicode"
z.is_lambda_function_ready(function_name)

@placebo_session
def test_invoke_lambda_function(self, session):
z = Zappa(session)
Expand Down
29 changes: 11 additions & 18 deletions zappa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def dispatch_command(self, command, stage):
Given a command to execute and stage,
execute that command.
"""

self.check_stage_name(stage)
self.api_stage = stage

if command not in ["status", "manage"]:
Expand Down Expand Up @@ -996,7 +996,7 @@ def deploy(self, source_zip=None, docker_image_uri=None):
)

if self.stage_config.get("touch", True):
self.zappa.wait_until_lambda_function_is_ready(
self.zappa.wait_until_lambda_function_is_updated(
function_name=self.lambda_name
)
self.touch_endpoint(endpoint_url)
Expand Down Expand Up @@ -1140,9 +1140,6 @@ def update(self, source_zip=None, no_upload=False, docker_image_uri=None):
if docker_image_uri:
kwargs["docker_image_uri"] = docker_image_uri
self.lambda_arn = self.zappa.update_lambda_function(**kwargs)
self.zappa.wait_until_lambda_function_is_ready(
function_name=self.lambda_name
)
elif source_zip and source_zip.startswith("s3://"):
bucket, key_name = parse_s3_url(source_zip)
kwargs.update(dict(bucket=bucket, s3_key=key_name))
Expand Down Expand Up @@ -1174,6 +1171,7 @@ def update(self, source_zip=None, no_upload=False, docker_image_uri=None):
aws_environment_variables=self.aws_environment_variables,
aws_kms_key_arn=self.aws_kms_key_arn,
layers=self.layers,
wait=False,
)

# Finally, delete the local copy our zip package
Expand Down Expand Up @@ -1263,7 +1261,7 @@ def update(self, source_zip=None, no_upload=False, docker_image_uri=None):
deployed_string = deployed_string + " (" + api_url + ")"

if self.stage_config.get("touch", True):
self.zappa.wait_until_lambda_function_is_ready(
self.zappa.wait_until_lambda_function_is_updated(
function_name=self.lambda_name
)
if api_url:
Expand Down Expand Up @@ -1789,9 +1787,15 @@ def check_stage_name(self, stage_name):
calling the CreateDeployment operation: Stage name only allows
a-zA-Z0-9_" if the pattern does not match)
"""
if not self.use_apigateway:
return True
if self.stage_name_env_pattern.match(stage_name):
return True
raise ValueError("AWS requires stage name to match a-zA-Z0-9_")
raise ValueError(
"API stage names must match a-zA-Z0-9_ ; '{0!s}' does not.".format(
stage_name
)
)

def check_environment(self, environment):
"""
Expand Down Expand Up @@ -2483,17 +2487,6 @@ def load_settings(self, settings_file=None, session=None):
# Load up file
self.load_settings_file(settings_file)

# Make sure that the stages are valid names:
for stage_name in self.zappa_settings.keys():
try:
self.check_stage_name(stage_name)
except ValueError:
raise ValueError(
"API stage names must match a-zA-Z0-9_ ; '{0!s}' does not.".format(
stage_name
)
)

# Make sure that this stage is our settings
if self.api_stage not in self.zappa_settings.keys():
raise ClickException(
Expand Down
45 changes: 22 additions & 23 deletions zappa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,9 @@ def create_lambda_function(
ReservedConcurrentExecutions=concurrency,
)

# Wait for lambda to become active, otherwise many operations will fail
self.wait_until_lambda_function_is_active(function_name)

return resource_arn

def update_lambda_function(
Expand Down Expand Up @@ -1333,6 +1336,8 @@ def update_lambda_function(
FunctionName=function_name, Qualifier=version
)

self.wait_until_lambda_function_is_updated(function_name)

return resource_arn

def update_lambda_configuration(
Expand All @@ -1349,6 +1354,7 @@ def update_lambda_configuration(
aws_environment_variables=None,
aws_kms_key_arn=None,
layers=None,
wait=True,
):
"""
Given an existing function ARN, update the configuration variables.
Expand All @@ -1366,6 +1372,10 @@ def update_lambda_configuration(
if not layers:
layers = []

if wait:
# Wait until function is ready, otherwise expected keys will be missing from 'lambda_aws_config'.
self.wait_until_lambda_function_is_updated(function_name)

# Check if there are any remote aws lambda env vars so they don't get trashed.
# https://github.com/Miserlou/Zappa/issues/987, Related: https://github.com/Miserlou/Zappa/issues/765
lambda_aws_config = self.lambda_client.get_function_configuration(
Expand Down Expand Up @@ -1483,34 +1493,23 @@ def rollback_lambda_function_version(

return response["FunctionArn"]

def is_lambda_function_ready(self, function_name):
def wait_until_lambda_function_is_active(self, function_name):
"""
Checks if a lambda function is active and no updates are in progress.
Wait until lambda State=Active
"""
response = self.lambda_client.get_function(FunctionName=function_name)
return (
response["Configuration"]["State"] == "Active"
and response["Configuration"]["LastUpdateStatus"] != "InProgress"
)
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#waiters
waiter = self.lambda_client.get_waiter("function_active")
print(f"Waiting for lambda function [{function_name}] to become active...")
waiter.wait(FunctionName=function_name)

def wait_until_lambda_function_is_ready(self, function_name):
def wait_until_lambda_function_is_updated(self, function_name):
"""
Continuously check if a lambda function is active.
For functions deployed with a docker image instead of a
ZIP package, the function can take a few seconds longer
to be created or update, so we must wait before running any status
checks against the function.
Wait until lambda LastUpdateStatus=Successful
"""
show_waiting_message = True
while True:
if self.is_lambda_function_ready(function_name):
break

if show_waiting_message:
print("Waiting until lambda function is ready.")
show_waiting_message = False

time.sleep(1)
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#waiters
waiter = self.lambda_client.get_waiter("function_updated")
print(f"Waiting for lambda function [{function_name}] to be updated...")
waiter.wait(FunctionName=function_name)

def get_lambda_function(self, function_name):
"""
Expand Down

0 comments on commit 18dd896

Please sign in to comment.