Skip to content
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
20 changes: 15 additions & 5 deletions src/cfnlint/rules/resources/lmbd/FunctionEnvironmentSize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import annotations

import json
from typing import Any

from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
Expand All @@ -19,8 +20,8 @@ class FunctionEnvironmentSize(CfnLintKeyword):
description = (
"AWS Lambda limits the total size of all environment variables "
"to 4 KB. If this limit is exceeded, the deployment will fail. "
"This rule sums the lengths of all keys and values and validates "
"the total does not exceed 4096 bytes."
"This rule measures the size of the compact JSON encoding of the "
"variables and validates the total does not exceed 4096 bytes."
)
source_url = (
"https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html"
Expand All @@ -43,13 +44,22 @@ def validate(
if not validator.is_type(instance, "object"):
return

total_size = 0
variables: dict[str, str] = {}
for key, value in instance.items():
if not isinstance(key, str):
continue
total_size += len(key)
if isinstance(value, str):
total_size += len(value)
variables[key] = value

# Lambda measures the variables as compact JSON, so the structural
# characters (quotes, colons, commas and braces) count toward the
# limit. Measure the UTF-8 byte length of the compact encoding
# instead of summing key and value lengths.
total_size = len(
json.dumps(variables, ensure_ascii=False, separators=(",", ":")).encode(
"utf-8"
)
)

max_size = 4096
if total_size > max_size:
Expand Down
52 changes: 48 additions & 4 deletions test/unit/rules/resources/lmbd/test_function_environment_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,34 @@ def rule():
[],
),
(
# 4096 bytes of key and value, but 4103 as compact JSON
{"A" * 2048: "B" * 2048},
[],
[
ValidationError(
"Lambda environment variables total size (4103) "
"exceeds the 4 KB (4096 bytes) limit",
rule=FunctionEnvironmentSize(),
),
],
),
(
{"A" * 2048: "B" * 2049},
[
ValidationError(
"Lambda environment variables total size (4104) "
"exceeds the 4 KB (4096 bytes) limit",
rule=FunctionEnvironmentSize(),
),
],
),
(
# Exactly 4096 bytes as compact JSON stays silent
{"K" * 2044: "V" * 2045},
[],
),
(
# One byte over the limit as compact JSON
{"K" * 2044: "V" * 2046},
[
ValidationError(
"Lambda environment variables total size (4097) "
Expand All @@ -40,17 +63,38 @@ def rule():
),
],
),
(
# 100 variables: 3500 bytes of key and value, 4101 as
# compact JSON (the 6n + 1 structural overhead matters)
{f"V{i:03d}": "a" * 31 for i in range(100)},
[
ValidationError(
"Lambda environment variables total size (4101) "
"exceeds the 4 KB (4096 bytes) limit",
rule=FunctionEnvironmentSize(),
),
],
),
(
{f"KEY{i}": "x" * 100 for i in range(50)},
[
ValidationError(
f"Lambda environment variables total size "
f"({sum(len(f'KEY{i}') + 100 for i in range(50))}) "
f"exceeds the 4 KB (4096 bytes) limit",
"Lambda environment variables total size (5541) "
"exceeds the 4 KB (4096 bytes) limit",
rule=FunctionEnvironmentSize(),
),
],
),
(
# Non-ASCII values count as UTF-8 bytes, not escape sequences
{"K": "é"},
[],
),
(
# JSON escaping (quotes) counts toward the limit
{"K": 'a"b'},
[],
),
(
"not-an-object",
[],
Expand Down
Loading