Skip to content

Replace Pydantic's builtin Secret Support with a configuration provider that loads parameters from AWS Systems Manager Parameter Store.

License

Notifications You must be signed in to change notification settings

developmentseed/pydantic-ssm-settings

Repository files navigation

pydantic-ssm-settings

Integrate AWS Systems Manager Parameter Store with 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.

from pydantic_ssm_settings import SsmBaseSettings


class WebserviceSettings(SsmBaseSettings):
    some_val: str
    another_val: int

WebserviceSettings(_ssm_prefix="/prod/webservice")

Alternatively, configuration may be specified within the settings class via BaseModel.model_config:

from pydantic_ssm_settings import SsmSettingsConfigDict

class WebserviceSettings(SsmBaseSettings):
    model_config = SsmSettingsConfigDict(ssm_prefix="/prod/webservice")
    some_val: str
    another_val: int


WebserviceSettings()

If it is preferred to avoid altering the baseclass of a settings model, the source can be manually added and configured as such:

from typing import Tuple, Type
from pydantic_settings import (
    BaseSettings,
    EnvSettingsSource,
    InitSettingsSource,
    PydanticBaseSettingsSource,
    SecretsSettingsSource,
)
from pydantic_ssm_settings import SsmSettingsConfigDict, SsmSettingsSource

class WebserviceSettings(BaseSettings):
    model_config = SsmSettingsConfigDict(ssm_prefix="/asdf")
    foo: str

    def 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),
        )

About

Replace Pydantic's builtin Secret Support with a configuration provider that loads parameters from AWS Systems Manager Parameter Store.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages