Skip to content

docs(idempotency): document keyPrefix feature #3611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/utilities/idempotency.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,31 @@ This means that we will raise **`IdempotencyKeyError`** if the evaluation of **`
--8<-- "examples/snippets/idempotency/samples/workingWIthIdempotencyRequiredKeyError.json"
```

### Customizing the idempotency key prefix

!!! warning
Changing the idempotency key generation will invalidate existing idempotency records

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.

=== "Using a custom prefix with function wrapper"

```typescript hl_lines="25"
--8<-- "examples/snippets/idempotency/customKeyPrefixFnWrapper.ts"
```

=== "Using a custom prefix with decorator"

```typescript hl_lines="13"
--8<-- "examples/snippets/idempotency/customKeyPrefixDecorator.ts"
```

=== "Using a custom prefix with Middy.js middleware"

```typescript hl_lines="14"
--8<-- "examples/snippets/idempotency/customKeyPrefixMiddleware.ts"
```

### Batch integration

You can easily integrate with [Batch](batch.md) utility by using idempotency wrapper around your processing function.
Expand Down
31 changes: 31 additions & 0 deletions examples/snippets/idempotency/customKeyPrefixDecorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { randomUUID } from 'node:crypto';
import { idempotent } from '@aws-lambda-powertools/idempotency';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
import type { Context } from 'aws-lambda';

const persistenceStore = new DynamoDBPersistenceLayer({
tableName: 'idempotencyTableName',
});

class Lambda {
@idempotent({
persistenceStore,
keyPrefix: 'createSubscriptionPayment',
})
async handler(event: unknown, context: Context) {
try {
// ... create payment

return {
paymentId: randomUUID(),
message: 'success',
statusCode: 200,
};
} catch (error) {
throw new Error('Error creating payment');
}
}
}

const lambda = new Lambda();
export const handler = lambda.handler.bind(lambda);
27 changes: 27 additions & 0 deletions examples/snippets/idempotency/customKeyPrefixFnWrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { randomUUID } from 'node:crypto';
import { makeIdempotent } from '@aws-lambda-powertools/idempotency';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';

const persistenceStore = new DynamoDBPersistenceLayer({
tableName: 'idempotencyTableName',
});

export const handler = makeIdempotent(
async () => {
try {
// ... create payment

return {
paymentId: randomUUID(),
message: 'success',
statusCode: 200,
};
} catch (error) {
throw new Error('Error creating payment');
}
},
{
persistenceStore,
keyPrefix: 'createSubscriptionPayment',
}
);
29 changes: 29 additions & 0 deletions examples/snippets/idempotency/customKeyPrefixMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { randomUUID } from 'node:crypto';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
import { makeHandlerIdempotent } from '@aws-lambda-powertools/idempotency/middleware';
import middy from '@middy/core';

const persistenceStore = new DynamoDBPersistenceLayer({
tableName: 'idempotencyTableName',
});

export const handler = middy()
.use(
makeHandlerIdempotent({
persistenceStore,
keyPrefix: 'createSubscriptionPayment',
})
)
.handler(async () => {
try {
// ... create payment

return {
paymentId: randomUUID(),
message: 'success',
statusCode: 200,
};
} catch (error) {
throw new Error('Error creating payment');
}
});
Loading