generated from ran-isenberg/aws-lambda-handler-cookbook
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: Ran Isenberg <[email protected]>
- Loading branch information
1 parent
3be8f5b
commit f770d8f
Showing
16 changed files
with
233 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,102 @@ | ||
|
||
# AWS Lambda Handler Environment Variables (Python) | ||
# AWS Lambda Handler Environment Variables Modeler (Python) | ||
|
||
[](https://github.com/ran-isenberg/aws-lambda-env-vars-parser/blob/master/LICENSE) | ||
[](https://github.com/ran-isenberg/aws-lambda-env-modeler/blob/master/LICENSE) | ||
 | ||
 | ||
 | ||
[](https://codecov.io/gh/ran-isenberg/aws-lambda-env-vars-parser) | ||
 | ||
 | ||
 | ||
 | ||
[](https://codecov.io/gh/ran-isenberg/aws-lambda-env-modeler) | ||
 | ||
 | ||
 | ||
 | ||
|
||
 | ||
 | ||
|
||
This project provides a | ||
AWS-Lambda-Env-Modeler is a Python library designed to simplify the process of managing and validating environment variables in your AWS Lambda functions. | ||
|
||
**[📜Documentation](https://ran-isenberg.github.io/aws-lambda-env-vars-parser/)** | **[Blogs website](https://www.ranthebuilder.cloud)** | ||
It leverages the power of [Pydantic](https://pydantic-docs.helpmanual.io/) 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](https://ran-isenberg.github.io/aws-lambda-env-modeler/)** | **[Blogs website](https://www.ranthebuilder.cloud)** | ||
> **Contact details | [email protected]** | ||
|
||
## **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. | ||
|
||
## **The Solution** | ||
|
||
This library allows you to correctly parse, validate, and use your environment variables in your Python AWS Lambda code. | ||
|
||
Read more about it [here](https://www.ranthebuilder.cloud/post/aws-lambda-cookbook-environment-variables) | ||
|
||
### **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. | ||
|
||
- [Environment Variables](https://www.ranthebuilder.cloud/post/aws-lambda-cookbook-environment-variables) | ||
|
||
## Installation | ||
|
||
You can install it using pip: | ||
|
||
```bash | ||
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-vars-parser](https://ran-isenberg.github.io/aws-lambda-env-vars-parser/) | ||
Head over to the complete project documentation pages at GitHub pages at [https://ran-isenberg.github.io/aws-lambda-env-modeler](https://ran-isenberg.github.io/aws-lambda-env-modeler/) | ||
|
||
|
||
## Usage | ||
First, define a Pydantic model for your environment variables: | ||
|
||
```python | ||
from pydantic import BaseModel | ||
|
||
class MyEnvVariables(BaseModel): | ||
DB_HOST: str | ||
DB_PORT: int | ||
DB_USER: str | ||
DB_PASS: str | ||
``` | ||
|
||
You must first use the `@init_environment_variables` decorator to automatically validate and initialize the environment variables before executing a function: | ||
|
||
```python | ||
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: | ||
|
||
```python | ||
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.](https://github.com/ran-isenberg/aws-lambda-env-vars-parser/blob/main/CONTRIBUTING.md) | ||
Code contributions are welcomed. Read this [guide.](https://github.com/ran-isenberg/aws-lambda-env-modeler/blob/main/CONTRIBUTING.md) | ||
|
||
## Code of Conduct | ||
Read our code of conduct [here.](https://github.com/ran-isenberg/aws-lambda-env-vars-parser/blob/main/CODE_OF_CONDUCT.md) | ||
Read our code of conduct [here.](https://github.com/ran-isenberg/aws-lambda-env-modeler/blob/main/CODE_OF_CONDUCT.md) | ||
|
||
## Connect | ||
* Email: [[email protected]](mailto:[email protected]) | ||
|
@@ -48,4 +106,4 @@ Read our code of conduct [here.](https://github.com/ran-isenberg/aws-lambda-env- | |
|
||
|
||
## License | ||
This library is licensed under the MIT License. See the [LICENSE](https://github.com/ran-isenberg/aws-lambda-env-vars-parser/blob/main/LICENSE) file. | ||
This library is licensed under the MIT License. See the [LICENSE](https://github.com/ran-isenberg/aws-lambda-env-modeler/blob/main/LICENSE) file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Advanced event_parser utility | ||
""" | ||
from pydantic import BaseModel | ||
|
||
from .modeler import get_environment_variables, init_environment_variables | ||
from .types import Model | ||
|
||
__all__ = [ | ||
'Model', | ||
'BaseModel', | ||
'init_environment_variables', | ||
'get_environment_variables', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import os | ||
from functools import lru_cache, wraps | ||
from typing import Any, Callable, Dict, Type | ||
|
||
from aws_lambda_env_modeler.types import Model | ||
|
||
|
||
def get_environment_variables(model: Type[Model]) -> Model: | ||
""" | ||
This function receives a model of type Model, uses it to validate the environment variables, and returns the | ||
validated model. | ||
Args: | ||
model (Type[Model]): A Pydantic model that defines the structure and types of the expected environment variables. | ||
Returns: | ||
Model: An instance of the provided model filled with the values of the validated environment variables. | ||
""" | ||
return __parse_model(model) | ||
|
||
|
||
def init_environment_variables(model: Type[Model]): | ||
""" | ||
A decorator function for AWS Lambda handler functions that initializes environment variables based on the given Pydantic model before executing | ||
the decorated function. The decorator validates the environment variables according to the model structure before | ||
running the handler. | ||
Args: | ||
model (Type[Model]): A Pydantic model that defines the structure and types of the expected environment variables. | ||
Returns: | ||
Callable: A decorated function that first initializes the environment variables and then runs the function. | ||
""" | ||
|
||
def decorator(lambda_handler_function: Callable): | ||
|
||
@wraps(lambda_handler_function) | ||
def wrapper(event: Dict[str, Any], context): | ||
__parse_model(model) | ||
return lambda_handler_function(event, context) | ||
|
||
return wrapper | ||
|
||
return decorator | ||
|
||
|
||
@lru_cache | ||
def __parse_model(model: Type[Model]) -> Model: | ||
""" | ||
A helper function to validate and parse environment variables based on a given Pydantic model. This function is | ||
also cached to improve performance in successive calls. | ||
Args: | ||
model (Type[Model]): A Pydantic model that defines the structure and types of the expected environment variables. | ||
Returns: | ||
Model: An instance of the provided model filled with the values of the validated environment variables. | ||
Raises: | ||
ValueError: If the environment variables do not match the structure of the model or cannot be validated. | ||
""" | ||
try: | ||
return model.model_validate(os.environ) | ||
except Exception as exc: | ||
raise ValueError(f'failed to load environment variables, exception={str(exc)}') from exc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from typing import TypeVar | ||
|
||
from pydantic import BaseModel | ||
|
||
Model = TypeVar('Model', bound=BaseModel) | ||
|
||
__all__ = ['Model'] |
Empty file.
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.