File size: 1,560 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
from aws_cdk import CfnOutput, Duration, RemovalPolicy
from aws_cdk import aws_dynamodb as dynamodb
from aws_cdk.aws_dynamodb import Table

from tests.e2e.utils.infrastructure import BaseInfrastructure


class IdempotencyDynamoDBStack(BaseInfrastructure):
    def create_resources(self):
        table = self._create_dynamodb_table()

        env_vars = {"IdempotencyTable": table.table_name}
        functions = self.create_lambda_functions(
            function_props={"environment": env_vars, "timeout": Duration.seconds(10)},
        )

        table.grant_read_write_data(functions["TtlCacheExpirationHandler"])
        table.grant_read_write_data(functions["TtlCacheTimeoutHandler"])
        table.grant_read_write_data(functions["ParallelExecutionHandler"])
        table.grant_read_write_data(functions["FunctionThreadSafetyHandler"])
        table.grant_read_write_data(functions["OptionalIdempotencyKeyHandler"])
        table.grant_read_write_data(functions["PayloadTamperingValidationHandler"])
        table.grant_read_write_data(functions["ResponseHook"])

    def _create_dynamodb_table(self) -> Table:
        table = dynamodb.Table(
            self.stack,
            "Idempotency",
            removal_policy=RemovalPolicy.DESTROY,
            partition_key=dynamodb.Attribute(name="id", type=dynamodb.AttributeType.STRING),
            time_to_live_attribute="expiration",
            billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST,
        )

        CfnOutput(self.stack, "DynamoDBTable", value=table.table_name)

        return table