Menu

Validate Environment Variables in Lambda with Zod
📰

Validate Environment Variables in Lambda with Zod

DEV Community: typescript·Sascha Mirow-Stanitzok·9 days ago
#ndheP5BK
Reading 0:00
15s threshold

If you want to validate environment variables, you usually only think about it after a Lambda function has spent a night working with undefined . The topic keeps bugging me because these errors are so unnecessary: the configuration is fixed before the first request arrives. Still, many teams only find out in production that a value is missing, misspelled or arrives as text instead of a number.

In this article I build a small, complete solution with TypeScript, Zod and the Serverless Framework. It checks the configuration twice: before the deployment in the pipeline and once more during the cold start of the function. The example is a fictional reminder service of a city library that reminds members to return their books.

In short

In Lambda, environment variables always arrive as text and are sometimes missing entirely. A Zod schema that is checked once at cold start turns them into a typed configuration object and lets the function fail immediately if something is wrong.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.