Persistence
MODULE | DESCRIPTION |
---|---|
base |
Persistence layers supporting idempotency |
datarecord |
Data Class for idempotency records. |
dynamodb |
|
redis |
|
base ¶
Persistence layers supporting idempotency
CLASS | DESCRIPTION |
---|---|
BasePersistenceLayer |
Abstract Base Class for Idempotency persistence layer. |
BasePersistenceLayer ¶
BasePersistenceLayer()
Bases: ABC
Abstract Base Class for Idempotency persistence layer.
METHOD | DESCRIPTION |
---|---|
configure |
Initialize the base persistence layer from the configuration settings |
delete_record |
Delete record from the persistence store |
get_record |
Retrieve idempotency key for data provided, fetch from persistence store, and convert to DataRecord. |
save_inprogress |
Save record of function's execution being in progress |
save_success |
Save record of function's execution completing successfully |
Source code in aws_lambda_powertools/utilities/idempotency/persistence/base.py
43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
configure ¶
configure(
config: IdempotencyConfig,
function_name: str | None = None,
key_prefix: str | None = None,
) -> None
Initialize the base persistence layer from the configuration settings
PARAMETER | DESCRIPTION |
---|---|
config
|
Idempotency configuration settings
TYPE:
|
function_name
|
The name of the function being decorated
TYPE:
|
key_prefix
|
Custom prefix for idempotency key: key_prefix#hash
TYPE:
|
Source code in aws_lambda_powertools/utilities/idempotency/persistence/base.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
delete_record ¶
delete_record(data: dict[str, Any], exception: Exception)
Delete record from the persistence store
PARAMETER | DESCRIPTION |
---|---|
data
|
Payload |
exception
|
The exception raised by the function
TYPE:
|
Source code in aws_lambda_powertools/utilities/idempotency/persistence/base.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
|
get_record ¶
get_record(data: dict[str, Any]) -> DataRecord | None
Retrieve idempotency key for data provided, fetch from persistence store, and convert to DataRecord.
PARAMETER | DESCRIPTION |
---|---|
data
|
Payload |
RETURNS | DESCRIPTION |
---|---|
DataRecord
|
DataRecord representation of existing record found in persistence store |
RAISES | DESCRIPTION |
---|---|
IdempotencyItemNotFoundError
|
Exception raised if no record exists in persistence store with the idempotency key |
IdempotencyValidationError
|
Payload doesn't match the stored record for the given idempotency key |
Source code in aws_lambda_powertools/utilities/idempotency/persistence/base.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
|
save_inprogress ¶
save_inprogress(
data: dict[str, Any],
remaining_time_in_millis: int | None = None,
) -> None
Save record of function's execution being in progress
PARAMETER | DESCRIPTION |
---|---|
data
|
Payload |
remaining_time_in_millis
|
If expiry of in-progress invocations is enabled, this will contain the remaining time available in millis
TYPE:
|
Source code in aws_lambda_powertools/utilities/idempotency/persistence/base.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
|
save_success ¶
save_success(data: dict[str, Any], result: dict) -> None
Save record of function's execution completing successfully
PARAMETER | DESCRIPTION |
---|---|
data
|
Payload |
result
|
The response from function
TYPE:
|
Source code in aws_lambda_powertools/utilities/idempotency/persistence/base.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
|
datarecord ¶
Data Class for idempotency records.
CLASS | DESCRIPTION |
---|---|
DataRecord |
Data Class for idempotency records. |
DataRecord ¶
DataRecord(
idempotency_key: str,
status: str = "",
expiry_timestamp: int | None = None,
in_progress_expiry_timestamp: int | None = None,
response_data: str = "",
payload_hash: str = "",
)
Data Class for idempotency records.
METHOD | DESCRIPTION |
---|---|
get_expiration_datetime |
Converts the expiry timestamp to a datetime object. |
response_json_as_dict |
Get response data deserialized to python dict |
ATTRIBUTE | DESCRIPTION |
---|---|
is_expired |
Check if data record is expired
TYPE:
|
status |
Get status of data record
TYPE:
|
Source code in aws_lambda_powertools/utilities/idempotency/persistence/datarecord.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
is_expired
property
¶
is_expired: bool
Check if data record is expired
RETURNS | DESCRIPTION |
---|---|
bool
|
Whether the record is currently expired or not |
get_expiration_datetime ¶
get_expiration_datetime() -> datetime.datetime | None
Converts the expiry timestamp to a datetime object.
This method checks if an expiry timestamp exists and converts it to a datetime object. If no timestamp is present, it returns None.
datetime.datetime | None A datetime object representing the expiration time, or None if no expiry timestamp is set.
The returned datetime object is timezone-naive and assumes the timestamp is in the system's local timezone. Lambda default timezone is UTC.
Source code in aws_lambda_powertools/utilities/idempotency/persistence/datarecord.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
response_json_as_dict ¶
response_json_as_dict() -> dict | None
Get response data deserialized to python dict
RETURNS | DESCRIPTION |
---|---|
dict | None
|
previous response data deserialized |
Source code in aws_lambda_powertools/utilities/idempotency/persistence/datarecord.py
85 86 87 88 89 90 91 92 93 94 |
|
dynamodb ¶
CLASS | DESCRIPTION |
---|---|
DynamoDBPersistenceLayer |
|
DynamoDBPersistenceLayer ¶
DynamoDBPersistenceLayer(
table_name: str,
key_attr: str = "id",
static_pk_value: str | None = None,
sort_key_attr: str | None = None,
expiry_attr: str = "expiration",
in_progress_expiry_attr: str = "in_progress_expiration",
status_attr: str = "status",
data_attr: str = "data",
validation_key_attr: str = "validation",
boto_config: Config | None = None,
boto3_session: boto3.session.Session | None = None,
boto3_client: DynamoDBClient | None = None,
)
Bases: BasePersistenceLayer
PARAMETER | DESCRIPTION |
---|---|
table_name
|
Name of the table to use for storing execution records
TYPE:
|
key_attr
|
DynamoDB attribute name for partition key, by default "id"
TYPE:
|
static_pk_value
|
DynamoDB attribute value for partition key, by default "idempotency#
TYPE:
|
sort_key_attr
|
DynamoDB attribute name for the sort key
TYPE:
|
expiry_attr
|
DynamoDB attribute name for expiry timestamp, by default "expiration"
TYPE:
|
in_progress_expiry_attr
|
DynamoDB attribute name for in-progress expiry timestamp, by default "in_progress_expiration"
TYPE:
|
status_attr
|
DynamoDB attribute name for status, by default "status"
TYPE:
|
data_attr
|
DynamoDB attribute name for response data, by default "data"
TYPE:
|
validation_key_attr
|
DynamoDB attribute name for hashed representation of the parts of the event used for validation
TYPE:
|
boto_config
|
Botocore configuration to pass during client initialization
TYPE:
|
boto3_session
|
Boto3 session to use for AWS API communication
TYPE:
|
boto3_client
|
Boto3 DynamoDB Client to use, boto3_session and boto_config will be ignored if both are provided
TYPE:
|
Example
Create a DynamoDB persistence layer with custom settings
1 2 3 4 5 6 7 8 9 |
|
METHOD | DESCRIPTION |
---|---|
boto3_supports_condition_check_failure |
Check if the installed boto3 version supports condition check failure. |
Source code in aws_lambda_powertools/utilities/idempotency/persistence/dynamodb.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
boto3_supports_condition_check_failure
staticmethod
¶
boto3_supports_condition_check_failure(
boto3_version: str,
) -> bool
Check if the installed boto3 version supports condition check failure.
Params
RETURNS | DESCRIPTION |
---|---|
bool
|
True if the boto3 version supports condition check failure, False otherwise. |
Source code in aws_lambda_powertools/utilities/idempotency/persistence/dynamodb.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
redis ¶
CLASS | DESCRIPTION |
---|---|
RedisCachePersistenceLayer |
|
RedisClientProtocol |
Protocol class defining the interface for a Redis client. |
RedisConnection |
|
RedisCachePersistenceLayer ¶
RedisCachePersistenceLayer(
url: str = "",
host: str = "",
port: int = 6379,
username: str = "",
password: str = "",
db_index: int = 0,
mode: Literal["standalone", "cluster"] = "standalone",
ssl: bool = True,
client: RedisClientProtocol | None = None,
in_progress_expiry_attr: str = "in_progress_expiration",
expiry_attr: str = "expiration",
status_attr: str = "status",
data_attr: str = "data",
validation_key_attr: str = "validation",
)
Bases: BasePersistenceLayer
PARAMETER | DESCRIPTION |
---|---|
host
|
Redis host
TYPE:
|
port
|
Redis port
TYPE:
|
username
|
Redis username
TYPE:
|
password
|
Redis password
TYPE:
|
url
|
Redis connection string, using url will override the host/port in the previous parameters
TYPE:
|
db_index
|
Redis db index
TYPE:
|
mode
|
set Redis client mode, choose from standalone/cluster
TYPE:
|
ssl
|
set whether to use ssl for Redis connection
TYPE:
|
client
|
Bring your own Redis client that follows RedisClientProtocol. If provided, all other connection configuration options will be ignored
TYPE:
|
expiry_attr
|
Redis json attribute name for expiry timestamp, by default "expiration"
TYPE:
|
in_progress_expiry_attr
|
Redis json attribute name for in-progress expiry timestamp, by default "in_progress_expiration"
TYPE:
|
status_attr
|
Redis json attribute name for status, by default "status"
TYPE:
|
data_attr
|
Redis json attribute name for response data, by default "data"
TYPE:
|
validation_key_attr
|
Redis json attribute name for hashed representation of the parts of the event used for validation
TYPE:
|
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Source code in aws_lambda_powertools/utilities/idempotency/persistence/redis.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
|
RedisClientProtocol ¶
Bases: Protocol
Protocol class defining the interface for a Redis client.
This protocol outlines the expected behavior of a Redis client, allowing for standardization among different implementations and allowing customers to extend it in their own implementation.
METHOD | DESCRIPTION |
---|---|
- get |
Retrieves the value associated with the given key. |
- set |
|
) -> bool | None: |
Sets the value for the specified key with optional parameters. |
- delete |
Deletes one or more keys. |
Note
- The
ex
parameter represents the expiration time in seconds. - The
px
parameter represents the expiration time in milliseconds. - The
nx
parameter, if True, sets the value only if the key does not exist.
RAISES | DESCRIPTION |
---|---|
- NotImplementedError: If any of the methods are not implemented by the concrete class.
|
|
RedisConnection ¶
RedisConnection(
url: str = "",
host: str = "",
port: int = 6379,
username: str = "",
password: str = "",
db_index: int = 0,
mode: Literal["standalone", "cluster"] = "standalone",
ssl: bool = True,
)
PARAMETER | DESCRIPTION |
---|---|
host
|
Redis host
TYPE:
|
port
|
Redis port
TYPE:
|
username
|
Redis username
TYPE:
|
password
|
Redis password
TYPE:
|
url
|
Redis connection string, using url will override the host/port in the previous parameters
TYPE:
|
db_index
|
Redis db index
TYPE:
|
mode
|
set Redis client mode, choose from standalone/cluster. The default is standalone
TYPE:
|
ssl
|
set whether to use ssl for Redis connection
TYPE:
|
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
Source code in aws_lambda_powertools/utilities/idempotency/persistence/redis.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|