Skip to content

Commit 5a0952e

Browse files
committed
docs(idempotency): utility docs
1 parent c660dd0 commit 5a0952e

22 files changed

+768
-562
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { makeHandlerIdempotent } from '@aws-lambda-powertools/idempotency/middleware';
2+
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
3+
import middy from '@middy/core';
4+
import type { Context } from 'aws-lambda';
5+
import type { Request, Response } from './types';
6+
7+
const persistenceStore = new DynamoDBPersistenceLayer({
8+
tableName: 'idempotencyTableName',
9+
keyAttr: 'idempotencyKey',
10+
expiryAttr: 'expiresAt',
11+
inProgressExpiryAttr: 'inProgressExpiresAt',
12+
statusAttr: 'currentStatus',
13+
dataAttr: 'resultData',
14+
validationKeyAttr: 'validationKey',
15+
});
16+
17+
export const handler = middy(
18+
async (_event: Request, _context: Context): Promise<Response> => {
19+
try {
20+
// ... create payment
21+
22+
return {
23+
paymentId: '1234567890',
24+
message: 'success',
25+
statusCode: 200,
26+
};
27+
} catch (error) {
28+
throw new Error('Error creating payment');
29+
}
30+
}
31+
).use(
32+
makeHandlerIdempotent({
33+
persistenceStore,
34+
})
35+
);

docs/snippets/idempotency/disablingUtility.ts

-28
This file was deleted.

docs/snippets/idempotency/jestMockDynamoDB.ts

-40
This file was deleted.

docs/snippets/idempotency/localDynamoDB.ts

-23
This file was deleted.

docs/snippets/idempotency/makeFunctionIdempotentBase.ts

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { randomUUID } from 'node:crypto';
2+
import { makeHandlerIdempotent } from '@aws-lambda-powertools/idempotency/middleware';
3+
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
4+
import middy from '@middy/core';
5+
import type { Context } from 'aws-lambda';
6+
import type { Request, Response, SubscriptionResult } from './types';
7+
8+
const persistenceStore = new DynamoDBPersistenceLayer({
9+
tableName: 'idempotencyTableName',
10+
});
11+
12+
const createSubscriptionPayment = async (
13+
event: Request
14+
): Promise<SubscriptionResult> => {
15+
// ... create payment
16+
return {
17+
id: randomUUID(),
18+
productId: event.productId,
19+
};
20+
};
21+
22+
export const handler = middy(
23+
async (event: Request, _context: Context): Promise<Response> => {
24+
try {
25+
const payment = await createSubscriptionPayment(event);
26+
27+
return {
28+
paymentId: payment.id,
29+
message: 'success',
30+
statusCode: 200,
31+
};
32+
} catch (error) {
33+
throw new Error('Error creating payment');
34+
}
35+
}
36+
).use(
37+
makeHandlerIdempotent({
38+
persistenceStore,
39+
})
40+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { randomUUID } from 'node:crypto';
2+
import {
3+
makeIdempotent,
4+
IdempotencyConfig,
5+
} from '@aws-lambda-powertools/idempotency';
6+
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
7+
import type { Context } from 'aws-lambda';
8+
import type { Request, Response, SubscriptionResult } from './types';
9+
10+
const persistenceStore = new DynamoDBPersistenceLayer({
11+
tableName: 'idempotencyTableName',
12+
});
13+
const config = new IdempotencyConfig({});
14+
15+
const reportSubscriptionMetrics = async (
16+
_transactionId: string,
17+
_user: string
18+
): Promise<void> => {
19+
// ... send notification
20+
};
21+
22+
const createSubscriptionPayment = makeIdempotent(
23+
async (
24+
transactionId: string,
25+
event: Request
26+
): Promise<SubscriptionResult> => {
27+
// ... create payment
28+
return {
29+
id: transactionId,
30+
productId: event.productId,
31+
};
32+
},
33+
{
34+
persistenceStore,
35+
dataIndexArgument: 1,
36+
config,
37+
}
38+
);
39+
40+
export const handler = async (
41+
event: Request,
42+
context: Context
43+
): Promise<Response> => {
44+
config.registerLambdaContext(context);
45+
try {
46+
const transactionId = randomUUID();
47+
const payment = await createSubscriptionPayment(transactionId, event);
48+
49+
await reportSubscriptionMetrics(transactionId, event.user);
50+
51+
return {
52+
paymentId: payment.id,
53+
message: 'success',
54+
statusCode: 200,
55+
};
56+
} catch (error) {
57+
throw new Error('Error creating payment');
58+
}
59+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { randomUUID } from 'node:crypto';
2+
import { makeIdempotent } from '@aws-lambda-powertools/idempotency';
3+
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
4+
import type { Context } from 'aws-lambda';
5+
import type { Request, Response, SubscriptionResult } from './types';
6+
7+
const persistenceStore = new DynamoDBPersistenceLayer({
8+
tableName: 'idempotencyTableName',
9+
});
10+
11+
const createSubscriptionPayment = async (
12+
event: Request
13+
): Promise<SubscriptionResult> => {
14+
// ... create payment
15+
return {
16+
id: randomUUID(),
17+
productId: event.productId,
18+
};
19+
};
20+
21+
export const handler = makeIdempotent(
22+
async (event: Request, _context: Context): Promise<Response> => {
23+
try {
24+
const payment = await createSubscriptionPayment(event);
25+
26+
return {
27+
paymentId: payment.id,
28+
message: 'success',
29+
statusCode: 200,
30+
};
31+
} catch (error) {
32+
throw new Error('Error creating payment');
33+
}
34+
},
35+
{
36+
persistenceStore,
37+
}
38+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { randomUUID } from 'node:crypto';
2+
import {
3+
makeIdempotent,
4+
IdempotencyConfig,
5+
} from '@aws-lambda-powertools/idempotency';
6+
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
7+
import type { Context } from 'aws-lambda';
8+
import type { Request, Response, SubscriptionResult } from './types';
9+
10+
const persistenceStore = new DynamoDBPersistenceLayer({
11+
tableName: 'idempotencyTableName',
12+
});
13+
14+
const createSubscriptionPayment = async (
15+
user: string,
16+
productId: string
17+
): Promise<SubscriptionResult> => {
18+
// ... create payment
19+
return {
20+
id: randomUUID(),
21+
productId: productId,
22+
};
23+
};
24+
25+
// Extract the idempotency key from the request headers
26+
const config = new IdempotencyConfig({
27+
eventKeyJmesPath: 'headers."X-Idempotency-Key"',
28+
});
29+
30+
export const handler = makeIdempotent(
31+
async (event: Request, _context: Context): Promise<Response> => {
32+
try {
33+
const payment = await createSubscriptionPayment(
34+
event.user,
35+
event.productId
36+
);
37+
38+
return {
39+
paymentId: payment.id,
40+
message: 'success',
41+
statusCode: 200,
42+
};
43+
} catch (error) {
44+
throw new Error('Error creating payment');
45+
}
46+
},
47+
{
48+
persistenceStore,
49+
config,
50+
}
51+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { randomUUID } from 'node:crypto';
2+
import {
3+
makeIdempotent,
4+
IdempotencyConfig,
5+
} from '@aws-lambda-powertools/idempotency';
6+
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
7+
import type { Context } from 'aws-lambda';
8+
import type { Request, Response, SubscriptionResult } from './types';
9+
10+
const persistenceStore = new DynamoDBPersistenceLayer({
11+
tableName: 'idempotencyTableName',
12+
});
13+
const config = new IdempotencyConfig({});
14+
15+
const createSubscriptionPayment = makeIdempotent(
16+
async (
17+
transactionId: string,
18+
event: Request
19+
): Promise<SubscriptionResult> => {
20+
// ... create payment
21+
return {
22+
id: transactionId,
23+
productId: event.productId,
24+
};
25+
},
26+
{
27+
persistenceStore,
28+
dataIndexArgument: 1,
29+
config,
30+
}
31+
);
32+
33+
export const handler = async (
34+
event: Request,
35+
context: Context
36+
): Promise<Response> => {
37+
// Register the Lambda context to the IdempotencyConfig instance
38+
config.registerLambdaContext(context);
39+
try {
40+
const transactionId = randomUUID();
41+
const payment = await createSubscriptionPayment(transactionId, event);
42+
43+
return {
44+
paymentId: payment.id,
45+
message: 'success',
46+
statusCode: 200,
47+
};
48+
} catch (error) {
49+
throw new Error('Error creating payment');
50+
}
51+
};

0 commit comments

Comments
 (0)