Skip to content

Commit 4ac2281

Browse files
authored
docs(idempotency): document keyPrefix feature (#3611)
1 parent 7be7a83 commit 4ac2281

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

Diff for: docs/utilities/idempotency.md

+25
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,31 @@ This means that we will raise **`IdempotencyKeyError`** if the evaluation of **`
695695
--8<-- "examples/snippets/idempotency/samples/workingWIthIdempotencyRequiredKeyError.json"
696696
```
697697

698+
### Customizing the idempotency key prefix
699+
700+
!!! warning
701+
Changing the idempotency key generation will invalidate existing idempotency records
702+
703+
You can use the `keyPrefix` parameter in any of the idempotency configurations to define a custom prefix for your idempotency key. This allows you to decouple the idempotency key from the function name, which is especially useful during application refactorings.
704+
705+
=== "Using a custom prefix with function wrapper"
706+
707+
```typescript hl_lines="25"
708+
--8<-- "examples/snippets/idempotency/customKeyPrefixFnWrapper.ts"
709+
```
710+
711+
=== "Using a custom prefix with decorator"
712+
713+
```typescript hl_lines="13"
714+
--8<-- "examples/snippets/idempotency/customKeyPrefixDecorator.ts"
715+
```
716+
717+
=== "Using a custom prefix with Middy.js middleware"
718+
719+
```typescript hl_lines="14"
720+
--8<-- "examples/snippets/idempotency/customKeyPrefixMiddleware.ts"
721+
```
722+
698723
### Batch integration
699724

700725
You can easily integrate with [Batch](batch.md) utility by using idempotency wrapper around your processing function.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { randomUUID } from 'node:crypto';
2+
import { idempotent } from '@aws-lambda-powertools/idempotency';
3+
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
4+
import type { Context } from 'aws-lambda';
5+
6+
const persistenceStore = new DynamoDBPersistenceLayer({
7+
tableName: 'idempotencyTableName',
8+
});
9+
10+
class Lambda {
11+
@idempotent({
12+
persistenceStore,
13+
keyPrefix: 'createSubscriptionPayment',
14+
})
15+
async handler(event: unknown, context: Context) {
16+
try {
17+
// ... create payment
18+
19+
return {
20+
paymentId: randomUUID(),
21+
message: 'success',
22+
statusCode: 200,
23+
};
24+
} catch (error) {
25+
throw new Error('Error creating payment');
26+
}
27+
}
28+
}
29+
30+
const lambda = new Lambda();
31+
export const handler = lambda.handler.bind(lambda);
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { randomUUID } from 'node:crypto';
2+
import { makeIdempotent } from '@aws-lambda-powertools/idempotency';
3+
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
4+
5+
const persistenceStore = new DynamoDBPersistenceLayer({
6+
tableName: 'idempotencyTableName',
7+
});
8+
9+
export const handler = makeIdempotent(
10+
async () => {
11+
try {
12+
// ... create payment
13+
14+
return {
15+
paymentId: randomUUID(),
16+
message: 'success',
17+
statusCode: 200,
18+
};
19+
} catch (error) {
20+
throw new Error('Error creating payment');
21+
}
22+
},
23+
{
24+
persistenceStore,
25+
keyPrefix: 'createSubscriptionPayment',
26+
}
27+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { randomUUID } from 'node:crypto';
2+
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
3+
import { makeHandlerIdempotent } from '@aws-lambda-powertools/idempotency/middleware';
4+
import middy from '@middy/core';
5+
6+
const persistenceStore = new DynamoDBPersistenceLayer({
7+
tableName: 'idempotencyTableName',
8+
});
9+
10+
export const handler = middy()
11+
.use(
12+
makeHandlerIdempotent({
13+
persistenceStore,
14+
keyPrefix: 'createSubscriptionPayment',
15+
})
16+
)
17+
.handler(async () => {
18+
try {
19+
// ... create payment
20+
21+
return {
22+
paymentId: randomUUID(),
23+
message: 'success',
24+
statusCode: 200,
25+
};
26+
} catch (error) {
27+
throw new Error('Error creating payment');
28+
}
29+
});

0 commit comments

Comments
 (0)