|
1 | 1 | # figgy.python.lib |
2 | 2 | Contains a public python library that may be used be Figgy users to simply application config management. |
3 | 3 |
|
4 | | -Start using figgy today: https://www.figgy.dev/ |
| 4 | +Start using figgy today: https://www.figgy.dev |
5 | 5 |
|
6 | | -Check out the detailed docs: https://www.figgy.dev/docs/ |
| 6 | +Check out the detailed docs: https://www.figgy.dev/docs/ |
| 7 | + |
| 8 | +Demonstration & Walkthrough of how to use this library: https://github.com/mancej/figgy.python-reference |
| 9 | + |
| 10 | +## Using figgy.lib |
| 11 | + |
| 12 | +This library greatly simplifies the process of fetching / using configurations from ParameterStore. It is designed |
| 13 | +to be used alongside the larger figgy ecosystem and will help you. For more details on why Figgy offers check out |
| 14 | +the [Figgy Website.](https://www.figgy.dev) |
| 15 | + |
| 16 | + |
| 17 | +To the this lib to import parameters from parameter store: |
| 18 | + |
| 19 | +### Define your configurations |
| 20 | + |
| 21 | +```python |
| 22 | + |
| 23 | +# config.py |
| 24 | +from figgy.figs import * |
| 25 | +from figgy.fig_store import FigStore |
| 26 | + |
| 27 | +# All PS configurations are defined in our FigStore |
| 28 | +class Figs(FigStore): |
| 29 | + # Twig = Application's namespace |
| 30 | + TWIG: str = "/app/foo-service" |
| 31 | + |
| 32 | + # Custom Figs specific to my application (app figs) |
| 33 | + SECRET_ADMIRER = AppFig("secret-admirer") # Expected to be found at /app/foo-service/secret-admirer |
| 34 | + ADMIRED_PERSON = AppFig("admired-person") # Expected to be found at /app/foo-service/admired-person |
| 35 | + SQL_DB_NAME = AppFig("db-name", default="SecretAdmirerDB") |
| 36 | + |
| 37 | + # Figs shared by secret owners (shared figs) |
| 38 | + SQL_USER = SharedFig("replicated/sql/user") # Expected to be found at /app/foo-service/replicated/sql/user |
| 39 | + SQL_PASSWORD = SharedFig("replicated/sql/password") |
| 40 | + |
| 41 | + # Global figs used by many services that we need to use (replicated figs) |
| 42 | + SQL_HOSTNAME = ReplicatedFig(source="/shared/resources/dbs/fig-db/dns", name="replicated/sql/hostname") |
| 43 | + SQL_PORT = ReplicatedFig(source="/shared/resources/dbs/fig-db/port", name="replicated/sql/port") |
| 44 | + |
| 45 | + # Merged Connection URL (merged figs) |
| 46 | + SQL_CONNECTION_STRING = MergeFig( |
| 47 | + name="replicated/sql-connection", |
| 48 | + pattern=["mysql://", SQL_USER, ":", SQL_PASSWORD, "@", SQL_HOSTNAME, ":", SQL_PORT, "/", SQL_DB_NAME] |
| 49 | + ) |
| 50 | +``` |
| 51 | + |
| 52 | + |
| 53 | +### Import the library |
| 54 | + |
| 55 | +```python |
| 56 | + |
| 57 | +# main.py |
| 58 | + |
| 59 | +import boto3 |
| 60 | +from figgy import FigService, ConfigWriter |
| 61 | +from config import Figs |
| 62 | + |
| 63 | +ssm = boto3.client('ssm', region_name='us-east-1') |
| 64 | +svc = FigService(ssm) |
| 65 | +FIGS = Figs(svc, lazy_load=False) # Lazy-load will fetch figs from PS as needed instead of at application bootstrap |
| 66 | + |
| 67 | +# Optional, but recommended. Have your `figgy.json` written to your run-directory. This will keep your application's |
| 68 | +# used configs in sync with remote configurations. |
| 69 | +ConfigWriter().write(FIGS) |
| 70 | + |
| 71 | +## Use a configuration |
| 72 | +print(f"Hello {FIGS.ADMIRED_PERSON}, {FIGS.SECRET_ADMIRER} is admiring you!") |
| 73 | + |
| 74 | +print(f"Found Merged SQL Connection String: {FIGS.SQL_CONNECTION_STRING}") |
| 75 | + |
| 76 | +``` |
| 77 | + |
| 78 | +If you run your APP and receive an error indicating a paramter is missing, run `figgy config sync --config figgy.json` where |
| 79 | +the `figgy.json` |
| 80 | + |
| 81 | +### Override with ENV variables |
| 82 | +Any and all configurations can be overridden locally through ENV variables. |
| 83 | + |
| 84 | +Suppose your: |
| 85 | +TWIG = '/app/foo' |
| 86 | +FIG1 = 'fig1' |
| 87 | +DB_PASS = 'replicated/db/password' |
| 88 | + |
| 89 | +To override these values with the follow ENV variables. The `TWIG` (namespace) should be left out of the fig name: |
| 90 | + |
| 91 | +```console |
| 92 | + export FIG1=foobar |
| 93 | + export REPLICATED_DB_PASSWORD=p@ssw0rd!! |
| 94 | +``` |
| 95 | + |
| 96 | +These will automatically take precedent during configuration resolution. ParameterStore will never be queried for these parameters. |
0 commit comments