Skip to content

Commit

Permalink
refactor(parameters): deprecate the config parameter in favor of boto…
Browse files Browse the repository at this point in the history
…_config (#4893)

* Replacing config with boto_config

* Replacing config with boto_config
  • Loading branch information
leandrodamascena authored Aug 6, 2024
1 parent 26cfe7f commit 08f2b53
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 71 deletions.
2 changes: 1 addition & 1 deletion aws_lambda_powertools/utilities/feature_flags/appconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(
self.config = sdk_config
self.envelope = envelope
self.jmespath_options = jmespath_options
self._conf_store = AppConfigProvider(environment=environment, application=application, config=sdk_config)
self._conf_store = AppConfigProvider(environment=environment, application=application, boto_config=sdk_config)

@property
def get_raw_configuration(self) -> Dict[str, Any]:
Expand Down
17 changes: 16 additions & 1 deletion aws_lambda_powertools/utilities/parameters/appconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"""

import os
import warnings
from typing import TYPE_CHECKING, Dict, Optional, Union

import boto3
from botocore.config import Config

from aws_lambda_powertools.utilities.parameters.types import TransformOptions
from aws_lambda_powertools.warnings import PowertoolsDeprecationWarning

if TYPE_CHECKING:
from mypy_boto3_appconfigdata.client import AppConfigDataClient
Expand Down Expand Up @@ -72,15 +74,28 @@ def __init__(
environment: str,
application: Optional[str] = None,
config: Optional[Config] = None,
boto_config: Optional[Config] = None,
boto3_session: Optional[boto3.session.Session] = None,
boto3_client: Optional["AppConfigDataClient"] = None,
):
"""
Initialize the App Config client
"""

super().__init__()

if config:
warnings.warn(
message="The 'config' parameter is deprecated in V3 and will be removed in V4. "
"Please use 'boto_config' instead.",
category=PowertoolsDeprecationWarning,
stacklevel=2,
)

if boto3_client is None:
boto3_session = boto3_session or boto3.session.Session()
boto3_client = boto3_session.client("appconfigdata", config=config)
boto3_client = boto3_session.client("appconfigdata", config=boto_config or config)

self.client = boto3_client

self.application = resolve_env_var_choice(
Expand Down
15 changes: 14 additions & 1 deletion aws_lambda_powertools/utilities/parameters/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
Amazon DynamoDB parameter retrieval and caching utility
"""

import warnings
from typing import TYPE_CHECKING, Dict, Optional

import boto3
from boto3.dynamodb.conditions import Key
from botocore.config import Config

from aws_lambda_powertools.warnings import PowertoolsDeprecationWarning

from .base import BaseProvider

if TYPE_CHECKING:
Expand Down Expand Up @@ -155,15 +158,25 @@ def __init__(
value_attr: str = "value",
endpoint_url: Optional[str] = None,
config: Optional[Config] = None,
boto_config: Optional[Config] = None,
boto3_session: Optional[boto3.session.Session] = None,
boto3_client: Optional["DynamoDBServiceResource"] = None,
):
"""
Initialize the DynamoDB client
"""
if config:
warnings.warn(
message="The 'config' parameter is deprecated in V3 and will be removed in V4. "
"Please use 'boto_config' instead.",
category=PowertoolsDeprecationWarning,
stacklevel=2,
)

if boto3_client is None:
boto3_session = boto3_session or boto3.session.Session()
boto3_client = boto3_session.resource("dynamodb", config=config, endpoint_url=endpoint_url)
boto3_client = boto3_session.resource("dynamodb", config=boto_config or config, endpoint_url=endpoint_url)

self.table = boto3_client.Table(table_name)
self.key_attr = key_attr
self.sort_attr = sort_attr
Expand Down
14 changes: 13 additions & 1 deletion aws_lambda_powertools/utilities/parameters/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import json
import logging
import os
import warnings
from typing import TYPE_CHECKING, Dict, Literal, Optional, Union, overload

import boto3
from botocore.config import Config

from aws_lambda_powertools.warnings import PowertoolsDeprecationWarning

if TYPE_CHECKING:
from mypy_boto3_secretsmanager.client import SecretsManagerClient
from mypy_boto3_secretsmanager.type_defs import CreateSecretResponseTypeDef
Expand Down Expand Up @@ -78,15 +81,24 @@ class SecretsProvider(BaseProvider):
def __init__(
self,
config: Optional[Config] = None,
boto_config: Optional[Config] = None,
boto3_session: Optional[boto3.session.Session] = None,
boto3_client: Optional[SecretsManagerClient] = None,
):
"""
Initialize the Secrets Manager client
"""
if config:
warnings.warn(
message="The 'config' parameter is deprecated in V3 and will be removed in V4. "
"Please use 'boto_config' instead.",
category=PowertoolsDeprecationWarning,
stacklevel=2,
)

if boto3_client is None:
boto3_session = boto3_session or boto3.session.Session()
boto3_client = boto3_session.client("secretsmanager", config=config)
boto3_client = boto3_session.client("secretsmanager", config=boto_config or config)
self.client = boto3_client

super().__init__(client=self.client)
Expand Down
13 changes: 12 additions & 1 deletion aws_lambda_powertools/utilities/parameters/ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import logging
import os
import warnings
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, overload

import boto3
Expand All @@ -26,6 +27,7 @@
)
from aws_lambda_powertools.utilities.parameters.exceptions import GetParameterError, SetParameterError
from aws_lambda_powertools.utilities.parameters.types import TransformOptions
from aws_lambda_powertools.warnings import PowertoolsDeprecationWarning

if TYPE_CHECKING:
from mypy_boto3_ssm.client import SSMClient
Expand Down Expand Up @@ -108,15 +110,24 @@ class SSMProvider(BaseProvider):
def __init__(
self,
config: Optional[Config] = None,
boto_config: Optional[Config] = None,
boto3_session: Optional[boto3.session.Session] = None,
boto3_client: Optional[SSMClient] = None,
):
"""
Initialize the SSM Parameter Store client
"""
if config:
warnings.warn(
message="The 'config' parameter is deprecated in V3 and will be removed in V4. "
"Please use 'boto_config' instead.",
category=PowertoolsDeprecationWarning,
stacklevel=2,
)

if boto3_client is None:
boto3_session = boto3_session or boto3.session.Session()
boto3_client = boto3_session.client("ssm", config=config)
boto3_client = boto3_session.client("ssm", config=boto_config or config)
self.client = boto3_client

super().__init__(client=self.client)
Expand Down
45 changes: 45 additions & 0 deletions aws_lambda_powertools/warnings/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Shared warnings that don't belong to a single utility"""


class PowertoolsUserWarning(UserWarning):
"""
This class provides a custom Warning tailored for better clarity when certain situations occur.
Examples:
- Using development-only features in production environment.
- Potential performance or security issues due to misconfiguration.
Parameters
----------
message: str
The warning message to be displayed.
"""

def __init__(self, message):
self.message = message
super().__init__(message)

def __str__(self):
return self.message


class PowertoolsDeprecationWarning(DeprecationWarning):
"""
This class provides a DeprecationWarning custom Warning for utilities/parameters deprecated in v3.
Examples:
- Using development-only features in production environment.
- Potential performance or security issues due to misconfiguration.
Parameters
----------
message: str
The warning message to be displayed.
"""

def __init__(self, message):
self.message = message
super().__init__(message)

def __str__(self):
return self.message
2 changes: 1 addition & 1 deletion docs/utilities/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ Bringing them together in a single code snippet would look like this:

### Customizing boto configuration

The **`config`** , **`boto3_session`**, and **`boto3_client`** parameters enable you to pass in a custom [botocore config object](https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html){target="_blank"}, [boto3 session](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html){target="_blank"}, or a [boto3 client](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/boto3.html){target="_blank"} when constructing any of the built-in provider classes.
The **`boto_config`** , **`boto3_session`**, and **`boto3_client`** parameters enable you to pass in a custom [botocore config object](https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html){target="_blank"}, [boto3 session](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html){target="_blank"}, or a [boto3 client](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/boto3.html){target="_blank"} when constructing any of the built-in provider classes.

???+ tip
You can use a custom session for retrieving parameters cross-account/region and for snapshot testing.
Expand Down
2 changes: 1 addition & 1 deletion examples/parameters/src/custom_boto_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aws_lambda_powertools.utilities import parameters

boto_config = Config()
ssm_provider = parameters.SSMProvider(config=boto_config)
ssm_provider = parameters.SSMProvider(boto_config=boto_config)


def handler(event, context):
Expand Down
Loading

0 comments on commit 08f2b53

Please sign in to comment.