Skip to content

Commit 92307cb

Browse files
committed
docs(idempotency): cleanup idempotent decorator; inline admonitions
Signed-off-by: heitorlessa <[email protected]>
1 parent 8b4a7b3 commit 92307cb

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

docs/utilities/idempotency.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,47 +119,45 @@ We recommend you start with a Redis compatible management services such as [Amaz
119119

120120
In both services and self-hosting Redis, you'll need to configure [VPC access](https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html){target="_blank"} to your AWS Lambda.
121121

122-
!!! tip "First time setting it all up? Checkout the official tutorials for [Amazon ElastiCache for Redis](https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/LambdaRedis.html) or [Amazon MemoryDB for Redis](https://aws.amazon.com/blogs/database/access-amazon-memorydb-for-redis-from-aws-lambda/)"
123-
124122
##### Redis IaC examples
125123

126124
=== "AWS CloudFormation example"
127125

126+
!!! tip inline end "Prefer AWS Console/CLI?"
127+
128+
Follow the official tutorials for [Amazon ElastiCache for Redis](https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/LambdaRedis.html) or [Amazon MemoryDB for Redis](https://aws.amazon.com/blogs/database/access-amazon-memorydb-for-redis-from-aws-lambda/)
129+
128130
```yaml hl_lines="5 21"
129131
--8<-- "examples/idempotency/templates/cfn_redis_serverless.yaml"
130132
```
131133

132134
1. Replace the Security Group ID and Subnet ID to match your VPC settings.
133135
2. Replace the Security Group ID and Subnet ID to match your VPC settings.
134136

135-
Once setup, you can find quick start and advanced examples for Redis in [the persistent layers section](RedisCachePersistenceLayer).
137+
Once setup, you can find a quick start and advanced examples for Redis in [the persistent layers section](RedisCachePersistenceLayer).
136138

137139
<!-- markdownlint-enable MD013 -->
138-
### Idempotent decorator
139140

140-
You can quickly start by initializing the `DynamoDBPersistenceLayer` class and using it with the `idempotent` decorator on your lambda handler.
141+
### Idempotent decorator
141142

142-
???+ note
143-
In this example, the entire Lambda handler is treated as a single idempotent operation. If your Lambda handler can cause multiple side effects, or you're only interested in making a specific logic idempotent, use [`idempotent_function`](#idempotent_function-decorator) instead.
143+
For simple use cases, you can use the `idempotent` decorator on your Lambda handler function.
144144

145-
!!! tip "See [Choosing a payload subset for idempotency](#choosing-a-payload-subset-for-idempotency) for more elaborate use cases."
145+
It will treat the entire event as an idempotency key. That is, the same event will return the previously stored result within a [configurable time window](#expiring-idempotency-records) _(1 hour, by default)_.
146146

147147
=== "Idempotent decorator"
148148

149-
```python hl_lines="4-7 10 24"
149+
!!! tip "You can also choose [one or more fields](#choosing-a-payload-subset-for-idempotency) as an idempotency key."
150+
151+
```python title="getting_started_with_idempotency.py" hl_lines="5-8 12 25"
150152
--8<-- "examples/idempotency/src/getting_started_with_idempotency.py"
151153
```
152154

153155
=== "Sample event"
154156

155-
```json
157+
```json title="getting_started_with_idempotency_payload.json"
156158
--8<-- "examples/idempotency/src/getting_started_with_idempotency_payload.json"
157159
```
158160

159-
After processing this request successfully, a second request containing the exact same payload above will now return the same response, ensuring our customer isn't charged twice.
160-
161-
!!! question "New to idempotency concept? Please review our [Terminology](#terminology) section if you haven't yet."
162-
163161
### Idempotent_function decorator
164162

165163
Similar to [idempotent decorator](#idempotent-decorator), you can use `idempotent_function` decorator for any synchronous Python function.

examples/idempotency/src/getting_started_with_idempotency.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

@@ -7,7 +8,8 @@
78
)
89
from aws_lambda_powertools.utilities.typing import LambdaContext
910

10-
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
11+
table = os.getenv("IDEMPOTENCY_TABLE")
12+
persistence_layer = DynamoDBPersistenceLayer(table_name=table)
1113

1214

1315
@dataclass
@@ -17,8 +19,7 @@ class Payment:
1719
payment_id: str = field(default_factory=lambda: f"{uuid4()}")
1820

1921

20-
class PaymentError(Exception):
21-
...
22+
class PaymentError(Exception): ...
2223

2324

2425
@idempotent(persistence_store=persistence_layer)

examples/idempotency/templates/sam.yaml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ Resources:
2121
Handler: app.py
2222
Policies:
2323
- Statement:
24-
- Sid: AllowDynamodbReadWrite
25-
Effect: Allow
26-
Action:
27-
- dynamodb:PutItem
28-
- dynamodb:GetItem
29-
- dynamodb:UpdateItem
30-
- dynamodb:DeleteItem
31-
Resource: !GetAtt IdempotencyTable.Arn
24+
- Sid: AllowDynamodbReadWrite
25+
Effect: Allow
26+
Action:
27+
- dynamodb:PutItem
28+
- dynamodb:GetItem
29+
- dynamodb:UpdateItem
30+
- dynamodb:DeleteItem
31+
Resource: !GetAtt IdempotencyTable.Arn
32+
Environment:
33+
Variables:
34+
IDEMPOTENCY_TABLE: !Ref IdempotencyTable

0 commit comments

Comments
 (0)