-
Notifications
You must be signed in to change notification settings - Fork 433
feat: Add AppConfig parameter provider #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
aws_lambda_powertools/utilities/parameters/appconfig.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
""" | ||
AWS App Config configuration retrieval and caching utility | ||
""" | ||
|
||
|
||
import os | ||
from typing import Dict, Optional, Union | ||
from uuid import uuid4 | ||
|
||
import boto3 | ||
from botocore.config import Config | ||
|
||
from .base import DEFAULT_PROVIDERS, BaseProvider | ||
|
||
CLIENT_ID = str(uuid4()) | ||
|
||
|
||
class AppConfigProvider(BaseProvider): | ||
""" | ||
AWS App Config Provider | ||
|
||
Parameters | ||
---------- | ||
environment: str | ||
Environment of the configuration to pass during client initialization | ||
application: str, optional | ||
Application of the configuration to pass during client initialization | ||
config: botocore.config.Config, optional | ||
Botocore configuration to pass during client initialization | ||
|
||
Example | ||
------- | ||
**Retrieves the latest configuration value from App Config** | ||
|
||
>>> from aws_lambda_powertools.utilities.parameters import AppConfigProvider | ||
>>> appconf_provider = parameters.AppConfigProvider(environment="my_env", application="my_app") | ||
>>> | ||
>>> value : bytes = appconf_provider.get("my_conf") | ||
>>> | ||
>>> print(value) | ||
My configuration value | ||
|
||
**Retrieves a configuration value from App Config in another AWS region** | ||
|
||
>>> from botocore.config import Config | ||
>>> from aws_lambda_powertools.utilities.parameters import AppConfigProvider | ||
>>> | ||
>>> config = Config(region_name="us-west-1") | ||
>>> appconf_provider = parameters.AppConfigProvider(environment="my_env", application="my_app", config=config) | ||
>>> | ||
>>> value : bytes = appconf_provider.get("my_conf") | ||
>>> | ||
>>> print(value) | ||
My configuration value | ||
|
||
""" | ||
|
||
client = None | ||
|
||
def __init__( | ||
self, environment: str, application: Optional[str] = None, config: Optional[Config] = None, | ||
): | ||
""" | ||
Initialize the App Config client | ||
""" | ||
|
||
config = config or Config() | ||
self.client = boto3.client("appconfig", config=config) | ||
self.application = application or os.getenv("POWERTOOLS_SERVICE_NAME") or "application_undefined" | ||
self.environment = environment | ||
self.current_version = "" | ||
|
||
super().__init__() | ||
|
||
def _get(self, name: str, **sdk_options) -> str: | ||
""" | ||
Retrieve a parameter value from AWS App config. | ||
|
||
Parameters | ||
---------- | ||
name: str | ||
Name of the configuration | ||
environment: str | ||
Environment of the configuration | ||
sdk_options: dict, optional | ||
Dictionary of options that will be passed to the Parameter Store get_parameter API call | ||
""" | ||
|
||
sdk_options["Configuration"] = name | ||
sdk_options["Application"] = self.application | ||
sdk_options["Environment"] = self.environment | ||
sdk_options["ClientId"] = CLIENT_ID | ||
|
||
response = self.client.get_configuration(**sdk_options) | ||
return response["Content"].read() # read() of botocore.response.StreamingBody | ||
|
||
def _get_multiple(self, path: str, **sdk_options) -> Dict[str, str]: | ||
""" | ||
Retrieving multiple parameter values is not supported with AWS App Config Provider | ||
""" | ||
raise NotImplementedError() | ||
|
||
|
||
def get_app_config( | ||
name: str, environment: str, application: Optional[str] = None, transform: Optional[str] = None, **sdk_options | ||
) -> Union[str, list, dict, bytes]: | ||
""" | ||
Retrieve a configuration value from AWS App Config. | ||
|
||
Parameters | ||
---------- | ||
name: str | ||
Name of the configuration | ||
environment: str | ||
Environment of the configuration | ||
application: str | ||
Application of the configuration | ||
transform: str, optional | ||
Transforms the content from a JSON object ('json') or base64 binary string ('binary') | ||
sdk_options: dict, optional | ||
Dictionary of options that will be passed to the Parameter Store get_parameter API call | ||
|
||
Raises | ||
------ | ||
GetParameterError | ||
When the parameter provider fails to retrieve a parameter value for | ||
a given name. | ||
TransformParameterError | ||
When the parameter provider fails to transform a parameter value. | ||
|
||
Example | ||
------- | ||
**Retrieves the latest version of configuration value from App Config** | ||
|
||
>>> from aws_lambda_powertools.utilities.parameters import get_app_config | ||
>>> | ||
>>> value = get_app_config("my_config", environment="my_env", application="my_env") | ||
>>> | ||
>>> print(value) | ||
My configuration value | ||
|
||
**Retrieves a confiugration value and decodes it using a JSON decoder** | ||
|
||
>>> from aws_lambda_powertools.utilities.parameters import get_parameter | ||
>>> | ||
>>> value = get_app_config("my_config", environment="my_env", application="my_env", transform='json') | ||
>>> | ||
>>> print(value) | ||
My configuration's JSON value | ||
""" | ||
|
||
# Only create the provider if this function is called at least once | ||
if "appconfig" not in DEFAULT_PROVIDERS: | ||
DEFAULT_PROVIDERS["appconfig"] = AppConfigProvider(environment=environment, application=application) | ||
|
||
sdk_options["ClientId"] = CLIENT_ID | ||
|
||
return DEFAULT_PROVIDERS["appconfig"].get(name, transform=transform, **sdk_options) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.