Skip to main content

Sample helper library for AWS AppConfig

Project description

Sample AWS AppConfig Helper

A sample helper Python library for AWS AppConfig which makes rolling configuration updates out easier.

PyPI - Python Version PyPI version Code style: black

Features

  • 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 bytes type.
  • Supports AWS Lambda, Amazon EC2 instances and on-premises use.

Installation

pip install sample-helper-aws-appconfig

Example

from appconfig_helper import AppConfigHelper
from fastapi import FastAPI

appconfig = AppConfigHelper(
    "MyAppConfigApp",
    "MyAppConfigEnvironment",
    "MyAppConfigProfile",
    45  # minimum interval between update checks
)

app = FastAPI()

@app.get("/some-url")
def index():
    if appconfig.update_config():
        print("New configuration received")
    # your configuration is available in the "config" attribute
    return {
        "config_info": appconfig.config
    }

Usage

Please see the AWS AppConfig documentation for details on configuring the service.

Initialising

Start by creating an AppConfigHelper 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 set fetch_on_init. To have the library fetch the configuration when it is accessed, if it has been more than max_config_age seconds since the last fetch, set fetch_on_read.

If you need to customise the AWS credentials or region, set session to a configured boto3.Session object. Otherwise, the standard boto3 logic for credential/configuration discovery is used.

Reading the configuration

The configuration from AWS AppConfig is available as the config property. Before accessing it, you should call update_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, call update_config(True).

update_config() returns True 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 returns False. It will raise ValueError 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 the config 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 the raw_config property. Accessing this property will not trigger an automatic update even if fetch_on_read is True. The content type field received from AppConfig is available in the content_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}

Use in AWS Lambda

AWS AppConfig is best used in Lambda by taking advantage of Lambda Extensions

Security

See CONTRIBUTING for more information.

Licence

This library is licensed under Apache-2.0. See the LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sample_helper_aws_appconfig-2.3.1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sample_helper_aws_appconfig-2.3.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file sample_helper_aws_appconfig-2.3.1.tar.gz.

File metadata

  • Download URL: sample_helper_aws_appconfig-2.3.1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.2 Darwin/24.6.0

File hashes

Hashes for sample_helper_aws_appconfig-2.3.1.tar.gz
Algorithm Hash digest
SHA256 88b06afa67cf0711e6fcdb9642488a580b4623ef564f18d982a4cf7d22b1274f
MD5 cdea0a896bb143e9de385a82f7c77244
BLAKE2b-256 7752583fef2e6e858e5e8ef384788d77ea407e714caa768f9eca045512c4359a

See more details on using hashes here.

File details

Details for the file sample_helper_aws_appconfig-2.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sample_helper_aws_appconfig-2.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6eb51345240d6ee7cc95f3938bf3f78edd0deb153cea35e0cab235dc9ae571db
MD5 a6cb3192dc5642507780ac88951bc2fe
BLAKE2b-256 8b731d3711d3e545701f207580b38057c4629300bec35088646c23d4a758707c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page