Skip to content

Fetch Lambda properties on demand instead of calling ListFunctions - #25

Open
thehamsti wants to merge 1 commit into
BetterStackHQ:mainfrom
thehamsti:fix/lazy-lambda-properties-cache
Open

thehamsti wants to merge 1 commit into
BetterStackHQ:mainfrom
thehamsti:fix/lazy-lambda-properties-cache

Conversation

@thehamsti

Copy link
Copy Markdown

In AWS accounts with thousands of Lambda functions in a region, firehose_metrics_tag_enrichment paginates lambda:ListFunctions across every function in the region on every cold start and every CACHE_TTL_MINUTES refresh per concurrent Firehose container.

When functions use customer-managed KMS keys for environment variables, AWS Lambda internally triggers kms:Decrypt for every function returned by ListFunctions. In an account with ~16,000 Lambda functions (~400 actively emitting metrics), concurrent Firehose containers hit TooManyRequestsException throttling, causing 120s Lambda timeouts (~5,800/day, ~23% Firehose processing failures), container evictions, Firehose retries, and a sustained retry storm generating significant CloudTrail/GuardDuty/KMS overhead.

What changed

  • Replaced bulk ListFunctions pagination with an on-demand per-function cache (FunctionCache) backed by GetFunction, fetching only functions that actually appear in AWS/Lambda metrics (which already matches the existing lambda:GetFunction IAM grant and drastically reduces kms:Decrypt volume in large accounts).
  • Added a 30s per-invocation lookup budget (FUNCTION_LOOKUP_BUDGET) so a cold or throttled batch returns records with their tags (and any stale properties) well before the 120s Lambda timeout, warming remaining functions on subsequent invocations.
  • Mapped ResourceNotFoundException to Ok(None) so deleted or unresolvable function names clear stale properties and cache empty for cache_ttl (10 min), while transient errors preserve stale properties for ERROR_TTL (60s).
  • Intentionally left lambda:ListFunctions on the shared account-wide TagEnrichmentLambdaRole so multi-region rolling upgrades do not disrupt older regional binaries; removing it (and optionally moving to GetFunctionConfiguration) can follow in a separate PR.

Verification

  • Local CI: cargo test --locked (50 passed, 0 failed), cargo clippy --all-targets --locked -- -D warnings (clean), cargo fmt --check (clean).
  • Live AWS verification (provided.al2023 arm64 built via lambda/build.sh, deployed to a production account with ~16,000 Lambdas / ~400 active):
    • Direct Firehose-shaped invoke confirmed AWS/Lambda records receive full tags and properties (Runtime, MemorySize, Timeout, Architecture, PackageType), and missing functions (ResourceNotFoundException) succeed cleanly without an empty properties key.
    • Before fix: ~5,800 timeouts/day (120s hard limit), ~38s avg duration, 0.766 Firehose ExecuteProcessing.Success, sustained ListFunctions + kms:Decrypt storm.
    • After fix: 0 errors/timeouts, 1.0000 Firehose ExecuteProcessing.Success, 0.5–5.2s cold-cache batches, 1–3 ms warm batches, 25–31 MB max memory, and 0 ListFunctions calls.

Council review

Rounds: 2

Delegate Round 1 Round 2
Claude Opus 5.5 8/10 9/10
GPT 6.2 Sol 6/10 7/10
Gemini 4 Neon 9/10 9/10

Created with Gemini 4 Neon in T3 Code.

@thehamsti

Copy link
Copy Markdown
Author

Note

🤖 Gemini 4 Neon responding on behalf of @thehamsti

Follow-up from running this build live in production for an hour: this fixes both silent metric loss and significant AWS cost overhead, not just Lambda timeouts.

  • Metric delivery reliability: Before the fix, ListFunctions throttling and 120s timeouts caused 23.7% of Firehose processing batches to fail (ExecuteProcessing.Success at ~76.3%) and spill to the S3 backup bucket instead of reaching Better Stack. After deploying this change, ExecuteProcessing.Success reached 100% with zero failed batches written to S3, while keeping full AWS/Lambda tag and property enrichment intact.
  • AWS cost impact: Before the fix, the ListFunctions retry storm drove the vast majority of regional CloudTrail management events and KMS Decrypt calls, creating substantial recurring overhead across GuardDuty (PaidEventsAnalyzed), KMS, CloudTrail, and Lambda compute. With on-demand GetFunction caching, this Lambda's CloudTrail, KMS, and compute footprint dropped by >99% to negligible ongoing cost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant