Skip to content

Commit c3404b4

Browse files
committed
docs(idempotency): cleanup idempotent decorator; inline admonitions
Signed-off-by: heitorlessa <lessa@amazon.co.uk>
1 parent 4d90a20 commit c3404b4

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
@@ -122,47 +122,45 @@ We recommend you start with a Redis compatible management services such as [Amaz
122122

123123
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.
124124

125-
!!! 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/)"
126-
127125
##### Redis IaC examples
128126

129127
=== "AWS CloudFormation example"
130128

129+
!!! tip inline end "Prefer AWS Console/CLI?"
130+
131+
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/)
132+
131133
```yaml hl_lines="5 21"
132134
--8<-- "examples/idempotency/templates/cfn_redis_serverless.yaml"
133135
```
134136

135137
1. Replace the Security Group ID and Subnet ID to match your VPC settings.
136138
2. Replace the Security Group ID and Subnet ID to match your VPC settings.
137139

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

140142
<!-- markdownlint-enable MD013 -->
141-
### Idempotent decorator
142143

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

145-
???+ note
146-
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.
146+
For simple use cases, you can use the `idempotent` decorator on your Lambda handler function.
147147

148-
!!! tip "See [Choosing a payload subset for idempotency](#choosing-a-payload-subset-for-idempotency) for more elaborate use cases."
148+
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)_.
149149

150150
=== "Idempotent decorator"
151151

152-
```python hl_lines="4-7 10 24"
152+
!!! tip "You can also choose [one or more fields](#choosing-a-payload-subset-for-idempotency) as an idempotency key."
153+
154+
```python title="getting_started_with_idempotency.py" hl_lines="5-8 12 25"
153155
--8<-- "examples/idempotency/src/getting_started_with_idempotency.py"
154156
```
155157

156158
=== "Sample event"
157159

158-
```json
160+
```json title="getting_started_with_idempotency_payload.json"
159161
--8<-- "examples/idempotency/src/getting_started_with_idempotency_payload.json"
160162
```
161163

162-
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.
163-
164-
!!! question "New to idempotency concept? Please review our [Terminology](#terminology) section if you haven't yet."
165-
166164
### Idempotent_function decorator
167165

168166
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)