8000 Add CloudFormation Lambda Version Provisioned Concurrency by joe4dev · Pull Request #12594 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

Add CloudFormation Lambda Version Provisioned Concurrency #12594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,65 @@ def create(
response = lambda_client.publish_version(**params)
model["Version"] = response["Version"]
model["Id"] = response["FunctionArn"]
if model.get("ProvisionedConcurrencyConfig"):
lambda_client.put_provisioned_concurrency_config(
FunctionName=model["FunctionName"],
Qualifier=model["Version"],
ProvisionedConcurrentExecutions=model["ProvisionedConcurrencyConfig"][
"ProvisionedConcurrentExecutions"
],
)
ctx[REPEATED_INVOCATION] = True
return ProgressEvent(
status=OperationStatus.IN_PROGRESS,
resource_model=model,
custom_context=request.custom_context,
)

version = lambda_client.get_function(FunctionName=model["Id"])
if version["Configuration"]["State"] == "Pending":
return ProgressEvent(
status=OperationStatus.IN_PROGRESS,
resource_model=model,
custom_context=request.custom_context,
)
elif version["Configuration"]["State"] == "Active":
return ProgressEvent(
status=OperationStatus.SUCCESS,
resource_model=model,
if model.get("ProvisionedConcurrencyConfig"):
# Assumption: Ready provisioned concurrency implies the function version is ready
provisioned_concurrency_config = lambda_client.get_provisioned_concurrency_config(
FunctionName=model["FunctionName"],
Qualifier=model["Version"],
)
if provisioned_concurrency_config["Status&q 8000 uot;] == "IN_PROGRESS":
return ProgressEvent(
status=OperationStatus.IN_PROGRESS,
resource_model=model,
custom_context=request.custom_context,
)
elif provisioned_concurrency_config["Status"] == "READY":
return ProgressEvent(
status=OperationStatus.SUCCESS,
resource_model=model,
)
else:
return ProgressEvent(
status=OperationStatus.FAILED,
resource_model=model,
message="",
error_code="VersionStateFailure", # TODO: not parity tested
)
else:
return ProgressEvent(
status=OperationStatus.FAILED,
resource_model=model,
message="",
error_code="VersionStateFailure", # TODO: not parity tested
)
version = lambda_client.get_function(FunctionName=model["Id"])
if version["Configuration"]["State"] == "Pending":
return ProgressEvent(
status=OperationStatus.IN_PROGRESS,
resource_model=model,
custom_context=request.custom_context,
)
elif version["Configuration"]["State"] == "Active":
return ProgressEvent(
status=OperationStatus.SUCCESS,
resource_model=model,
)
else:
return ProgressEvent(
status=OperationStatus.FAILED,
resource_model=model,
message="",
error_code="VersionStateFailure", # TODO: not parity tested
)

def read(
self,
Expand All @@ -117,6 +150,7 @@ def delete(
lambda_client = request.aws_client_factory.lambda_

# without qualifier entire function is deleted instead of just version
# provisioned concurrency is automatically deleted upon deleting a function or function version
lambda_client.delete_function(FunctionName=model["Id"], Qualifier=model["Version"])

return ProgressEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def create(
if model.get("ProvisionedConcurrencyConfig"):
lambda_.put_provisioned_concurrency_config(
FunctionName=model["FunctionName"],
Qualifier=model["Id"].split(":")[-1],
Qualifier=model["Name"],
ProvisionedConcurrentExecutions=model["ProvisionedConcurrencyConfig"][
"ProvisionedConcurrentExecutions"
],
Expand All @@ -100,13 +100,25 @@ def create(
# get provisioned config status
result = lambda_.get_provisioned_concurrency_config(
FunctionName=model["FunctionName"],
Qualifier=model["Id"].split(":")[-1],
Qualifier=model["Name"],
)
if result["Status"] == "IN_PROGRESS":
return ProgressEvent(
status=OperationStatus.IN_PROGRESS,
resource_model=model,
)
elif result["Status"] == "READY":
return ProgressEvent(
status=OperationStatus.SUCCESS,
resource_model=model,
)
else:
return ProgressEvent(
status=OperationStatus.FAILED,
resource_model=model,
message="",
error_code="VersionStateFailure", # TODO: not parity tested
)

return ProgressEvent(
status=OperationStatus.SUCCESS,
Expand Down Expand Up @@ -137,6 +149,7 @@ def delete(
lambda_ = request.aws_client_factory.lambda_

try:
# provisioned concurrency is automatically deleted upon deleting a function alias
lambda_.delete_alias(
FunctionName=model["FunctionName"],
Name=model["Name"],
Expand Down
73 changes: 70 additions & 3 deletions tests/aws/services/cloudformation/resources/test_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ def test_lambda_alias(deploy_cfn_template, snapshot, aws_client):
parameters={"FunctionName": function_name, "AliasName": alias_name},
)

invoke_result = aws_client.lambda_.invoke(
FunctionName=function_name, Qualifier=alias_name, Payload=b"{}"
)
assert "FunctionError" not in invoke_result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: do we still need this assertion of we are snapshotting the result?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion is not strictly when it comes to AWS<->LS parity. However, we commonly clarify the intention for Lambda invokes (see Lambda test suite) because it's too/so easy to overlook a snapshot update, which contains a failing Lambda response (happened twice here).

snapshot.match("invoke_result", invoke_result)

role_arn = aws_client.lambda_.get_function(FunctionName=function_name)["Configuration"]["Role"]
snapshot.add_transformer(
snapshot.transform.regex(role_arn.partition("role/")[-1], "<role-name>"), priority=-1
Expand All @@ -252,6 +258,12 @@ def test_lambda_alias(deploy_cfn_template, snapshot, aws_client):
alias = aws_client.lambda_.get_alias(FunctionName=function_name, Name=alias_name)
snapshot.match("Alias", alias)

provisioned_concurrency_config = aws_client.lambda_.get_provisioned_concurrency_config(
FunctionName=function_name,
Qualifier=alias_name,
)
snapshot.match("provisioned_concurrency_config", provisioned_concurrency_config)


@markers.aws.validated
def test_lambda_logging_config(deploy_cfn_template, snapshot, aws_client):
Expand Down Expand Up @@ -351,21 +363,70 @@ def test_lambda_version(deploy_cfn_template, snapshot, aws_client):
template_path=os.path.join(
os.path.dirname(__file__), "../../../templates/cfn_lambda_version.yaml"
),
max_wait=240,
max_wait=180,
)
function_name = deployment.outputs["FunctionName"]
function_version = deployment.outputs["FunctionVersion"]

invoke_result = aws_client.lambda_.invoke(
FunctionName=deployment.outputs["FunctionName"], Payload=b"{}"
FunctionName=function_name, Qualifier=function_version, Payload=b"{}"
)
assert 200 <= invoke_result["StatusCode"] < 300
assert "FunctionError" not in invoke_result
snapshot.match("invoke_result", invoke_result)

stack_resources = aws_client.cloudformation.describe_stack_resources(
StackName=deployment.stack_id
)
snapshot.match("stack_resources", stack_resources)

versions_by_fn = aws_client.lambda_.list_versions_by_function(FunctionName=function_name)
get_function_version = aws_client.lambda_.get_function(
FunctionName=function_name, Qualifier=function_version
)

snapshot.match("versions_by_fn", versions_by_fn)
snapshot.match("get_function_version", get_function_version)


@markers.snapshot.skip_snapshot_verify(
paths=[
# Lambda ZIP flaky in CI
"$..CodeSize",
]
)
@markers.aws.validated
def test_lambda_version_provisioned_concurrency(deploy_cfn_template, snapshot, aws_client):
"""Provisioned concurrency slows down the test case considerably (~2min 40s on AWS)
because CloudFormation waits until the provisioned Lambda functions are ready.
"""
snapshot.add_transformer(snapshot.transform.cloudformation_api())
snapshot.add_transformer(snapshot.transform.lambda_api())
snapshot.add_transformer(
SortingTransformer("StackResources", lambda sr: sr["LogicalResourceId"])
)
snapshot.add_transformer(snapshot.transform.key_value("CodeSha256"))

deployment = deploy_cfn_template(
template_path=os.path.join(
os.path.dirname(__file__),
"../../../templates/cfn_lambda_version_provisioned_concurrency.yaml",
),
max_wait=240,
)
function_name = deployment.outputs["FunctionName"]
function_version = deployment.outputs["FunctionVersion"]

invoke_result = aws_client.lambda_.invoke(
FunctionName=function_name, Qualifier=function_version, Payload=b"{}"
)
assert "FunctionError" not in invoke_result
snapshot.match("invoke_result", invoke_result)

stack_resources = aws_client.cloudformation.describe_stack_resources(
StackName=deployment.stack_id
)
snapshot.match("stack_resources", stack_resources)

versions_by_fn = aws_client.lambda_.list_versions_by_function(FunctionName=function_name)
get_function_version = aws_client.lambda_.get_function(
FunctionName=function_name, Qualifier=function_version
Expand All @@ -374,6 +435,12 @@ def test_lambda_version(deploy_cfn_template, snapshot, aws_client):
snapshot.match("versions_by_fn", versions_by_fn)
snapshot.match("get_function_version", get_function_version)

provisioned_concurrency_config = aws_client.lambda_.get_provisioned_concurrency_config(
FunctionName=function_name,
Qualifier=function_version,
)
snapshot.match("provisioned_concurrency_config", provisioned_concurrency_config)


@markers.aws.validated
def test_lambda_cfn_run(deploy_cfn_template, aws_client):
Expand Down
Loading
Loading
0