https://github.com/developmentseed/pydantic-ssm-settings
Replace Pydantic's builtin Secret Support with a configuration provider that loads parameters from AWS Systems Manager Parameter Store.
https://github.com/developmentseed/pydantic-ssm-settings
aws parameter-store pydantic python ssm
Last synced: 4 months ago
JSON representation
Replace Pydantic's builtin Secret Support with a configuration provider that loads parameters from AWS Systems Manager Parameter Store.
- Host: GitHub
- URL: https://github.com/developmentseed/pydantic-ssm-settings
- Owner: developmentseed
- License: mit
- Created: 2021-09-07T20:49:37.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-12-29T12:15:20.000Z (almost 2 years ago)
- Last Synced: 2024-04-25T12:21:43.368Z (over 1 year ago)
- Topics: aws, parameter-store, pydantic, python, ssm
- Language: Python
- Homepage: https://pypi.org/project/pydantic-ssm-settings/
- Size: 182 KB
- Stars: 18
- Watchers: 3
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# pydantic-ssm-settings
Integrate [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html) with [Pydantic Settings](https://github.com/pydantic/pydantic-settings).
## Usage
The simplest way to use this module is to inhert your settings from `SsmBaseSettings`. This add the `SsmSettingsSource` as a custom settings source and enabled passing source configuration (e.g. `_ssm_prefix`, `_ssm_client`) via `kwargs` when initializing a settings class.
```py
from pydantic_ssm_settings import SsmBaseSettingsclass WebserviceSettings(SsmBaseSettings):
some_val: str
another_val: intWebserviceSettings(_ssm_prefix="/prod/webservice")
```Alternatively, configuration may be specified within the settings class via [`BaseModel.model_config`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_config):
```py
from pydantic_ssm_settings import SsmSettingsConfigDictclass WebserviceSettings(SsmBaseSettings):
model_config = SsmSettingsConfigDict(ssm_prefix="/prod/webservice")
some_val: str
another_val: intWebserviceSettings()
```If it is preferred to avoid altering the baseclass of a settings model, the source can be manually added and configured as such:
```py
from typing import Tuple, Type
from pydantic_settings import (
BaseSettings,
EnvSettingsSource,
InitSettingsSource,
PydanticBaseSettingsSource,
SecretsSettingsSource,
)
from pydantic_ssm_settings import SsmSettingsConfigDict, SsmSettingsSourceclass WebserviceSettings(BaseSettings):
model_config = SsmSettingsConfigDict(ssm_prefix="/asdf")
foo: strdef settings_customise_sources(
self,
settings_cls: Type[BaseSettings],
init_settings: InitSettingsSource,
env_settings: EnvSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: SecretsSettingsSource,
) -> Tuple[PydanticBaseSettingsSource, ...]:
return (
init_settings,
env_settings,
dotenv_settings,
file_secret_settings,
SsmSettingsSource(settings_cls),
)
```