Skip to content

refactor(commons): extract shared isRunningInLambda helper - #5424

Merged
dreamorosi merged 4 commits into
mainfrom
chore/commons-is-running-in-lambda-5408
Jul 8, 2026
Merged

dreamorosi merged 4 commits into
mainfrom
chore/commons-is-running-in-lambda-5408

Conversation

@dreamorosi

Copy link
Copy Markdown
Contributor

Summary

Extracts the heuristic for detecting whether code is running inside a real AWS Lambda execution environment into a single shared isRunningInLambda() helper, so all current and future call sites share one definition instead of maintaining verbatim copies that must be updated in lockstep.

Changes

  • Add isRunningInLambda() to packages/commons/src/envUtils.ts, exported via @aws-lambda-powertools/commons/utils/env; it reads AWS_LAMBDA_INITIALIZATION_TYPE with an 'unknown' fallback and returns false when the type is unknown or when running in dev mode (isDevMode())
  • Define the env var name once as AWS_LAMBDA_INITIALIZATION_TYPE_ENV_VAR in packages/commons/src/constants.ts
  • Refactor getMetadata in packages/commons/src/metadata.ts to use the new helper instead of its inline copy
  • Refactor Utility#getInitializationType to read the env var via the shared constant (kept reading process.env directly to avoid pulling the @aws/lambda-invoke-store side-effect import from envUtils.js into the package main entry)
  • Add unit tests covering on-demand, provisioned-concurrency, unset variable, and dev-mode override

Note: the second call site mentioned in the issue (parameters/src/appconfig-agent/getConfig.ts) comes from #5399, which is still open — it can adopt the helper once both land.

Issue number: closes #5408


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

The heuristic for detecting a real AWS Lambda execution environment
(read AWS_LAMBDA_INITIALIZATION_TYPE with an 'unknown' fallback, then
check dev mode) was duplicated inline in getMetadata and about to be
copied by other features. Extract it into a single isRunningInLambda()
helper in envUtils, define the env var name once in constants, and
reuse the constant in Utility#getInitializationType so future changes
to the detection logic happen in one place.

Fixes #5408
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/M PR between 30-99 LOC and removed size/M PR between 30-99 LOC labels Jul 8, 2026
@dreamorosi
dreamorosi requested a review from svozza July 8, 2026 18:28
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/M PR between 30-99 LOC and removed size/M PR between 30-99 LOC labels Jul 8, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/M PR between 30-99 LOC and removed size/M PR between 30-99 LOC labels Jul 8, 2026
@dreamorosi
dreamorosi merged commit aec888b into main Jul 8, 2026
42 checks passed
@dreamorosi
dreamorosi deleted the chore/commons-is-running-in-lambda-5408 branch July 8, 2026 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M PR between 30-99 LOC

2 participants