-
Notifications
You must be signed in to change notification settings - Fork 155
Feature request: Helper function for validating required environment variables #3159
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
Comments
Hey @garysassano , thanks for raising the issue. This is a common use case where you'd spend few lines of code to fetch and validate the environment variables that we could simplify and provide better error handling, especially for higher number of variables (10+). The requirements and the solution remind me of https://github.com/ran-isenberg/aws-lambda-env-modeler from @ran-isenberg. Instead of calling const envSchema = z.object({
DB_NAME: z.string(),
DB_PORT: z.number(),
})
type MyEnvs = z.infer<typeof envSchema>;
const myEnvs: Myenvs = validateEnvs(envSchema)
const handler = async (event: unknown, context: Context) => {
// myEnvs is available, valid and typed here
} The main benefit we could provide is 1/ fetch a list of variables from I'd like to hear more feedback from the community and customers how they approach this problem and if it's worth implementing. |
Personally I don't see what Powertools is adding here, if customers already have to provide their own schema, in the example const envSchema = z.object({
DB_NAME: z.string(),
DB_PORT: z.number(),
})
type MyEnvs = z.infer<typeof envSchema>;
const myEnvs: Myenvs = envSchema.parse(process.env);
const handler = async (event: unknown, context: Context) => {
// myEnvs is available, valid and typed here
} I'm also not a huge fan of bringing in a 3rd party dependency to read environment variables, since in practice you can only store strings and the total size of all env variables, including keys, cannot exceed 4 KB. This limits quite a lot what you can or should store in the environment. Having a |
Use case
It would be helpful to have a helper function to validate that required environment variables are present and not empty. This is a common use case when developing Lambda functions, and currently requires developers to write repetitive boilerplate code. This will improve error handling and make code more robust and maintainable.
Solution/User Experience
Create a helper function
getRequiredEnvVar
that:Example Usage
Benefits
Implementation Notes
@aws-lambda-powertools/commons
packageMissingEnvironmentVariableError
) for better error handlingAlternative solutions
No response
Acknowledgment
Future readers
Please react with 👍 and your use case to help us understand customer demand.
The text was updated successfully, but these errors were encountered: