Skip to content

feat(commons): environment variable helpers #3945

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 10 commits into from
May 22, 2025
Merged

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented May 20, 2025

Summary

Changes

Please provide a summary of what's being changed

This PR adds new zero-dependency functional utilities to read and parse environment variables.

Customers can read and cast environment variables into string, number, and boolean using the respective helpers.

By default, the environment variable is considered required, and the function will throw an error if not present.

import { getStringFromEnv } from '@aws-lambda-powertools/commons/utils/env';

const myEnvVar = getStringFromEnv({
  key: 'MY_ENV_VAR',
});

Customers can customize the error message thrown by the helper by passing an optional errorMessage parameter.

import { getBooleanFromEnv } from '@aws-lambda-powertools/commons/utils/env';

const myEnvVar = getBooleanFromEnv({
  key: 'MY_ENV_VAR',
  errorMessage: 'MY_ENV_VAR is required for this function',
});

Alternatively, if the env variable is optional, they can also provide a default fallback value that will be used if the env variable is not present.

import { getNumberFromEnv } from '@aws-lambda-powertools/commons/utils/env';

const myEnvVar = getNumberFromEnv({
  key: 'MY_ENV_VAR',
  defaultValue: 42,
});

The PR also adds a few higher-level utilities to read slightly more complex values from environment variables.

For example, getTruthyBooleanFromEnv and getFalsyBooleanFromEnv respectively allow customers to read values like 1, y, yes, on, true, 0, n, no, off, false and cast them to their corresponding boolean values.

import {
  getTruthyBooleanFromEnv,
  getFalsyBooleanFromEnv
} from '@aws-lambda-powertools/commons/utils/env';

process.env.MY_ENV_VAR = 'on';
const myEnvVar = getTruthyBooleanFromEnv({
  key: 'MY_ENV_VAR',
});
// ^ true

process.env.MY_ENV_VAR = 'no';
const myEnvVar = getFalsyBooleanFromEnv({
  key: 'MY_ENV_VAR',
});
// ^ true

Likewise, the isDevMode helper allows customers to detect if their application is running in a development environment based on the POWERTOOLS_DEV env variable, and the getServiceName one retrieves the value of the POWERTOOLS_SERVICE_NAME one.

Finally, the getXrayTraceDataFromEnv, isRequestXRaySampled, and getXRayTraceIdFromEnv all read the _X_AMZN_TRACE_ID env variable and extract/return different kind of information from it.

While this work comes from a feature request (#3159) most of the advantage to customers will come in the form of smaller utilities over time, since we'll be able to use these helpers across other Powertools for AWS utilities and replace the existing EnvironmentVariablesService class-based model which is extremely verbose and requires props drilling if you want to access env variables deep into an inheritance stack.

For Logger, Tracer, and Metrics we'll migrate to these utilities in the next major version while for other utilities we'll evaluate on a case-by-case basis. New utilities should use these helpers since the beginning.

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

Issue number: closes #3159


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.

@dreamorosi dreamorosi self-assigned this May 20, 2025
@boring-cyborg boring-cyborg bot added commons This item relates to the Commons Utility dependencies Changes that touch dependencies, e.g. Dependabot, etc. tests PRs that add or change tests labels May 20, 2025
@pull-request-size pull-request-size bot added the size/XL PRs between 500-999 LOC, often PRs that grown with feedback label May 20, 2025
@github-actions github-actions bot added the feature PRs that introduce new features or minor changes label May 20, 2025
@dreamorosi dreamorosi requested a review from am29d May 20, 2025 13:35
@dreamorosi dreamorosi requested a review from svozza May 22, 2025 08:32
@dreamorosi dreamorosi merged commit 7cfcd85 into main May 22, 2025
41 checks passed
@dreamorosi dreamorosi deleted the feat/env_var_utils branch May 22, 2025 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commons This item relates to the Commons Utility dependencies Changes that touch dependencies, e.g. Dependabot, etc. feature PRs that introduce new features or minor changes size/XL PRs between 500-999 LOC, often PRs that grown with feedback tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Helper function for validating required environment variables
3 participants