Skip to content

Commit f85cdd1

Browse files
am29ddreamorosi
andauthored
docs(idempotency): review API docs & README (#2917)
Co-authored-by: Andrea Amorosi <[email protected]>
1 parent 9e5fa64 commit f85cdd1

18 files changed

+212
-108
lines changed

.markdownlintignore

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ LICENSE
1313
# these will be removed from the ignore and linted in future PRs
1414
packages/batch/README.md
1515
packages/commons/README.md
16-
packages/idempotency/README.md
1716
packages/jmespath/README.md
1817
packages/logger/README.md
1918
packages/metrics/README.md

docs/utilities/idempotency.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ title: Idempotency
33
description: Utility
44
---
55

6-
<!-- markdownlint-disable MD043 -->
7-
86
The idempotency utility provides a simple solution to convert your Lambda functions into idempotent operations which are safe to retry.
97

108
## Key features
@@ -744,6 +742,30 @@ Below an example implementation of a custom persistence layer backed by a generi
744742

745743
For example, the `_putRecord()` method needs to throw an error if a non-expired record already exists in the data store with a matching key.
746744

745+
## Testing your code
746+
747+
The idempotency utility provides several routes to test your code.
748+
749+
### Disabling the idempotency utility
750+
751+
When testing your code, you may wish to disable the idempotency logic altogether and focus on testing your business logic. To do this, you can set the environment variable POWERTOOLS_IDEMPOTENCY_DISABLED with a truthy value.
752+
753+
### Testing with local DynamoDB
754+
755+
When testing your Lambda function locally, you can use a local DynamoDB instance to test the idempotency feature. You can use [DynamoDB Local](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html) or [LocalStack](https://localstack.cloud/){target="_blank"}.
756+
757+
=== "handler.test.ts"
758+
759+
```typescript hl_lines="7-9"
760+
--8<-- "examples/snippets/idempotency/workingWithLocalDynamoDB.test.ts"
761+
```
762+
763+
=== "handler.ts"
764+
765+
```typescript hl_lines="7-9"
766+
--8<-- "examples/snippets/idempotency/workingWithLocalDynamoDB.ts"
767+
```
768+
747769
## Extra resources
748770

749771
If you're interested in a deep dive on how Amazon uses idempotency when building our APIs, check out
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { makeIdempotent } from '@aws-lambda-powertools/idempotency';
2+
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
3+
import type { Context } from 'aws-lambda';
4+
import { handler } from './workingWithLocalDynamoDB';
5+
6+
describe('Idempotent handler', () => {
7+
const lambdaContext = {
8+
functionName: 'foo-bar-function',
9+
memoryLimitInMB: '128',
10+
invokedFunctionArn:
11+
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function',
12+
awsRequestId: 'c6af9ac6-7b61-11e6-9a41-93e812345678',
13+
getRemainingTimeInMillis: () => 1234,
14+
} as Context;
15+
16+
const mockPersistenceStore = new DynamoDBPersistenceLayer({
17+
tableName: 'IdempotencyTable',
18+
clientConfig: { endpoint: 'http://localhost:8000' }, // 8000 for local DynamoDB and 4566 for LocalStack
19+
});
20+
21+
const idempotentHandler = makeIdempotent(handler, {
22+
persistenceStore: mockPersistenceStore,
23+
});
24+
25+
it('should return the same response', async () => {
26+
const response = await idempotentHandler(
27+
{
28+
foo: 'bar',
29+
},
30+
lambdaContext
31+
);
32+
expect(response).toEqual({
33+
statusCode: 200,
34+
body: JSON.stringify({
35+
paymentId: '123',
36+
message: 'Payment created',
37+
}),
38+
});
39+
});
40+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { makeIdempotent } from '@aws-lambda-powertools/idempotency';
2+
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
3+
import type { Context } from 'aws-lambda';
4+
5+
const ddbPersistenceStore = new DynamoDBPersistenceLayer({
6+
tableName: 'IdempotencyTable',
7+
});
8+
9+
const handler = async (event: unknown, context: Context) => {
10+
return {
11+
statusCode: 200,
12+
body: JSON.stringify({
13+
paymentId: '123',
14+
message: 'Payment created',
15+
}),
16+
};
17+
};
18+
19+
const idempotentHandler = makeIdempotent(handler, {
20+
persistenceStore: ddbPersistenceStore,
21+
});
22+
23+
export { idempotentHandler, handler };

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"docs-generateApiDoc": "typedoc .",
3434
"docs-runLocalApiDoc": "npx live-server api",
3535
"postpublish": "git restore .",
36-
"lint:markdown": "markdownlint-cli2 '**/*.md' '#node_modules' '#**/*/node_modules' '#docs/changelog.md' '#LICENSE.md' '#.github' '#**/*/CHANGELOG.md' '#examples/app/README.md' '#packages/commons/README.md' '#packages/idempotency/README.md' '#packages/jmespath/README.md' '#packages/logger/README.md' '#packages/metrics/README.md' '#packages/parameters/README.md' '#packages/parser/README.md' '#packages/tracer/README.md'"
36+
"lint:markdown": "markdownlint-cli2 '**/*.md' '#node_modules' '#**/*/node_modules' '#docs/changelog.md' '#LICENSE.md' '#.github' '#**/*/CHANGELOG.md' '#examples/app/README.md' '#packages/commons/README.md' '#packages/jmespath/README.md' '#packages/logger/README.md' '#packages/metrics/README.md' '#packages/parameters/README.md' '#packages/parser/README.md' '#packages/tracer/README.md'"
3737
},
3838
"repository": {
3939
"type": "git",

0 commit comments

Comments
 (0)