File size: 888 Bytes
d8ad0fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import boto3
from retry import retry


@retry(ValueError, delay=2, jitter=1.5, tries=10)
def get_ddb_idempotency_record(
    function_name: str,
    table_name: str,
) -> int:
    """_summary_

    Parameters
    ----------
    function_name : str
        Name of Lambda function to fetch dynamodb record
    table_name : str
            Name of DynamoDB table

    Returns
    -------
    int
        Count of records found

    Raises
    ------
    ValueError
        When no record is found within retry window
    """
    ddb_client = boto3.resource("dynamodb")
    table = ddb_client.Table(table_name)
    ret = table.scan(
        FilterExpression="contains (id, :functionName)",
        ExpressionAttributeValues={":functionName": f"{function_name}#"},
    )

    if not ret["Items"]:
        raise ValueError("Empty response from DynamoDB Repeating...")

    return ret["Count"]