You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/idempotency/README.md
+89-97
Original file line number
Diff line number
Diff line change
@@ -8,15 +8,33 @@
8
8
9
9
Powertools for AWS Lambda (TypeScript) is a developer toolkit to implement Serverless [best practices and increase developer velocity](https://docs.powertools.aws.dev/lambda-typescript/latest/#features).
10
10
11
+
You can use the package in both TypeScript and JavaScript code bases.
-[How to support Powertools for AWS Lambda (TypeScript)?](#how-to-support-powertools-for-aws-lambda-typescript)
23
+
-[Becoming a reference customer](#becoming-a-reference-customer)
24
+
-[Sharing your work](#sharing-your-work)
25
+
-[Using Lambda Layer](#using-lambda-layer)
26
+
-[Credits](#credits)
27
+
-[License](#license)
28
+
11
29
## Intro
12
30
13
31
This package provides a utility to implement idempotency in your Lambda functions.
14
-
You can either use it as a decorator on your Lambda handler or as a wrapper on any other function.
15
-
If you use middy, we also provide a middleware to make your Lambda handler idempotent.
16
-
The current implementation provides a persistance layer for Amazon DynamoDB, which offers a variety of configuration options.
17
-
You can also bring your own persistance layer by implementing the `IdempotencyPersistanceLayer` interface.
32
+
You can either use it to wrapp a function, or as Middy middleware to make your AWS Lambda handler idempotent.
33
+
34
+
The current implementation provides a persistence layer for Amazon DynamoDB, which offers a variety of configuration options. You can also bring your own persistence layer by extending the `BasePersistenceLayer` class.
18
35
19
36
## Key features
37
+
20
38
* Prevent Lambda handler from executing more than once on the same event payload during a time window
21
39
* Ensure Lambda handler returns the same result when called with the same payload
22
40
* Select a subset of the event as the idempotency key using JMESPath expressions
@@ -25,125 +43,99 @@ You can also bring your own persistance layer by implementing the `IdempotencyPe
25
43
26
44
## Usage
27
45
28
-
### Decorators
29
-
If you use classes to define your Lambda handlers, you can use the decorators to make your handler idempotent or a specific function idempotent.
30
-
We offer two decorators:
31
-
*`@idempotentLambdaHandler`: makes the handler idempotent.
32
-
*`@idempotentFunction`: makes any function within your class idempotent
46
+
To get started, install the library by running:
33
47
34
-
The first can only be applied to the handler function with the specific signature of a Lambda handler.
35
-
The second can be applied to any function within your class. In this case you need to pass a `Record` object and provide the `dataKeywordArgument` parameter to specify the name of the argument that contains the data to be used as the idempotency key.
36
-
In any of both cases yoiu need to pass the persistance layer where we will store the idempotency information.
48
+
```sh
49
+
npm install @aws-lambda-powertools/idempotency
50
+
```
37
51
52
+
Next, review the IAM permissions attached to your AWS Lambda function and make sure you allow the [actions detailed](https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/idempotency/#iam-permissions) in the documentation of the utility.
38
53
39
54
### Function wrapper
40
55
41
-
A more common approach is to use the function wrapper.
42
-
Similar to `@idempotentFunction` decorator you need to pass keyword argument to indicate which part of the payload will be hashed.
43
-
44
-
### Middy middleware
45
-
// TODO: after e2e tests are implemented
46
-
47
-
### DynamoDB peristance layer
48
-
To store the idempotency information offer a DynamoDB persistance layer.
49
-
This enables you to store the hash key, payload, status for progress and expiration and much more.
50
-
You can customise most of the configuration options of the DynamoDB table, i.e the names of the attributes.
51
-
See the [API documentation](https://docs.powertools.aws.dev/lambda-typescript/latest/modules/.index.DynamoDBPersistenceLayer.html) for more details.
The `dataKeywordArgument` parameter is optional. If not provided, the whole event will be used as the idempotency key.
102
-
Otherwise, you need to specify the string name of the argument that contains the data to be used as the idempotency key.
103
-
For example if you have an input like this:
86
+
Note that we are specifying a `dataKeywordArgument` option, this tells the Idempotency utility which field(s) will be used as idempotency key.
104
87
88
+
Check the [docs](https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/idempotency/) for more examples.
105
89
106
-
```json
107
-
{
108
-
"transactionId": 1235,
109
-
"product": "book",
110
-
"quantity": 1,
111
-
"price": 10
112
-
}
113
-
```
114
-
115
-
You can use `transactionId` as the idempotency key. This will ensure that the same transaction is not processed twice.
90
+
### Middy middleware
116
91
117
-
### Function wrapper
92
+
If instead you use Middy, you can use the `makeHandlerIdempotent` middleware. When using the middleware your Lambda handler becomes idempotent.
118
93
119
-
In case where you don't use classes and decorators you can wrap your function to make it idempotent.
94
+
By default, the Idempotency utility will use the full event payload to create an hash and determine if a request is idempotent, and therefore it should not be retried. When dealing with a more elaborate payload, where parts of the payload always change you should use the `IdempotencyConfig` object to instruct the utility to only use a portion of your payload. This is useful when dealing with payloads that contain timestamps or request ids.
Check the [docs](https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/idempotency/) for more examples.
131
+
132
+
### DynamoDB persistence layer
133
+
134
+
You can use a DynamoDB Table to store the idempotency information. This enables you to keep track of the hash key, payload, status for progress, expiration, and much more.
135
+
136
+
You can customize most of the configuration options of the table, i.e the names of the attributes.
137
+
See the [API documentation](https://docs.powertools.aws.dev/lambda/typescript/latest/api/types/_aws_lambda_powertools_idempotency.types.DynamoDBPersistenceOptions.html) for more details.
138
+
147
139
## Contribute
148
140
149
141
If you are interested in contributing to this project, please refer to our [Contributing Guidelines](https://github.com/aws-powertools/powertools-lambda-typescript/blob/main/CONTRIBUTING.md).
0 commit comments