AWS Lambda Environment Modeler

raw JSON →
3.0.0 verified Fri May 01 auth: no python

AWS-Lambda-Env-Modeler is a Python library (version 3.0.0, release cadence: irregular) that simplifies managing and validating environment variables in AWS Lambda functions using Python dataclasses, Pydantic models, or TypedDicts. It provides decorators and base classes to parse, validate, and cache environment configuration.

pip install aws-lambda-env-modeler
error AttributeError: module 'aws_lambda_env_modeler' has no attribute 'LambdaEnvModel'
cause `LambdaEnvModel` was removed in v2.0.0.
fix
Replace LambdaEnvModel with BaseModel or EnvModel.
error TypeError: parse_env() got an unexpected keyword argument 'cache'
cause The `cache` parameter was renamed to `use_cache` in v1.0.7.
fix
Use use_cache=False instead of cache=False.
error ImportError: cannot import name 'BaseModel' from 'aws_lambda_env_modeler'
cause The library is not installed or you are using an old version (<2.0.0).
fix
Install the latest version: pip install --upgrade aws-lambda-env-modeler. If on v1.x, use from aws_lambda_env_modeler import LambdaEnvModel.
breaking v3.0.0 drops Python 3.9 support (requires >=3.10). Pin to 2.1.0 if you need Python 3.9.
fix Upgrade to Python 3.10+ or pin to aws-lambda-env-modeler==2.1.0
deprecated `LambdaEnvModel` was removed in v2.0.0. Use `BaseModel` or `EnvModel` instead.
fix Replace `LambdaEnvModel` with `BaseModel` from the same package.
gotcha The `parse_env` decorator uses LRU caching by default (via lru_cache). This can cause stale values in tests or when environment changes.
fix Pass `use_cache=False` to `parse_env` in tests: `@parse_env(model_class=MyEnv, use_cache=False)`.
gotcha Environment variables are parsed only once per process due to caching. If you change env vars at runtime, the model will not reflect the new values.
fix Avoid changing env vars after the first invocation. If necessary, disable cache or use `force_refresh=True`.

Define an environment model using BaseModel, then decorate your Lambda handler with @parse_env. The parsed environment is attached to the context object. Alternatively, instantiate the model directly for testing.

import os
from aws_lambda_env_modeler import BaseModel, parse_env

class MyEnv(BaseModel):
    table_name: str
    stage: str = "dev"

@parse_env(model_class=MyEnv)
def lambda_handler(event, context):
    env = context.env  # or use event['env']
    print(f"Table: {env.table_name}, Stage: {env.stage}")
    return {"statusCode": 200}

# For testing without Lambda context:
os.environ['TABLE_NAME'] = 'my-table'
os.environ['STAGE'] = 'prod'
env = MyEnv()  # directly instantiate
print(env.table_name)