env_vars_implementation
source link: https://gist.github.com/ran-isenberg/b5db6b9b68d92a81bdc614dd0f976e19
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Instantly share code, notes, and snippets.
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator from pydantic import BaseModel, ValidationError
# global isntance of the parsed Pydantic data class ENV_CONF: Type[BaseModel] = None
@lambda_handler_decorator def init_environment_variables(handler, event, context, model: Type[BaseModel]) -> Any: global ENV_CONF try: # parse the os enviorment variables dict ENV_CONF = model(**os.environ) except (ValidationError, TypeError) as exc: raise ValueError(f'failed to load environment variables, exception={str(exc)}') from exc
return handler(event, context)
def get_environment_variables() -> Type[BaseModel]: global ENV_CONF if ENV_CONF is None: raise ValueError('get_environment_variables was called before init_environment_variables, environment variables were not loaded') return ENV_CONF
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK