heitorlessa / aws-lambda-env-modeler

AWS-Lambda-Env-Modeler is a Python library designed to simplify the process of managing and validating environment variables in your AWS Lambda functions.

Home Page:https://ran-isenberg.github.io/aws-lambda-env-modeler/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AWS Lambda Environment Variables Modeler (Python)

license PythonSupport PyPI version PyPi monthly downloads codecov version OpenSSF Scorecard issues

alt text

AWS-Lambda-Env-Modeler is a Python library designed to simplify the process of managing and validating environment variables in your AWS Lambda functions.

It leverages the power of Pydantic models to define the expected structure and types of the environment variables.

This library is especially handy for serverless applications where managing configuration via environment variables is a common practice.

📜Documentation | Blogs website

Contact details | ran.isenberg@ranthebuilder.cloud

The Problem

Environment variables are often viewed as an essential utility. They serve as static AWS Lambda function configuration.

Their values are set during the Lambda deployment, and the only way to change them is to redeploy the Lambda function with updated values.

However, many engineers use them unsafely despite being such an integral and fundamental part of any AWS Lambda function deployment.

This usage may cause nasty bugs or even crashes in production.

This library allows you to correctly parse, validate, and use your environment variables in your Python AWS Lambda code.

Read more about it here

Features

  • Validates the environment variables against a Pydantic model: define both semantic and syntactic validation.
  • Serializes the string environment variables into complex classes and types.
  • Provides means to access the environment variables safely with a global getter function in every part of the function.
  • Provides a decorator to initialize the environment variables before executing a function.
  • Caches the parsed model for performance improvement for multiple 'get' calls.

Installation

You can install it using pip:

pip install aws-lambda-env-modeler

Getting started

Head over to the complete project documentation pages at GitHub pages at https://ran-isenberg.github.io/aws-lambda-env-modeler

Usage

First, define a Pydantic model for your environment variables:

from pydantic import BaseModel, HttpUrl

class MyEnvVariables(BaseModel):
    DB_HOST: str
    DB_PORT: int
    DB_USER: str
    DB_PASS: str
    FLAG_X:  bool
    API_URL: HttpUrl

You must first use the @init_environment_variables decorator to automatically validate and initialize the environment variables before executing a function:

from aws_lambda_env_modeler import init_environment_variables

@init_environment_variables(MyEnvVariables)
def my_handler_entry_function(event, context):
    # At this point, environment variables are already validated and initialized
    pass

Then, you can fetch and validate the environment variables with your model:

from aws_lambda_env_modeler import get_environment_variables

env_vars = get_environment_variables(MyEnvVariables)
print(env_vars.DB_HOST)

Code Contributions

Code contributions are welcomed. Read this guide.

Code of Conduct

Read our code of conduct here.

Connect

License

This library is licensed under the MIT License. See the LICENSE file.

About

AWS-Lambda-Env-Modeler is a Python library designed to simplify the process of managing and validating environment variables in your AWS Lambda functions.

https://ran-isenberg.github.io/aws-lambda-env-modeler/

License:MIT License


Languages

Language:Python 76.7%Language:Makefile 23.3%