|
| 1 | +""" |
| 2 | +AWS App Config configuration retrieval and caching utility |
| 3 | +""" |
| 4 | + |
| 5 | + |
| 6 | +import os |
| 7 | +from typing import Dict, Optional, Union |
| 8 | +from uuid import uuid4 |
| 9 | + |
| 10 | +import boto3 |
| 11 | +from botocore.config import Config |
| 12 | + |
| 13 | +from .base import DEFAULT_PROVIDERS, BaseProvider |
| 14 | + |
| 15 | +CLIENT_ID = str(uuid4()) |
| 16 | + |
| 17 | + |
| 18 | +class AppConfigProvider(BaseProvider): |
| 19 | + """ |
| 20 | + AWS App Config Provider |
| 21 | +
|
| 22 | + Parameters |
| 23 | + ---------- |
| 24 | + environment: str |
| 25 | + Environment of the configuration to pass during client initialization |
| 26 | + application: str, optional |
| 27 | + Application of the configuration to pass during client initialization |
| 28 | + config: botocore.config.Config, optional |
| 29 | + Botocore configuration to pass during client initialization |
| 30 | +
|
| 31 | + Example |
| 32 | + ------- |
| 33 | + **Retrieves a configuration value from App Config** |
| 34 | +
|
| 35 | + >>> from aws_lambda_powertools.utilities.parameters import AppConfigProvider |
| 36 | + >>> appconf_provider = parameters.AppConfigProvider(environment="my_env", application="my_app") |
| 37 | + >>> |
| 38 | + >>> value : bytes = appconf_provider.get("my_conf") |
| 39 | + >>> |
| 40 | + >>> print(value) |
| 41 | + My configuration value |
| 42 | +
|
| 43 | + **Retrieves a configuration value from App Config in another AWS region** |
| 44 | +
|
| 45 | + >>> from botocore.config import Config |
| 46 | + >>> from aws_lambda_powertools.utilities.parameters import AppConfigProvider |
| 47 | + >>> |
| 48 | + >>> config = Config(region_name="us-west-1") |
| 49 | + >>> appconf_provider = parameters.AppConfigProvider(environment="my_env", application="my_app", config=config) |
| 50 | + >>> |
| 51 | + >>> value : bytes = appconf_provider.get("my_conf") |
| 52 | + >>> |
| 53 | + >>> print(value) |
| 54 | + My configuration value |
| 55 | +
|
| 56 | + **Retrieves a specific version number of configuration value from App Config** |
| 57 | +
|
| 58 | + >>> from aws_lambda_powertools.utilities.parameters import AppConfigProvider |
| 59 | + >>> appconf_provider = parameters.AppConfigProvider(environment="my_env", application="my_app") |
| 60 | + >>> |
| 61 | + >>> value = appconf_provider.get("my_config", environment="my_env", application="env", client_conf_version="1") |
| 62 | + >>> |
| 63 | + >>> print(value) |
| 64 | + My configuration value |
| 65 | + """ |
| 66 | + |
| 67 | + client = None |
| 68 | + |
| 69 | + def __init__( |
| 70 | + self, environment: str, application: Optional[str] = None, config: Optional[Config] = None, |
| 71 | + ): |
| 72 | + """ |
| 73 | + Initialize the App Config client |
| 74 | + """ |
| 75 | + |
| 76 | + config = config or Config() |
| 77 | + self.client = boto3.client("appconfig", config=config) |
| 78 | + self.application = application or os.getenv("POWERTOOLS_SERVICE_NAME") or "application_undefined" |
| 79 | + self.environment = environment |
| 80 | + |
| 81 | + super().__init__() |
| 82 | + |
| 83 | + def _get(self, name: str, client_conf_version: Optional[str] = None, **sdk_options) -> str: |
| 84 | + """ |
| 85 | + Retrieve a parameter value from AWS App config. |
| 86 | +
|
| 87 | + Parameters |
| 88 | + ---------- |
| 89 | + name: str |
| 90 | + Name of the configuration |
| 91 | + environment: str |
| 92 | + Environment of the configuration |
| 93 | + client_conf_version: str, optional |
| 94 | + Client configuration vrsion to get, otherise gets the latest version |
| 95 | + sdk_options: dict, optional |
| 96 | + Dictionary of options that will be passed to the Parameter Store get_parameter API call |
| 97 | + """ |
| 98 | + |
| 99 | + sdk_options["Configuration"] = name |
| 100 | + sdk_options["Application"] = self.application |
| 101 | + sdk_options["Environment"] = self.environment |
| 102 | + sdk_options["ClientId"] = CLIENT_ID |
| 103 | + if client_conf_version is not None: |
| 104 | + sdk_options["ClientConfigurationVersion"] = client_conf_version |
| 105 | + |
| 106 | + response = self.client.get_configuration(**sdk_options) |
| 107 | + return response["Content"].read() # read() of botocore.response.StreamingBody |
| 108 | + |
| 109 | + def _get_multiple(self, path: str, **sdk_options) -> Dict[str, str]: |
| 110 | + """ |
| 111 | + Retrieving multiple parameter values is not supported with AWS App Config Provider |
| 112 | + """ |
| 113 | + raise NotImplementedError() |
| 114 | + |
| 115 | + |
| 116 | +def get_app_config( |
| 117 | + name: str, |
| 118 | + environment: str, |
| 119 | + application: Optional[str] = None, |
| 120 | + transform: Optional[str] = None, |
| 121 | + client_conf_version: Optional[str] = None, |
| 122 | + **sdk_options |
| 123 | +) -> Union[str, list, dict, bytes]: |
| 124 | + """ |
| 125 | + Retrieve a configuration value from AWS App Config. |
| 126 | +
|
| 127 | + Parameters |
| 128 | + ---------- |
| 129 | + name: str |
| 130 | + Name of the configuration |
| 131 | + environment: str |
| 132 | + Environment of the configuration |
| 133 | + application: str |
| 134 | + Application of the configuration |
| 135 | + transform: str, optional |
| 136 | + Transforms the content from a JSON object ('json') or base64 binary string ('binary') |
| 137 | + client_conf_version: str, optional |
| 138 | + Client configuration vrsion to get, otherise gets the latest version |
| 139 | + sdk_options: dict, optional |
| 140 | + Dictionary of options that will be passed to the Parameter Store get_parameter API call |
| 141 | +
|
| 142 | + Raises |
| 143 | + ------ |
| 144 | + GetParameterError |
| 145 | + When the parameter provider fails to retrieve a parameter value for |
| 146 | + a given name. |
| 147 | + TransformParameterError |
| 148 | + When the parameter provider fails to transform a parameter value. |
| 149 | +
|
| 150 | + Example |
| 151 | + ------- |
| 152 | + **Retrieves the latest version of configuration value from App Config** |
| 153 | +
|
| 154 | + >>> from aws_lambda_powertools.utilities.parameters import get_app_config |
| 155 | + >>> |
| 156 | + >>> value = get_app_config("my_config", environment="my_env", application="my_env") |
| 157 | + >>> |
| 158 | + >>> print(value) |
| 159 | + My configuration value |
| 160 | +
|
| 161 | + **Retrieves a specific version number of configuration value from App Config** |
| 162 | +
|
| 163 | + >>> from aws_lambda_powertools.utilities.parameters import get_app_config |
| 164 | + >>> |
| 165 | + >>> value = get_app_config("my_config", environment="my_env", application="my_env", client_conf_version="1") |
| 166 | + >>> |
| 167 | + >>> print(value) |
| 168 | + My configuration value |
| 169 | +
|
| 170 | + **Retrieves a confiugration value and decodes it using a JSON decoder** |
| 171 | +
|
| 172 | + >>> from aws_lambda_powertools.utilities.parameters import get_parameter |
| 173 | + >>> |
| 174 | + >>> value = get_app_config("my_config", environment="my_env", application="my_env", transform='json') |
| 175 | + >>> |
| 176 | + >>> print(value) |
| 177 | + My configuration's JSON value |
| 178 | + """ |
| 179 | + |
| 180 | + # Only create the provider if this function is called at least once |
| 181 | + if "appconfig" not in DEFAULT_PROVIDERS: |
| 182 | + DEFAULT_PROVIDERS["appconfig"] = AppConfigProvider(environment=environment, application=application) |
| 183 | + |
| 184 | + sdk_options["ClientId"] = CLIENT_ID |
| 185 | + if client_conf_version is not None: |
| 186 | + sdk_options["ClientConfigurationVersion"] = client_conf_version |
| 187 | + |
| 188 | + return DEFAULT_PROVIDERS["appconfig"].get(name, transform=transform, **sdk_options) |
0 commit comments