Skip to content

Commit 46c5357

Browse files
committed
chore(docs): Improve readme
1 parent 333ebf6 commit 46c5357

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

README.md

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,66 @@
11
# pydantic-ssm-settings
22

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).
44

55
## Usage
66

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.
88

99
```py
10-
from pydantic import BaseSettings
11-
from pydantic_ssm_settings import AwsSsmSourceConfig
10+
from pydantic_ssm_settings import SsmBaseSettings
1211

1312

14-
class WebserviceSettings(BaseSettings):
13+
class WebserviceSettings(SsmBaseSettings):
1514
some_val: str
1615
another_val: int
1716

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
2029

21-
SimpleSettings(_secrets_dir='/prod/webservice')
30+
31+
WebserviceSettings()
2232
```
2333

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

Comments
 (0)