- Notifications
You must be signed in to change notification settings - Fork8
A Python helper library for AWS AppConfig
License
aws-samples/sample-python-helper-aws-appconfig
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A sample helper Python library for AWS AppConfig which makes rolling configuration updates out easier.
- Configurable update interval: you can ask the library to update your configuration as often as needed, but it will only call the AWS AppConfig API at the configured interval (in seconds).
- Handles correct API usage: the library uses the new AppConfig Data API and handles tracking the next configuration token and poll interval for you.
- Flexible: Can automatically fetch the current configuration on initialisation, every time the configuration is read by your code, or on demand. You can override the caching interval if needed.
- Handles YAML, JSON and plain text configurations, stored in any supported AppConfig store. Any other content type is returned unprocessed as the Python
bytestype. - Supports AWS Lambda, Amazon EC2 instances and on-premises use.
pip install sample-helper-aws-appconfig
fromappconfig_helperimportAppConfigHelperfromfastapiimportFastAPIappconfig=AppConfigHelper("MyAppConfigApp","MyAppConfigEnvironment","MyAppConfigProfile",45# minimum interval between update checks)app=FastAPI()@app.get("/some-url")defindex():ifappconfig.update_config():print("New configuration received")# your configuration is available in the "config" attributereturn {"config_info":appconfig.config }
Please see theAWS AppConfig documentation for details on configuring the service.
Start by creating anAppConfigHelper object. You must specify the application name, environment name, and profile (configuration) name. You must also specify the refresh interval, in seconds. AppConfigHelper will not attempt to fetch a new configuration version from the AWS AppConfig service more frequently than this interval. You should set it low enough that your code will receive new configuration promptly, but not so low that it takes too long. The library enforces a minimum interval of 15 seconds.
The configuration is not automatically fetched unless you setfetch_on_init. To have the library fetch the configuration when it is accessed, if it has been more thanmax_config_age seconds since the last fetch, setfetch_on_read.
If you need to customise the AWS credentials or region, setsession to a configuredboto3.Session object. Otherwise, thestandard boto3 logic for credential/configuration discovery is used.
The configuration from AWS AppConfig is available as theconfig property. Before accessing it, you should callupdate_config(), unless you specified fetch_on_init or fetch_on_read during initialisation. If you want to force a config fetch, even if the number of seconds specified have not yet passed, callupdate_config(True).
update_config() returnsTrue if a new version of the configuration was received. If no attempt was made to fetch it, or the configuration received was the same as current one, it returnsFalse. It will raiseValueError if the received configuration data could not be processed (e.g. invalid JSON). If needed, the inner exception for JSON or YAML parsing is available as__context__ on the raised exception.
To read the values in your configuration, access theconfig property. For JSON and YAML configurations, this will contain the structure of your data. For plain text configurations, this will be a simple string.
The original data received from AppConfig is available in theraw_config property. Accessing this property will not trigger an automatic update even iffetch_on_read is True. The content type field received from AppConfig is available in thecontent_type property.
For example, with the following JSON in your AppConfig configuration profile:
{"hello":"world","data": {"is_sample":true }}you would see the following when using the library:
# appconfig is the instance of the library>>>appconfig.config["hello"]"world">>>appconfig.config["data"]{'is_sample':True}
AWS AppConfig is best used in Lambda by taking advantage ofLambda Extensions
SeeCONTRIBUTING for more information.
This library is licensed under Apache-2.0. See the LICENSE file.
About
A Python helper library for AWS AppConfig
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.