import type {
APIGatewayProxyEvent,
Context,
APIGatewayProxyResult,
} from 'aws-lambda';
import { makeIdempotent } from '@aws-lambda-powertools/idempotency';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
const myHandler = async (
event: APIGatewayProxyEvent,
context: Context
): Promise<APIGatewayProxyResult> => {
// your code goes here
};
// Since the function being wrapped is a Lambda handler, the `event` object is used as idempotency payload
export const handler = makeIdempotent(myHandler, {
persistenceStore: new DynamoDBPersistenceLayer({ tableName: 'my-table' }),
});
On the other hand, when the function being wrapped is an arbitray function, the data to be hashed can be extracted
from any argument of the function. In JavaScript, functions can be called with any number of arguments, and the
dataIndexArgument
property is used to indicate the index of the argument that contains the payload.
By default, if the dataIndexArgument
property is not specified, the first argument is used as idempotency payload.
However, you can specify a different argument by setting the dataIndexArgument
property. Note that the index of the
argument is zero-based, so the first argument has an index of 0
.
import { makeIdempotent } from '@aws-lambda-powertools/idempotency';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
const myFunction = (data: string, bar: number): string => {
// your code goes here
};
// This will use the `data` argument (first) as idempotency payload
const idempotentMyFunction = makeIdempotent(myFunction, {
persistenceStore: new DynamoDBPersistenceLayer({ tableName: 'my-table' }),
});
// This will use the `bar` argument as idempotency payload
const idempotentMyFunction = makeIdempotent(myFunction, {
persistenceStore: new DynamoDBPersistenceLayer({ tableName: 'my-table' }),
dataIndexArgument: 1,
});
If instead you want the Idempotency utility to use only part of your payload as idempotency payload, you can use
the eventKeyJmesPath
property to indicate the JMESPath expression to use to extract part of the payload.
This is a conditional type that represents the options that can be passed to the
makeIdempotent
function.Depending on the function being wrapped, the options will be different. For example, if the function being wrapped is a Lambda handler (as indicated by the presence of a
Context
object as the second argument), then the options will be the standardIdempotencyLambdaHandlerOptions
.If instead the function being wrapped is an arbitrary function, then the options can include a
dataIndexArgument
property to indicate the index of the argument that contains the data to be hashed.The reasoning behind this is that the
makeIdempotent
function needs to know where to find the function payload, and while we can make assumptions about the structure of a Lambda handler, we can't be certain for an arbitrary function.When the function being wrapped is a Lambda handler the
event
object, which is always the first argument of the handler, is used as idempoency payload. For this reason, you don't need to specify thedataIndexArgument
.