Skip to content

Commit 696caa3

Browse files
committed
docs: use env var for DDB table, no hardcode
1 parent 4592e72 commit 696caa3

23 files changed

+113
-59
lines changed

docs/utilities/idempotency.md

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ You can use `data_keyword_argument` to tell us the argument to extract an idempo
187187

188188
=== "Using Dataclasses"
189189

190-
```python title="working_with_idempotent_function_dataclass.py" hl_lines="3-7 11 26 39"
190+
```python title="working_with_idempotent_function_dataclass.py" hl_lines="4-8 12 28 41"
191191
--8<-- "examples/idempotency/src/working_with_idempotent_function_dataclass.py"
192192
```
193193

@@ -197,7 +197,7 @@ You can use `data_keyword_argument` to tell us the argument to extract an idempo
197197

198198
=== "Using Pydantic"
199199

200-
```python title="working_with_idempotent_function_pydantic.py" hl_lines="1-5 10 23 34"
200+
```python title="working_with_idempotent_function_pydantic.py" hl_lines="3-7 12 26 37"
201201
--8<-- "examples/idempotency/src/working_with_idempotent_function_pydantic.py"
202202
```
203203

@@ -215,7 +215,7 @@ The output serializer supports any JSON serializable data, **Python Dataclasses*
215215

216216
=== "Inferring via the return type"
217217

218-
```python hl_lines="6 24 25 32 36 45"
218+
```python hl_lines="8 27 35 38 48"
219219
--8<-- "examples/idempotency/src/working_with_pydantic_deduced_output_serializer.py"
220220
```
221221

@@ -225,7 +225,7 @@ The output serializer supports any JSON serializable data, **Python Dataclasses*
225225

226226
Alternatively, you can provide an explicit model as an input to `PydanticSerializer`.
227227

228-
```python hl_lines="6 24 25 32 35 44"
228+
```python hl_lines="8 27 35 35 47"
229229
--8<-- "examples/idempotency/src/working_with_pydantic_explicitly_output_serializer.py"
230230
```
231231

@@ -235,7 +235,7 @@ The output serializer supports any JSON serializable data, **Python Dataclasses*
235235

236236
=== "Inferring via the return type"
237237

238-
```python hl_lines="8 27-29 36 40 49"
238+
```python hl_lines="9 30 38 41 51"
239239
--8<-- "examples/idempotency/src/working_with_dataclass_deduced_output_serializer.py"
240240
```
241241

@@ -245,7 +245,7 @@ The output serializer supports any JSON serializable data, **Python Dataclasses*
245245

246246
Alternatively, you can provide an explicit model as an input to `DataclassSerializer`.
247247

248-
```python hl_lines="8 27-29 36 39 48"
248+
```python hl_lines="8 30 38 40 50"
249249
--8<-- "examples/idempotency/src/working_with_dataclass_explicitly_output_serializer.py"
250250
```
251251

@@ -256,7 +256,7 @@ The output serializer supports any JSON serializable data, **Python Dataclasses*
256256
* **to_dict**. Function to convert any type to a JSON serializable dictionary before it saves into the persistent storage.
257257
* **from_dict**. Function to convert from a dictionary retrieved from persistent storage and serialize in its original form.
258258

259-
```python hl_lines="8 32 36 40 50 53"
259+
```python hl_lines="9 34 38 42 52 54 64"
260260
--8<-- "examples/idempotency/src/working_with_idempotent_function_custom_output_serializer.py"
261261
```
262262

@@ -274,7 +274,7 @@ By default, caching is disabled since we don't know how big your response could
274274

275275
=== "Enabling cache"
276276

277-
```python hl_lines="12"
277+
```python hl_lines="15"
278278
--8<-- "examples/idempotency/src/working_with_local_cache.py"
279279
```
280280

@@ -309,7 +309,7 @@ We want to use `user_id` and `product_id` fields as our idempotency key. **If we
309309

310310
=== "Payment function"
311311

312-
```python hl_lines="5-9 16 30"
312+
```python hl_lines="6-10 18 31"
313313
--8<-- "examples/idempotency/src/working_with_payload_subset.py"
314314
```
315315

@@ -327,7 +327,7 @@ By default, we protect against [concurrent executions](#handling-concurrent-exec
327327

328328
To prevent extended failures, use **`register_lambda_context`** function from your idempotency config to calculate and include the remaining invocation time in your idempotency record.
329329

330-
```python title="working_with_lambda_timeout.py" hl_lines="11 20"
330+
```python title="working_with_lambda_timeout.py" hl_lines="14 23"
331331
--8<-- "examples/idempotency/src/working_with_lambda_timeout.py"
332332
```
333333

@@ -355,7 +355,7 @@ If an exception is handled or raised **outside** your decorated function, then i
355355

356356
This persistence layer is built-in, allowing you to use an existing DynamoDB table or create a new one dedicated to idempotency state (recommended).
357357

358-
```python title="customize_persistence_layer.py" hl_lines="7-15"
358+
```python title="customize_persistence_layer.py" hl_lines="10-18"
359359
--8<-- "examples/idempotency/src/customize_persistence_layer.py"
360360
```
361361

@@ -384,12 +384,12 @@ For simple setups, initialize `RedisCachePersistenceLayer` with your cluster end
384384
For security, we enforce SSL connections by default; to disable it, set `ssl=False`.
385385

386386
=== "Redis quick start"
387-
```python title="getting_started_with_idempotency_redis_config.py" hl_lines="7-9 12 26"
387+
```python title="getting_started_with_idempotency_redis_config.py" hl_lines="8-10 14 27"
388388
--8<-- "examples/idempotency/src/getting_started_with_idempotency_redis_config.py"
389389
```
390390

391391
=== "Using an existing Redis client"
392-
```python title="getting_started_with_idempotency_redis_client.py" hl_lines="4 9-11 14 22 36"
392+
```python title="getting_started_with_idempotency_redis_client.py" hl_lines="5 10-11 16 24 38"
393393
--8<-- "examples/idempotency/src/getting_started_with_idempotency_redis_client.py"
394394
```
395395

@@ -447,7 +447,7 @@ You can customize the attribute names during initialization:
447447
| **data_attr** | | `data` | Stores results of successfully executed Lambda handlers |
448448
| **validation_key_attr** | | `validation` | Hashed representation of the parts of the event used for validation |
449449

450-
```python title="customize_persistence_layer_redis.py" hl_lines="9-16"
450+
```python title="customize_persistence_layer_redis.py" hl_lines="15-18"
451451
--8<-- "examples/idempotency/src/customize_persistence_layer_redis.py"
452452
```
453453

@@ -784,7 +784,7 @@ You can change this window with the **`expires_after_seconds`** parameter:
784784

785785
=== "Adjusting idempotency record expiration"
786786

787-
```python hl_lines="11"
787+
```python hl_lines="14"
788788
--8<-- "examples/idempotency/src/working_with_record_expiration.py"
789789
```
790790

@@ -818,7 +818,7 @@ With **`payload_validation_jmespath`**, you can provide an additional JMESPath e
818818

819819
=== "Payload validation"
820820

821-
```python hl_lines="12 20 28"
821+
```python hl_lines="16 25 32"
822822
--8<-- "examples/idempotency/src/working_with_validation_payload.py"
823823
```
824824

@@ -854,7 +854,7 @@ This means that we will raise **`IdempotencyKeyError`** if the evaluation of **`
854854

855855
=== "Idempotency key required"
856856

857-
```python hl_lines="11"
857+
```python hl_lines="14"
858858
--8<-- "examples/idempotency/src/working_with_idempotency_key_required.py"
859859
```
860860

@@ -876,13 +876,13 @@ The **`boto_config`** and **`boto3_session`** parameters enable you to pass in a
876876

877877
=== "Custom session"
878878

879-
```python hl_lines="1 11 13"
879+
```python hl_lines="3 13 16"
880880
--8<-- "examples/idempotency/src/working_with_custom_session.py"
881881
```
882882

883883
=== "Custom config"
884884

885-
```python hl_lines="1 11 13"
885+
```python hl_lines="3 13 16"
886886
--8<-- "examples/idempotency/src/working_with_custom_config.py"
887887
```
888888

@@ -902,7 +902,7 @@ You can optionally set a static value for the partition key using the `static_pk
902902

903903
=== "Reusing a DynamoDB table that uses a composite primary key"
904904

905-
```python hl_lines="7"
905+
```python hl_lines="10"
906906
--8<-- "examples/idempotency/src/working_with_composite_key.py"
907907
```
908908

@@ -931,11 +931,9 @@ You can create your own persistent store from scratch by inheriting the `BasePer
931931
* **`_update_record()`** – Updates an item in the persistence store.
932932
* **`_delete_record()`** – Removes an item from the persistence store.
933933

934-
=== "Bring your own persistent store"
935-
936-
```python hl_lines="8 18 65 74 96 124"
937-
--8<-- "examples/idempotency/src/bring_your_own_persistent_store.py"
938-
```
934+
```python title="bring_your_own_persistent_store.py" hl_lines="8 18 65 74 96 124"
935+
--8<-- "examples/idempotency/src/bring_your_own_persistent_store.py"
936+
```
939937

940938
???+ danger
941939
Pay attention to the documentation for each - you may need to perform additional checks inside these methods to ensure the idempotency guarantees remain intact.
@@ -948,7 +946,7 @@ You can set up a `response_hook` in the `IdempotentConfig` class to manipulate t
948946

949947
=== "Using an Idempotent Response Hook"
950948

951-
```python hl_lines="19 21 27 34"
949+
```python hl_lines="20 22 28 36"
952950
--8<-- "examples/idempotency/src/working_with_response_hook.py"
953951
```
954952

@@ -978,7 +976,7 @@ When using response hooks to manipulate returned data from idempotent operations
978976

979977
See [Batch integration](#batch-integration) above.
980978

981-
### Validation utility
979+
### JSON Schema Validation
982980

983981
The idempotency utility can be used with the `validator` decorator. Ensure that idempotency is the innermost decorator.
984982

@@ -990,7 +988,7 @@ The idempotency utility can be used with the `validator` decorator. Ensure that
990988

991989
=== "Using Idempotency with JSONSchema Validation utility"
992990

993-
```python hl_lines="13"
991+
```python hl_lines="16"
994992
--8<-- "examples/idempotency/src/integrate_idempotency_with_validator.py"
995993
```
996994

examples/idempotency/src/customize_persistence_layer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import os
2+
13
from aws_lambda_powertools.utilities.idempotency import (
24
DynamoDBPersistenceLayer,
35
idempotent,
46
)
57
from aws_lambda_powertools.utilities.typing import LambdaContext
68

9+
table = os.getenv("IDEMPOTENCY_TABLE")
710
persistence_layer = DynamoDBPersistenceLayer(
8-
table_name="IdempotencyTable",
11+
table_name=table,
912
key_attr="idempotency_key",
1013
expiry_attr="expires_at",
1114
in_progress_expiry_attr="in_progress_expires_at",

examples/idempotency/src/customize_persistence_layer_redis.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
from aws_lambda_powertools.utilities.idempotency import (
24
idempotent,
35
)
@@ -6,8 +8,9 @@
68
)
79
from aws_lambda_powertools.utilities.typing import LambdaContext
810

11+
redis_endpoint = os.getenv("REDIS_CLUSTER_ENDPOINT")
912
persistence_layer = RedisCachePersistenceLayer(
10-
host="localhost",
13+
host=redis_endpoint,
1114
port=6379,
1215
in_progress_expiry_attr="in_progress_expiration",
1316
status_attr="status",

examples/idempotency/src/getting_started_with_idempotency_redis_client.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from dataclasses import dataclass, field
23
from uuid import uuid4
34

@@ -11,8 +12,9 @@
1112
)
1213
from aws_lambda_powertools.utilities.typing import LambdaContext
1314

15+
redis_endpoint = os.getenv("REDIS_CLUSTER_ENDPOINT")
1416
client = Redis(
15-
host="localhost",
17+
host=redis_endpoint,
1618
port=6379,
1719
socket_connect_timeout=5,
1820
socket_timeout=5,
@@ -29,8 +31,7 @@ class Payment:
2931
payment_id: str = field(default_factory=lambda: f"{uuid4()}")
3032

3133

32-
class PaymentError(Exception):
33-
...
34+
class PaymentError(Exception): ...
3435

3536

3637
@idempotent(persistence_store=persistence_layer)

examples/idempotency/src/getting_started_with_idempotency_redis_config.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from dataclasses import dataclass, field
23
from uuid import uuid4
34

@@ -9,7 +10,8 @@
910
)
1011
from aws_lambda_powertools.utilities.typing import LambdaContext
1112

12-
persistence_layer = RedisCachePersistenceLayer(host="localhost", port=6379)
13+
redis_endpoint = os.getenv("REDIS_CLUSTER_ENDPOINT")
14+
persistence_layer = RedisCachePersistenceLayer(host=redis_endpoint, port=6379)
1315

1416

1517
@dataclass
@@ -19,8 +21,7 @@ class Payment:
1921
payment_id: str = field(default_factory=lambda: f"{uuid4()}")
2022

2123

22-
class PaymentError(Exception):
23-
...
24+
class PaymentError(Exception): ...
2425

2526

2627
@idempotent(persistence_store=persistence_layer)

examples/idempotency/src/integrate_idempotency_with_validator.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
from aws_lambda_powertools.utilities.idempotency import (
24
DynamoDBPersistenceLayer,
35
IdempotencyConfig,
@@ -6,8 +8,9 @@
68
from aws_lambda_powertools.utilities.typing import LambdaContext
79
from aws_lambda_powertools.utilities.validation import envelopes, validator
810

11+
table = os.getenv("IDEMPOTENCY_TABLE")
912
config = IdempotencyConfig(event_key_jmespath='["message", "username"]')
10-
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
13+
persistence_layer = DynamoDBPersistenceLayer(table_name=table)
1114

1215

1316
@validator(envelope=envelopes.API_GATEWAY_HTTP)

examples/idempotency/src/working_with_composite_key.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import os
2+
13
from aws_lambda_powertools.utilities.idempotency import (
24
DynamoDBPersistenceLayer,
35
idempotent,
46
)
57
from aws_lambda_powertools.utilities.typing import LambdaContext
68

7-
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable", sort_key_attr="sort_key")
9+
table = os.getenv("IDEMPOTENCY_TABLE")
10+
persistence_layer = DynamoDBPersistenceLayer(table_name=table, sort_key_attr="sort_key")
811

912

1013
@idempotent(persistence_store=persistence_layer)

examples/idempotency/src/working_with_custom_config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
from botocore.config import Config
24

35
from aws_lambda_powertools.utilities.idempotency import (
@@ -10,7 +12,8 @@
1012
# See: https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#botocore-config
1113
boto_config = Config()
1214

13-
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable", boto_config=boto_config)
15+
table = os.getenv("IDEMPOTENCY_TABLE")
16+
persistence_layer = DynamoDBPersistenceLayer(table_name=table, boto_config=boto_config)
1417

1518
config = IdempotencyConfig(event_key_jmespath="body")
1619

examples/idempotency/src/working_with_custom_session.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import boto3
24

35
from aws_lambda_powertools.utilities.idempotency import (
@@ -10,7 +12,8 @@
1012
# See: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html#module-boto3.session
1113
boto3_session = boto3.session.Session()
1214

13-
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable", boto3_session=boto3_session)
15+
table = os.getenv("IDEMPOTENCY_TABLE")
16+
persistence_layer = DynamoDBPersistenceLayer(table_name=table, boto3_session=boto3_session)
1417

1518
config = IdempotencyConfig(event_key_jmespath="body")
1619

examples/idempotency/src/working_with_dataclass_deduced_output_serializer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from dataclasses import dataclass
23

34
from aws_lambda_powertools.utilities.idempotency import (
@@ -8,7 +9,8 @@
89
from aws_lambda_powertools.utilities.idempotency.serialization.dataclass import DataclassSerializer
910
from aws_lambda_powertools.utilities.typing import LambdaContext
1011

11-
dynamodb = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
12+
table = os.getenv("IDEMPOTENCY_TABLE")
13+
dynamodb = DynamoDBPersistenceLayer(table_name=table)
1214
config = IdempotencyConfig(event_key_jmespath="order_id") # see Choosing a payload subset section
1315

1416

examples/idempotency/src/working_with_dataclass_explicitly_output_serializer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from dataclasses import dataclass
23

34
from aws_lambda_powertools.utilities.idempotency import (
@@ -8,7 +9,8 @@
89
from aws_lambda_powertools.utilities.idempotency.serialization.dataclass import DataclassSerializer
910
from aws_lambda_powertools.utilities.typing import LambdaContext
1011

11-
dynamodb = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
12+
table = os.getenv("IDEMPOTENCY_TABLE")
13+
dynamodb = DynamoDBPersistenceLayer(table_name=table)
1214
config = IdempotencyConfig(event_key_jmespath="order_id") # see Choosing a payload subset section
1315

1416

0 commit comments

Comments
 (0)