feat(commons): environment variable helpers#3945
Merged
dreamorosi merged 10 commits intomainfrom May 22, 2025
Merged
Conversation
svozza
reviewed
May 22, 2025
svozza
approved these changes
May 22, 2025
|
am29d
approved these changes
May 22, 2025
This was referenced May 23, 2025
This was referenced Jul 5, 2025
Closed
This was referenced Aug 1, 2025
Closed
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Changes
This PR adds new zero-dependency functional utilities to read and parse environment variables.
Customers can read and cast environment variables into
string,number, andbooleanusing the respective helpers.By default, the environment variable is considered required, and the function will throw an error if not present.
Customers can customize the error message thrown by the helper by passing an optional
errorMessageparameter.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.
The PR also adds a few higher-level utilities to read slightly more complex values from environment variables.
For example,
getTruthyBooleanFromEnvandgetFalsyBooleanFromEnvrespectively allow customers to read values like1,y,yes,on,true,0,n,no,off,falseand cast them to their correspondingbooleanvalues.Likewise, the
isDevModehelper allows customers to detect if their application is running in a development environment based on thePOWERTOOLS_DEVenv variable, and thegetServiceNameone retrieves the value of thePOWERTOOLS_SERVICE_NAMEone.Finally, the
getXrayTraceDataFromEnv,isRequestXRaySampled, andgetXRayTraceIdFromEnvall read the_X_AMZN_TRACE_IDenv 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
EnvironmentVariablesServiceclass-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.
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.