|
1 | 1 | # pydantic-ssm-settings |
2 | 2 |
|
3 | | -Replace Pydantic's builtin [Secret Support](https://pydantic-docs.helpmanual.io/usage/settings/#secret-support) with a configuration provider that loads parameters from [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html). Parameters are loaded _lazily_, meaning that they are only requested from AWS if they are not provided via [standard field value priority](https://pydantic-docs.helpmanual.io/usage/settings/#field-value-priority) (i.e. initialiser, environment variable, or via `.env` file). |
| 3 | +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). |
4 | 4 |
|
5 | 5 | ## Usage |
6 | 6 |
|
7 | | -The simplest way to use this module is to inhert your settings `Config` class from `AwsSsmSourceConfig`. This will overwrite the [`file_secret_settings` settings source](https://pydantic-docs.helpmanual.io/usage/settings/#customise-settings-sources) with the `AwsSsmSettingsSource`. Provide a prefix to SSM parameters via the `_secrets_dir` initialiser value or the `secrets_dir` Config value. |
| 7 | +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. |
8 | 8 |
|
9 | 9 | ```py |
10 | | -from pydantic import BaseSettings |
11 | | -from pydantic_ssm_settings import AwsSsmSourceConfig |
| 10 | +from pydantic_ssm_settings import SsmBaseSettings |
12 | 11 |
|
13 | 12 |
|
14 | | -class WebserviceSettings(BaseSettings): |
| 13 | +class WebserviceSettings(SsmBaseSettings): |
15 | 14 | some_val: str |
16 | 15 | another_val: int |
17 | 16 |
|
18 | | - class Config(AwsSsmSourceConfig): |
19 | | - ... |
| 17 | +WebserviceSettings(_ssm_prefix="/prod/webservice") |
| 18 | +``` |
| 19 | + |
| 20 | +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): |
| 21 | + |
| 22 | +```py |
| 23 | +from pydantic_ssm_settings import SsmSettingsConfigDict |
| 24 | + |
| 25 | +class WebserviceSettings(SsmBaseSettings): |
| 26 | + model_config = SsmSettingsConfigDict(ssm_prefix="/prod/webservice") |
| 27 | + some_val: str |
| 28 | + another_val: int |
20 | 29 |
|
21 | | -SimpleSettings(_secrets_dir='/prod/webservice') |
| 30 | + |
| 31 | +WebserviceSettings() |
22 | 32 | ``` |
23 | 33 |
|
24 | | -The above example will attempt to retreive values from `/prod/webservice/some_val` and `/prod/webservice/another_val` if not provided otherwise. |
| 34 | +If it is preferred to avoid altering the baseclass of a settings model, the source can be manually added and configured as such: |
| 35 | + |
| 36 | +```py |
| 37 | +from typing import Tuple, Type |
| 38 | +from pydantic_settings import ( |
| 39 | + BaseSettings, |
| 40 | + EnvSettingsSource, |
| 41 | + InitSettingsSource, |
| 42 | + PydanticBaseSettingsSource, |
| 43 | + SecretsSettingsSource, |
| 44 | +) |
| 45 | +from pydantic_ssm_settings import SsmSettingsConfigDict, SsmSettingsSource |
| 46 | + |
| 47 | +class WebserviceSettings(BaseSettings): |
| 48 | + model_config = SsmSettingsConfigDict(ssm_prefix="/asdf") |
| 49 | + foo: str |
| 50 | + |
| 51 | + def settings_customise_sources( |
| 52 | + self, |
| 53 | + settings_cls: Type[BaseSettings], |
| 54 | + init_settings: InitSettingsSource, |
| 55 | + env_settings: EnvSettingsSource, |
| 56 | + dotenv_settings: PydanticBaseSettingsSource, |
| 57 | + file_secret_settings: SecretsSettingsSource, |
| 58 | + ) -> Tuple[PydanticBaseSettingsSource, ...]: |
| 59 | + return ( |
| 60 | + init_settings, |
| 61 | + env_settings, |
| 62 | + dotenv_settings, |
| 63 | + file_secret_settings, |
| 64 | + SsmSettingsSource(settings_cls), |
| 65 | + ) |
| 66 | +``` |
0 commit comments