Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(lambda): enforce 250mb limit (#1419)

Nahuel Nucera7e154da98172
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index 13f52c9c..ca36aa99 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -731,15 +731,20 @@ def _zip_inline(source: str | None, handler: str, runtime: str = "python3.12") -
def _lambda_create(logical_id, props, stack_name):
name = props.get("FunctionName") or _physical_name(stack_name, logical_id, max_len=64)
arn = f"arn:aws:lambda:{get_region()}:{get_account_id()}:function:{name}"
- runtime = props.get("Runtime", "python3.12")
- handler = props.get("Handler", "index.handler")
+ code = props.get("Code", {})
+ image_uri = code.get("ImageUri")
+ # An Image-package function carries no Runtime/Handler on AWS — both are
+ # empty, exactly as the Lambda API's _build_config sets them. Only a Zip
+ # package gets the python3.12/index.handler defaults.
+ is_image = props.get("PackageType") == "Image" or bool(image_uri)
+ runtime = props.get("Runtime", "" if is_image else "python3.12")
+ handler = props.get("Handler", "" if is_image else "index.handler")
role = props.get("Role", f"arn:aws:iam::{get_account_id()}:role/dummy-role")
timeout = int(props.get("Timeout", 3))
memory = int(props.get("MemorySize", 128))
env_vars = props.get("Environment", {}).get("Variables", {})
description = props.get("Description", "")
layers = props.get("Layers", [])
- code = props.get("Code", {})
# Resolve the actual code bytes:
# - inline ZipFile is wrapped to a real zip archive
@@ -779,7 +784,7 @@ def _lambda_create(logical_id, props, stack_name):
"Layers": [{"Arn": l} if isinstance(l, str) else l for l in layers],
"State": "Active",
"LastUpdateStatus": "Successful",
- "PackageType": "Zip",
+ "PackageType": "Image" if is_image else "Zip",
"Architectures": props.get("Architectures", ["x86_64"]),
"EphemeralStorage": {"Size": props.get("EphemeralStorage", {}).get("Size", 512)},
"TracingConfig": props.get("TracingConfig", {"Mode": "PassThrough"}),
@@ -800,6 +805,13 @@ def _lambda_create(logical_id, props, stack_name):
"concurrency": None,
"provisioned_concurrency": {},
}
+ if is_image:
+ # Mirror the Lambda API's image response: ImageUri echoed on the config,
+ # plus ImageConfigResponse when an ImageConfig (Command/EntryPoint/
+ # WorkingDirectory) is supplied. GetFunction reads these back.
+ func["config"]["ImageUri"] = image_uri
+ if props.get("ImageConfig"):
+ func["config"]["ImageConfigResponse"] = {"ImageConfig": props["ImageConfig"]}
_lambda_svc._functions[name] = func
# On a stack UPDATE this re-provisions over an existing function; recycle the
# warm worker + docker pool so the new code/config load on the next invoke
diff --git a/ministack/services/cognito.py b/ministack/services/cognito.py
index d28bef07..9c2fec13 100644
--- a/ministack/services/cognito.py
+++ b/ministack/services/cognito.py
@@ -3894,11 +3894,6 @@ def _cloudfront_domain(domain: str) -> str:
return f"d{hashlib.sha256(domain.encode()).hexdigest()[:13]}.cloudfront.net"
-def _prefix_domain_host(domain: str, pid: str) -> str:
- """Regional Cognito host a prefix domain expands to."""
- return f"{domain}.auth.{_pool_region(pid)}.amazoncognito.com"
-
-
def _create_user_pool_domain(data):
pid = data.get("UserPoolId")
pool, err = _resolve_pool(pid)
@@ -3924,9 +3919,11 @@ def _create_user_pool_domain(data):
cloudfront = _cloudfront_domain(domain)
else:
pool["Domain"] = domain
- cloudfront = _prefix_domain_host(domain, pid)
+ # A prefix (Amazon Cognito) domain has no CloudFront alias target to
+ # CNAME to, so AWS returns a null CloudFrontDomain for it — omit it.
+ cloudfront = None
_pool_domain_map[domain] = pid
- return json_response({"CloudFrontDomain": cloudfront})
+ return json_response({"CloudFrontDomain": cloudfront} if cloudfront else {})
def _delete_user_pool_domain(data):
@@ -3939,7 +3936,7 @@ def _delete_user_pool_domain(data):
if domain and pool.get("CustomDomain") == domain:
pool["CustomDomain"] = None
pool.pop("_custom_domain_config", None)
- else:
+ elif domain and pool.get("Domain") == domain:
pool["Domain"] = None
return json_response({})
@@ -3957,8 +3954,10 @@ def _describe_user_pool_domain(data):
"AWSAccountId": get_account_id(),
"Domain": domain,
"S3Bucket": "",
+ # Only a custom domain has a CloudFront alias target; a prefix
+ # domain has none (same as its null CloudFrontDomain on create).
"CloudFrontDistribution": (_cloudfront_domain(domain) if is_custom
- else _prefix_domain_host(domain, pid)),
+ else ""),
"Version": "1",
"Status": "ACTIVE",
"CustomDomainConfig": pool.get("_custom_domain_config", {}) if is_custom else {},
diff --git a/ministack/services/dynamodb.py b/ministack/services/dynamodb.py
index fce1dc40..3c9f7ffd 100644
--- a/ministack/services/dynamodb.py
+++ b/ministack/services/dynamodb.py
@@ -548,6 +548,13 @@ def _validate_item(item: dict, pk_name: str | None = None, sk_name: str | None =
# DynamoDB Streams: table_name -> list of stream records
# Each record follows the DynamoDB Streams event format consumed by Lambda ESMs.
_stream_records = AccountRegionScopedDict()
+
+
+def _stream_label() -> str:
+ """A DynamoDB stream label: ISO 8601 with millisecond precision and NO
+ trailing ``Z``, matching the form AWS puts in a stream ARN — e.g.
+ ``2015-05-11T21:21:33.291`` (not ``now_iso()``'s ``...291Z``)."""
+ return now_iso()[:-1]
# Records dropped off the front of each table's stream, so a consumer's read
# position stays an absolute index into the stream rather than an offset into
# the surviving slice.
@@ -671,7 +678,7 @@ def _build_change_record(table: dict, event_name: str, old_item: dict | None, ne
"SizeBytes": 0,
"StreamViewType": view_type,
},
- "eventSourceARN": f"{table['TableArn']}/stream/{now_iso()}",
+ "eventSourceARN": f"{table['TableArn']}/stream/{_stream_label()}",
}
ref_item = new_item or old_item or {}
@@ -1229,7 +1236,7 @@ def _create_table(data):
"MaxWriteRequestUnits": int(on_demand.get("MaxWriteRequestUnits", -1)),
}
if data.get("StreamSpecification"):
- stream_label = now_iso()
+ stream_label = _stream_label()
_tables[name]["LatestStreamLabel"] = stream_label
_tables[name]["LatestStreamArn"] = f"{_tables[name]['TableArn']}/stream/{stream_label}"
if data.get("Tags"):
@@ -1327,7 +1334,7 @@ def _update_table(data):
stream_was_enabled = bool((table.get("StreamSpecification") or {}).get("StreamEnabled"))
table["StreamSpecification"] = stream_spec
if stream_spec.get("StreamEnabled") and not stream_was_enabled:
- stream_label = now_iso()
+ stream_label = _stream_label()
table["LatestStreamLabel"] = stream_label
table["LatestStreamArn"] = f"{table['TableArn']}/stream/{stream_label}"
if "SSESpecification" in data:
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index a1f8b02f..9f0d95f7 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -1121,15 +1121,22 @@ def _fetch_code_from_s3(bucket: str, key: str, version_id: str | None = None) ->
_UNZIPPED_LIMIT_BYTES = 262144000 # 250 MiB — AWS hard limit
-def _validate_unzipped_size(zip_data: bytes | None):
+def _unzipped_size(zip_data: bytes | None) -> int:
+ """Total uncompressed size of a zip's members. Empty or unreadable data
+ (an image function, or a layer whose bytes we don't hold) contributes 0."""
if not zip_data:
- return None
+ return 0
try:
with zipfile.ZipFile(io.BytesIO(zip_data)) as zf:
- unzipped_size = sum(info.file_size for info in zf.infolist())
+ return sum(info.file_size for info in zf.infolist())
except zipfile.BadZipFile:
- return None
- if unzipped_size > _UNZIPPED_LIMIT_BYTES:
+ return 0
+
+
+def _validate_unzipped_size(zip_data: bytes | None):
+ """A single deployment package's unzipped size against the 250 MB quota —
+ used when publishing a layer version, whose own content must fit on its own."""
+ if _unzipped_size(zip_data) > _UNZIPPED_LIMIT_BYTES:
return error_response_json(
"InvalidParameterValueException",
f"Unzipped size must be smaller than {_UNZIPPED_LIMIT_BYTES} bytes",
@@ -1138,6 +1145,37 @@ def _validate_unzipped_size(zip_data: bytes | None):
return None
+def _layer_unzipped_size(layer_arn: str) -> int:
+ """Unzipped size of an attached layer version's content. A layer we don't
+ hold the bytes for (an external ARN, or content that failed to fetch)
+ contributes 0 — it can't be measured, so it isn't counted rather than
+ guessed at (never over-rejecting a function that AWS would accept)."""
+ version_config, err = _resolve_layer_version_for_attachment(layer_arn)
+ if err or not version_config:
+ return 0
+ return _unzipped_size(version_config.get("_zip_data"))
+
+
+def _validate_total_unzipped_size(code_zip: bytes | None, layers):
+ """The 250 MB unzipped quota is the total of the function code plus every
+ attached layer, unzipped — not each package on its own. AWS: the limit is
+ "the maximum size of the contents of a deployment package, including
+ layers and custom runtimes" (unzipped)."""
+ total = _unzipped_size(code_zip)
+ for layer in layers or []:
+ arn = layer.get("Arn") if isinstance(layer, dict) else layer
+ if arn:
+ total += _layer_unzipped_size(arn)
+ if total > _UNZIPPED_LIMIT_BYTES:
+ return error_response_json(
+ "InvalidParameterValueException",
+ f"Unzipped size must be smaller than {_UNZIPPED_LIMIT_BYTES} bytes",
+ 400,
+ )
+ return None
+ return None
+
+
import contextvars
# Per-invocation durable-execution context, set by `_invoke` and read by the
@@ -1887,7 +1925,7 @@ def _create_function(data: dict):
version_id=code_data.get("S3ObjectVersion"),
)
- err = _validate_unzipped_size(code_zip)
+ err = _validate_total_unzipped_size(code_zip, data.get("Layers"))
if err is not None:
return err
@@ -2306,7 +2344,7 @@ def _update_code(name: str, data: dict):
f"Failed to fetch code from s3://{data['S3Bucket']}/{data['S3Key']}",
400,
)
- err = _validate_unzipped_size(code_zip)
+ err = _validate_total_unzipped_size(code_zip, func["config"].get("Layers"))
if err is not None:
return err
if code_zip:
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index 67944fb0..9cb9f0bc 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -1819,6 +1819,43 @@ def test_cfn_lambda_url_uses_function_url_state(cfn, lam):
)["FunctionUrlConfigs"] == []
+def test_cfn_lambda_image_package_type(cfn, lam):
+ """AWS::Lambda::Function with PackageType=Image is created as an image, not a Zip."""
+ suffix = _uuid_mod.uuid4().hex[:8]
+ stack_name = f"cfn-lambda-image-{suffix}"
+ function_name = f"cfn-image-{suffix}"
+ template = {
+ "AWSTemplateFormatVersion": "2010-09-09",
+ "Resources": {
+ "Function": {
+ "Type": "AWS::Lambda::Function",
+ "Properties": {
+ "FunctionName": function_name,
+ "PackageType": "Image",
+ "Role": "arn:aws:iam::000000000000:role/lambda-role",
+ "Code": {"ImageUri": "public.ecr.aws/lambda/python:3.12"},
+ "ImageConfig": {"Command": ["app.handler"]},
+ },
+ },
+ },
+ }
+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+
+ got = lam.get_function(FunctionName=function_name)
+ config = got["Configuration"]
+ assert config["PackageType"] == "Image"
+ assert got["Code"]["ImageUri"] == "public.ecr.aws/lambda/python:3.12"
+ # An image package carries no Runtime/Handler on AWS.
+ assert not config.get("Runtime")
+ assert not config.get("Handler")
+ assert config["ImageConfigResponse"] == {"ImageConfig": {"Command": ["app.handler"]}}
+
+ cfn.delete_stack(StackName=stack_name)
+ _wait_stack(cfn, stack_name)
+
+
def test_cfn_lambda_permission_qualified_arn_uses_base_function_policy(cfn, lam):
"""Qualified FunctionName refs should add permission to the base function policy."""
suffix = _uuid_mod.uuid4().hex[:8]
diff --git a/tests/test_cognito.py b/tests/test_cognito.py
index e88d20ba..46339531 100644
--- a/tests/test_cognito.py
+++ b/tests/test_cognito.py
@@ -415,7 +415,8 @@ def test_cognito_admin_add_remove_user_from_group(cognito_idp):
def test_cognito_domain_crud(cognito_idp):
pid = cognito_idp.create_user_pool(PoolName="DomainPool")["UserPool"]["Id"]
resp = cognito_idp.create_user_pool_domain(UserPoolId=pid, Domain="my-test-domain")
- assert "CloudFrontDomain" in resp
+ # Prefix domain: AWS returns a null CloudFrontDomain (no alias target).
+ assert "CloudFrontDomain" not in resp
desc = cognito_idp.describe_user_pool_domain(Domain="my-test-domain")
assert desc["DomainDescription"]["UserPoolId"] == pid
@@ -5432,13 +5433,13 @@ def test_cognito_user_pool_domain_cloudfront_uses_pool_region():
domain = f"domain-region-{pid.split('_', 1)[1].lower()}"
create_resp = client.create_user_pool_domain(Domain=domain, UserPoolId=pid)
- assert create_resp["CloudFrontDomain"].endswith(".auth.eu-central-1.amazoncognito.com"), (
- create_resp["CloudFrontDomain"]
- )
+ # A prefix domain has no CloudFront alias target — AWS returns a null
+ # CloudFrontDomain (and an empty CloudFrontDistribution on describe).
+ assert not create_resp.get("CloudFrontDomain"), create_resp.get("CloudFrontDomain")
desc_resp = client.describe_user_pool_domain(Domain=domain)
- assert desc_resp["DomainDescription"]["CloudFrontDistribution"].endswith(
- ".auth.eu-central-1.amazoncognito.com"
- ), desc_resp["DomainDescription"]["CloudFrontDistribution"]
+ assert desc_resp["DomainDescription"]["CloudFrontDistribution"] == "", (
+ desc_resp["DomainDescription"]["CloudFrontDistribution"]
+ )
def test_cognito_email_disabled_env_skips_send(cognito_idp, monkeypatch):
@@ -7129,10 +7130,11 @@ def test_cognito_prefix_domain_unchanged_by_custom_domain_support(cognito_idp):
pid = cognito_idp.create_user_pool(PoolName="PrefixDomainPool")["UserPool"]["Id"]
domain = f"prefix-{pid.split('_', 1)[1].lower()}"
resp = cognito_idp.create_user_pool_domain(UserPoolId=pid, Domain=domain)
- assert resp["CloudFrontDomain"] == f"{domain}.auth.us-east-1.amazoncognito.com"
+ # Prefix domain: AWS returns a null CloudFrontDomain (no alias target).
+ assert not resp.get("CloudFrontDomain")
dd = cognito_idp.describe_user_pool_domain(Domain=domain)["DomainDescription"]
- assert dd["CloudFrontDistribution"] == f"{domain}.auth.us-east-1.amazoncognito.com"
+ assert dd["CloudFrontDistribution"] == ""
assert dd["CustomDomainConfig"] == {}
pool = cognito_idp.describe_user_pool(UserPoolId=pid)["UserPool"]
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index 9a174a8b..6e901f44 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -2862,6 +2862,52 @@ def test_lambda_function_with_layer(lam):
assert layer_arn in fn["Configuration"]["Layers"][0]["Arn"]
+def test_lambda_unzipped_limit_counts_code_plus_layers(lam):
+ """The 250 MB unzipped quota is the total of function code + all attached
+ layers, not each package on its own (#1414)."""
+ MB = 1024 * 1024
+
+ def _zip_reporting(nbytes):
+ # Zeros compress away, so the archive stays tiny while its single member
+ # reports nbytes uncompressed — enough to exercise the quota without
+ # materializing hundreds of MB (the check reads the zip header, not the
+ # decompressed bytes).
+ buf = io.BytesIO()
+ with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as z:
+ z.writestr("index.py", b"\0" * nbytes)
+ return buf.getvalue()
+
+ role = "arn:aws:iam::000000000000:role/test"
+ big = _zip_reporting(150 * MB) # 150 MB unzipped — under 250 on its own
+
+ # A 150 MB layer and 150 MB of code each pass the per-package limit.
+ layer = lam.publish_layer_version(
+ LayerName="big-1414-layer", Content={"ZipFile": big}
+ )["LayerVersionArn"]
+ lam.create_function(
+ FunctionName="fn-1414-code-only", Runtime="python3.12", Role=role,
+ Handler="index.handler", Code={"ZipFile": big},
+ )
+
+ # But 150 MB code + the 150 MB layer = 300 MB unzipped, which AWS rejects.
+ with pytest.raises(ClientError) as ei:
+ lam.create_function(
+ FunctionName="fn-1414-over", Runtime="python3.12", Role=role,
+ Handler="index.handler", Code={"ZipFile": big}, Layers=[layer],
+ )
+ assert ei.value.response["Error"]["Code"] == "InvalidParameterValueException"
+
+ # 150 MB code + a 1 MB layer (151 MB total) stays under the limit — no
+ # false positive now that code and layers are summed.
+ small_layer = lam.publish_layer_version(
+ LayerName="small-1414-layer", Content={"ZipFile": _zip_reporting(1 * MB)}
+ )["LayerVersionArn"]
+ lam.create_function(
+ FunctionName="fn-1414-under", Runtime="python3.12", Role=role,
+ Handler="index.handler", Code={"ZipFile": big}, Layers=[small_layer],
+ )
+
+
def test_lambda_rejects_cross_region_layers_on_create_and_update():
east = _regional_client("lambda", "us-east-1")
west = _regional_client("lambda", "us-west-2")