Skip to content

Commit 74f65f9

Browse files
feat(parameters) - addressing Ruben's feedback
1 parent 2a6c90d commit 74f65f9

File tree

8 files changed

+46
-47
lines changed

8 files changed

+46
-47
lines changed

aws_lambda_powertools/shared/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
IDEMPOTENCY_DISABLED_ENV: str = "POWERTOOLS_IDEMPOTENCY_DISABLED"
2424

25-
PARAMETERS_DEFAULT_DECRYPT: str = "POWERTOOLS_PARAMETERS_SSM_DECRYPT"
26-
PARAMETERS_MAX_AGE: str = "POWERTOOLS_PARAMETERS_MAX_AGE"
25+
PARAMETERS_SSM_DECRYPT_ENV: str = "POWERTOOLS_PARAMETERS_SSM_DECRYPT"
26+
PARAMETERS_MAX_AGE_ENV: str = "POWERTOOLS_PARAMETERS_MAX_AGE"
2727

2828
LOGGER_LAMBDA_CONTEXT_KEYS = [
2929
"function_arn",

aws_lambda_powertools/utilities/parameters/appconfig.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def get_app_config(
158158
Transforms the content from a JSON object ('json') or base64 binary string ('binary')
159159
force_fetch: bool, optional
160160
Force update even before a cached item has expired, defaults to False
161-
max_age: int
161+
max_age: int, optional
162162
Maximum age of the cached value
163163
sdk_options: dict, optional
164164
SDK options to propagate to `start_configuration_session` API call
@@ -191,8 +191,8 @@ def get_app_config(
191191
>>> print(value)
192192
My configuration's JSON value
193193
"""
194-
# Resolving if will use the default value (5), the value passed by parameter or the environment variable
195-
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE, DEFAULT_MAX_AGE_SECS), choice=max_age)
194+
# If max_age is not set, resolve it from the environment variable, defaulting to DEFAULT_MAX_AGE_SECS
195+
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=max_age)
196196

197197
# Only create the provider if this function is called at least once
198198
if "appconfig" not in DEFAULT_PROVIDERS:

aws_lambda_powertools/utilities/parameters/base.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
from mypy_boto3_ssm import SSMClient
3939

4040

41-
# If the environment variable is not set, the default value is 5
4241
DEFAULT_MAX_AGE_SECS = "5"
4342

4443
# These providers will be dynamically initialized on first use of the helper functions
@@ -126,8 +125,8 @@ def get(
126125
value: Optional[Union[str, bytes, dict]] = None
127126
key = (name, transform)
128127

129-
# Resolving if will use the default value (5), the value passed by parameter or the environment variable
130-
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE, DEFAULT_MAX_AGE_SECS), choice=max_age)
128+
# If max_age is not set, resolve it from the environment variable, defaulting to DEFAULT_MAX_AGE_SECS
129+
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=max_age)
131130

132131
if not force_fetch and self.has_not_expired_in_cache(key):
133132
return self.store[key].value
@@ -194,8 +193,8 @@ def get_multiple(
194193
"""
195194
key = (path, transform)
196195

197-
# Resolving if will use the default value (5), the value passed by parameter or the environment variable
198-
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE, DEFAULT_MAX_AGE_SECS), choice=max_age)
196+
# If max_age is not set, resolve it from the environment variable, defaulting to DEFAULT_MAX_AGE_SECS
197+
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=max_age)
199198

200199
if not force_fetch and self.has_not_expired_in_cache(key):
201200
return self.store[key].value # type: ignore # need to revisit entire typing here

aws_lambda_powertools/utilities/parameters/secrets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def get_secret(
128128
Transforms the content from a JSON object ('json') or base64 binary string ('binary')
129129
force_fetch: bool, optional
130130
Force update even before a cached item has expired, defaults to False
131-
max_age: int
131+
max_age: int, optional
132132
Maximum age of the cached value
133133
sdk_options: dict, optional
134134
Dictionary of options that will be passed to the get_secret_value call
@@ -162,8 +162,8 @@ def get_secret(
162162
>>> get_secret("my-secret", VersionId="f658cac0-98a5-41d9-b993-8a76a7799194")
163163
"""
164164

165-
# Resolving if will use the default value (5), the value passed by parameter or the environment variable
166-
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE, DEFAULT_MAX_AGE_SECS), choice=max_age)
165+
# If max_age is not set, resolve it from the environment variable, defaulting to DEFAULT_MAX_AGE_SECS
166+
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=max_age)
167167

168168
# Only create the provider if this function is called at least once
169169
if "secrets" not in DEFAULT_PROVIDERS:

aws_lambda_powertools/utilities/parameters/ssm.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def get( # type: ignore[override]
129129
----------
130130
name: str
131131
Parameter name
132-
max_age: int
132+
max_age: int, optional
133133
Maximum age of the cached value
134134
transform: str
135135
Optional transformation of the parameter value. Supported values
@@ -151,12 +151,12 @@ def get( # type: ignore[override]
151151
When the parameter provider fails to transform a parameter value.
152152
"""
153153

154-
# Resolving if will use the default value (5), the value passed by parameter or the environment variable
155-
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE, DEFAULT_MAX_AGE_SECS), choice=max_age)
154+
# If max_age is not set, resolve it from the environment variable, defaulting to DEFAULT_MAX_AGE_SECS
155+
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=max_age)
156156

157-
# Resolving if will use the default value (False), the value passed by parameter or the environment variable
157+
# If decrypt is not set, resolve it from the environment variable, defaulting to False
158158
decrypt = resolve_truthy_env_var_choice(
159-
env=os.getenv(constants.PARAMETERS_DEFAULT_DECRYPT, "false"), choice=decrypt
159+
env=os.getenv(constants.PARAMETERS_SSM_DECRYPT_ENV, "false"), choice=decrypt
160160
)
161161

162162
# Add to `decrypt` sdk_options to we can have an explicit option for this
@@ -261,7 +261,7 @@ def get_parameters_by_name(
261261
Transforms the content from a JSON object ('json') or base64 binary string ('binary')
262262
decrypt: bool, optional
263263
If the parameter values should be decrypted
264-
max_age: int
264+
max_age: int, optional
265265
Maximum age of the cached value
266266
raise_on_error: bool
267267
Whether to fail-fast or fail gracefully by including "_errors" key in the response, by default True
@@ -274,12 +274,12 @@ def get_parameters_by_name(
274274
When "_errors" reserved key is in parameters to be fetched from SSM.
275275
"""
276276

277-
# Resolving if will use the default value (5), the value passed by parameter or the environment variable
278-
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE, DEFAULT_MAX_AGE_SECS), choice=max_age)
277+
# If max_age is not set, resolve it from the environment variable, defaulting to DEFAULT_MAX_AGE_SECS
278+
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=max_age)
279279

280-
# Resolving if will use the default value (False), the value passed by parameter or the environment variable
280+
# If decrypt is not set, resolve it from the environment variable, defaulting to False
281281
decrypt = resolve_truthy_env_var_choice(
282-
env=os.getenv(constants.PARAMETERS_DEFAULT_DECRYPT, "false"), choice=decrypt
282+
env=os.getenv(constants.PARAMETERS_SSM_DECRYPT_ENV, "false"), choice=decrypt
283283
)
284284

285285
# Init potential batch/decrypt batch responses and errors
@@ -528,7 +528,7 @@ def get_parameter(
528528
If the parameter values should be decrypted
529529
force_fetch: bool, optional
530530
Force update even before a cached item has expired, defaults to False
531-
max_age: int
531+
max_age: int, optional
532532
Maximum age of the cached value
533533
sdk_options: dict, optional
534534
Dictionary of options that will be passed to the Parameter Store get_parameter API call
@@ -566,12 +566,12 @@ def get_parameter(
566566
if "ssm" not in DEFAULT_PROVIDERS:
567567
DEFAULT_PROVIDERS["ssm"] = SSMProvider()
568568

569-
# Resolving if will use the default value (5), the value passed by parameter or the environment variable
570-
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE, DEFAULT_MAX_AGE_SECS), choice=max_age)
569+
# If max_age is not set, resolve it from the environment variable, defaulting to DEFAULT_MAX_AGE_SECS
570+
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=max_age)
571571

572-
# Resolving if will use the default value (False), the value passed by parameter or the environment variable
572+
# If decrypt is not set, resolve it from the environment variable, defaulting to False
573573
decrypt = resolve_truthy_env_var_choice(
574-
env=os.getenv(constants.PARAMETERS_DEFAULT_DECRYPT, "false"), choice=decrypt
574+
env=os.getenv(constants.PARAMETERS_SSM_DECRYPT_ENV, "false"), choice=decrypt
575575
)
576576

577577
# Add to `decrypt` sdk_options to we can have an explicit option for this
@@ -607,7 +607,7 @@ def get_parameters(
607607
If the parameter values should be decrypted
608608
force_fetch: bool, optional
609609
Force update even before a cached item has expired, defaults to False
610-
max_age: int
610+
max_age: int, optional
611611
Maximum age of the cached value
612612
raise_on_transform_error: bool, optional
613613
Raises an exception if any transform fails, otherwise this will
@@ -648,12 +648,12 @@ def get_parameters(
648648
if "ssm" not in DEFAULT_PROVIDERS:
649649
DEFAULT_PROVIDERS["ssm"] = SSMProvider()
650650

651-
# Resolving if will use the default value (5), the value passed by parameter or the environment variable
652-
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE, DEFAULT_MAX_AGE_SECS), choice=max_age)
651+
# If max_age is not set, resolve it from the environment variable, defaulting to DEFAULT_MAX_AGE_SECS
652+
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=max_age)
653653

654-
# Resolving if will use the default value (False), the value passed by parameter or the environment variable
654+
# If decrypt is not set, resolve it from the environment variable, defaulting to False
655655
decrypt = resolve_truthy_env_var_choice(
656-
env=os.getenv(constants.PARAMETERS_DEFAULT_DECRYPT, "false"), choice=decrypt
656+
env=os.getenv(constants.PARAMETERS_SSM_DECRYPT_ENV, "false"), choice=decrypt
657657
)
658658

659659
sdk_options["recursive"] = recursive
@@ -731,7 +731,7 @@ def get_parameters_by_name(
731731
Transforms the content from a JSON object ('json') or base64 binary string ('binary')
732732
decrypt: bool, optional
733733
If the parameter values should be decrypted
734-
max_age: int
734+
max_age: int, optional
735735
Maximum age of the cached value
736736
raise_on_error: bool, optional
737737
Whether to fail-fast or fail gracefully by including "_errors" key in the response, by default True
@@ -771,12 +771,12 @@ def get_parameters_by_name(
771771
# NOTE: Decided against using multi-thread due to single-thread outperforming in 128M and 1G + timeout risk
772772
# see: https://github.com/awslabs/aws-lambda-powertools-python/issues/1040#issuecomment-1299954613
773773

774-
# Resolving if will use the default value (5), the value passed by parameter or the environment variable
775-
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE, DEFAULT_MAX_AGE_SECS), choice=max_age)
774+
# If max_age is not set, resolve it from the environment variable, defaulting to DEFAULT_MAX_AGE_SECS
775+
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=max_age)
776776

777-
# Resolving if will use the default value (False), the value passed by parameter or the environment variable
777+
# If decrypt is not set, resolve it from the environment variable, defaulting to False
778778
decrypt = resolve_truthy_env_var_choice(
779-
env=os.getenv(constants.PARAMETERS_DEFAULT_DECRYPT, "false"), choice=decrypt
779+
env=os.getenv(constants.PARAMETERS_SSM_DECRYPT_ENV, "false"), choice=decrypt
780780
)
781781

782782
# Only create the provider if this function is called at least once

docs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,8 @@ Core utilities such as Tracing, Logging, Metrics, and Event Handler will be avai
706706
| **POWERTOOLS_LOGGER_LOG_EVENT** | Logs incoming event | [Logging](./core/logger) | `false` |
707707
| **POWERTOOLS_LOGGER_SAMPLE_RATE** | Debug log sampling | [Logging](./core/logger) | `0` |
708708
| **POWERTOOLS_LOG_DEDUPLICATION_DISABLED** | Disables log deduplication filter protection to use Pytest Live Log feature | [Logging](./core/logger) | `false` |
709-
| **POWERTOOLS_PARAMETERS_MAX_AGE** | Adjust how long values are kept in cache (in seconds) | [Parameters](.//utilities/parameters/#adjusting-cache-ttl) | `5` |
710-
| **POWERTOOLS_PARAMETERS_SSM_DECRYPT** | Sets whether to decrypt or not values retrieved from AWS SSM Parameters Store | [Parameters](.//utilities/parameters/#ssmprovider) | `false` |
709+
| **POWERTOOLS_PARAMETERS_MAX_AGE** | Adjust how long values are kept in cache (in seconds) | [Parameters](./utilities/parameters/#adjusting-cache-ttl) | `5` |
710+
| **POWERTOOLS_PARAMETERS_SSM_DECRYPT** | Sets whether to decrypt or not values retrieved from AWS SSM Parameters Store | [Parameters](./utilities/parameters/#ssmprovider) | `false` |
711711
| **POWERTOOLS_DEV** | Increases verbosity across utilities | Multiple; see [POWERTOOLS_DEV effect below](#increasing-verbosity-across-utilities) | `false` |
712712
| **LOG_LEVEL** | Sets logging level | [Logging](./core/logger) | `INFO` |
713713

docs/utilities/parameters.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ The following will retrieve the latest version and store it in the cache.
103103
???+ tip
104104
`max_age` parameter is also available in underlying provider functions like `get()`, `get_multiple()`, etc.
105105

106-
By default, we cache parameters retrieved in-memory for 5 seconds. If you want to change this default value and set the same TTL for all parameters, you can set the `POWERTOOLS_PARAMETERS_MAX_AGE` environment variable. **This will override the default TTL of 5 seconds but can be overridden by the `maxAge` parameter**.
106+
By default, we cache parameters retrieved in-memory for 5 seconds. If you want to change this default value and set the same TTL for all parameters, you can set the `POWERTOOLS_PARAMETERS_MAX_AGE` environment variable. **You can still set `max_age` for individual parameters**.
107107

108108
You can adjust how long we should keep values in cache by using the param `max_age`, when using `get_parameter()`, `get_parameters()` and `get_secret()` methods across all providers.
109109

@@ -180,7 +180,7 @@ The AWS Systems Manager Parameter Store provider supports two additional argumen
180180
You can create `SecureString` parameters, which are parameters that have a plaintext parameter name and an encrypted parameter value. If you don't use the `decrypt` argument, you will get an encrypted value. Read [here](https://docs.aws.amazon.com/kms/latest/developerguide/services-parameter-store.html) about best practices using KMS to secure your parameters.
181181

182182
???+ tip
183-
If you want to always decrypt parameters, you can set the `POWERTOOLS_PARAMETERS_SSM_DECRYPT=true` environment variable. **This will override the default value of `false` but can be overridden by the `decrypt` parameter**.
183+
If you want to always decrypt parameters, you can set the `POWERTOOLS_PARAMETERS_SSM_DECRYPT=true` environment variable. **This will override the default value of `false` but you can still set the `decrypt` option for individual parameters**.
184184

185185
=== "builtin_provider_ssm_with_decrypt.py"
186186
```python hl_lines="6 10 16"

tests/unit/test_shared_functions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ def test_extract_event_any(data):
110110

111111
def test_resolve_max_age_explicit_wins_over_env_var(monkeypatch: pytest.MonkeyPatch):
112112
# GIVEN POWERTOOLS_PARAMETERS_MAX_AGE environment variable is set
113-
monkeypatch.setenv(constants.PARAMETERS_MAX_AGE, "20")
113+
monkeypatch.setenv(constants.PARAMETERS_MAX_AGE_ENV, "20")
114114

115115
# WHEN the choice is set explicitly
116-
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE, DEFAULT_MAX_AGE_SECS), choice=10)
116+
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=10)
117117

118118
# THEN the result must be the choice
119119
assert max_age == 10
@@ -123,18 +123,18 @@ def test_resolve_max_age_with_default_value():
123123
# GIVEN POWERTOOLS_PARAMETERS_MAX_AGE is not set
124124

125125
# WHEN the choice is set to None
126-
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE, DEFAULT_MAX_AGE_SECS), choice=None)
126+
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=None)
127127

128128
# THEN the result must be the default value (DEFAULT_MAX_AGE_SECS)
129129
assert max_age == int(DEFAULT_MAX_AGE_SECS)
130130

131131

132132
def test_resolve_max_age_env_var_wins_over_default_value(monkeypatch: pytest.MonkeyPatch):
133133
# GIVEN POWERTOOLS_PARAMETERS_MAX_AGE environment variable is set
134-
monkeypatch.setenv(constants.PARAMETERS_MAX_AGE, "20")
134+
monkeypatch.setenv(constants.PARAMETERS_MAX_AGE_ENV, "20")
135135

136136
# WHEN the choice is set to None
137-
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE, DEFAULT_MAX_AGE_SECS), choice=None)
137+
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=None)
138138

139139
# THEN the result must be the environment variable value
140140
assert max_age == 20

0 commit comments

Comments
 (0)