Open garysassano opened 1 month ago
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 getRequiredEnvVar
multiple times, I see a better solution in having an environment object defined as a model. We could use zod parser to validate the model and also get a full error report for all environment variables.
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 process.env
, 2/ validate against pre-defined schema 3/ output full report on all variables.
I'd like to hear more feedback from the community and customers how they approach this problem and if it's worth implementing.
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
packagefunction getRequiredEnvVar<T = string>( name: string, options?: GetEnvVarOptions
): T;