diff --git a/aws_lambda_powertools/utilities/idempotency/idempotency.py b/aws_lambda_powertools/utilities/idempotency/idempotency.py index dc05a30a239..aebac5f2410 100644 --- a/aws_lambda_powertools/utilities/idempotency/idempotency.py +++ b/aws_lambda_powertools/utilities/idempotency/idempotency.py @@ -6,7 +6,6 @@ import functools import logging -import os import sys import warnings from inspect import isclass, unwrap @@ -14,7 +13,7 @@ from aws_lambda_powertools.middleware_factory import lambda_handler_decorator from aws_lambda_powertools.shared import constants -from aws_lambda_powertools.shared.functions import strtobool +from aws_lambda_powertools.shared.functions import resolve_bool_env_var from aws_lambda_powertools.shared.types import AnyCallableT from aws_lambda_powertools.utilities.idempotency.base import IdempotencyHandler from aws_lambda_powertools.utilities.idempotency.config import IdempotencyConfig @@ -107,7 +106,7 @@ def handler(event, context): # Skip idempotency controls when POWERTOOLS_IDEMPOTENCY_DISABLED has a truthy value # Raises a warning if not running in development mode - if strtobool(os.getenv(constants.IDEMPOTENCY_DISABLED_ENV, "false")): + if resolve_bool_env_var(constants.IDEMPOTENCY_DISABLED_ENV): warnings.warn( message="Disabling idempotency is intended for development environments only " "and should not be used in production.", @@ -245,7 +244,7 @@ def process_order(customer_id: str, order: dict, **kwargs): def decorate(*args, **kwargs): # Skip idempotency controls when POWERTOOLS_IDEMPOTENCY_DISABLED has a truthy value # Raises a warning if not running in development mode - if strtobool(os.getenv(constants.IDEMPOTENCY_DISABLED_ENV, "false")): + if resolve_bool_env_var(constants.IDEMPOTENCY_DISABLED_ENV): warnings.warn( message="Disabling idempotency is intended for development environments only " "and should not be used in production.", diff --git a/tests/functional/idempotency/_boto3/test_idempotency.py b/tests/functional/idempotency/_boto3/test_idempotency.py index 790c4c67b2c..4e07a5f87d0 100644 --- a/tests/functional/idempotency/_boto3/test_idempotency.py +++ b/tests/functional/idempotency/_boto3/test_idempotency.py @@ -1892,6 +1892,44 @@ def dummy_handler(event, context): assert len(persistence_store.client.method_calls) == 4 +@pytest.mark.parametrize("idempotency_disabled_value", ["", " ", "maybe", None]) +def test_idempotency_disabled_envvar_empty_or_unparsable( + monkeypatch, + lambda_context, + persistence_store: DynamoDBPersistenceLayer, + idempotency_disabled_value, +): + # Scenario to validate an empty, unset or unparsable 'POWERTOOLS_IDEMPOTENCY_DISABLED' + # keeps idempotency enabled instead of crashing the invocation + mock_event = {"data": "value"} + + persistence_store.client = MagicMock() + + if idempotency_disabled_value is None: + monkeypatch.delenv("POWERTOOLS_IDEMPOTENCY_DISABLED", raising=False) + else: + monkeypatch.setenv("POWERTOOLS_IDEMPOTENCY_DISABLED", idempotency_disabled_value) + + @idempotent_function(data_keyword_argument="data", persistence_store=persistence_store) + def dummy(data): + return {"message": "hello"} + + @idempotent(persistence_store=persistence_store) + def dummy_handler(event, context): + return {"message": "hi"} + + if idempotency_disabled_value is None: + assert dummy(data=mock_event) == {"message": "hello"} + assert dummy_handler(mock_event, lambda_context) == {"message": "hi"} + else: + with pytest.warns(PowertoolsUserWarning, match="unrecognised value"): + assert dummy(data=mock_event) == {"message": "hello"} + with pytest.warns(PowertoolsUserWarning, match="unrecognised value"): + assert dummy_handler(mock_event, lambda_context) == {"message": "hi"} + + assert len(persistence_store.client.method_calls) == 4 + + @pytest.mark.parametrize("idempotency_config", [{"use_local_cache": True}], indirect=True) def test_idempotent_function_duplicates( idempotency_config: IdempotencyConfig,