File size: 3,333 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
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
import time
from typing import Tuple

from aws_cdk import Duration
from aws_cdk import aws_ec2 as ec2
from aws_cdk.aws_ec2 import (
    SecurityGroup,
    SubnetType,
    Vpc,
)
from aws_cdk.aws_elasticache import (
    CfnServerlessCache,
)

from tests.e2e.utils.data_builder import build_random_value
from tests.e2e.utils.infrastructure import BaseInfrastructure


class IdempotencyRedisServerlessStack(BaseInfrastructure):
    def create_resources(self) -> None:
        service_name = build_random_value(10).replace("_", "")

        vpc_stack: Vpc = self._create_vpc(service_name, "172.150.0.0/16")
        security_groups: Tuple = self._create_security_groups(vpc_stack)
        redis_cluster: CfnServerlessCache = self._create_redis_cache(service_name, vpc_stack, security_groups[0])

        env_vars = {"RedisEndpoint": f"{str(redis_cluster.attr_endpoint_address)}"}

        self.create_lambda_functions(
            function_props={
                "environment": env_vars,
                "vpc": vpc_stack,
                "security_groups": [security_groups[1]],
                "timeout": Duration.seconds(10),
            },
        )

    def _create_vpc(self, service_name: str, cidr: str) -> Vpc:
        vpc_stack: Vpc = Vpc(
            self.stack,
            "VPC-ServerlessCache",
            nat_gateways=1,
            vpc_name=f"VPC-ServerlessCache-{service_name}",
            ip_addresses=ec2.IpAddresses.cidr(cidr),
            subnet_configuration=[
                ec2.SubnetConfiguration(name="public", subnet_type=SubnetType.PUBLIC, cidr_mask=24),
                ec2.SubnetConfiguration(name="private", subnet_type=SubnetType.PRIVATE_WITH_EGRESS, cidr_mask=24),
            ],
            max_azs=2,
        )

        return vpc_stack

    def _create_security_groups(self, vpc_stack: Vpc) -> Tuple[SecurityGroup, SecurityGroup]:
        # Create a security group for the ElastiCache cluster
        cache_security_group: SecurityGroup = SecurityGroup(self.stack, "ElastiCacheSecurityGroup", vpc=vpc_stack)
        cache_security_group.add_ingress_rule(
            peer=ec2.Peer.ipv4(vpc_stack.vpc_cidr_block),
            connection=ec2.Port.tcp(6379),
            description="Allow inbound traffic from VPC",
        )

        lambda_security_group = SecurityGroup(
            self.stack,
            "LambdaSecurityGroup",
            vpc=vpc_stack,
            allow_all_ipv6_outbound=True,
            allow_all_outbound=True,
        )

        return cache_security_group, lambda_security_group

    def _create_redis_cache(
        self,
        service_name: str,
        vpc_stack: Vpc,
        cache_security_group: SecurityGroup,
    ) -> CfnServerlessCache:
        cache_cluster = CfnServerlessCache(
            self.stack,
            "ElastiCacheCluster",
            engine="redis",
            security_group_ids=[cache_security_group.security_group_id],
            subnet_ids=[subnet.subnet_id for subnet in vpc_stack.private_subnets],
            serverless_cache_name=f"Cache-{service_name}",
        )

        # Just to make sure the Cluster will be ready before the Stack is complete
        while cache_cluster.attr_status == "CREATING":
            print("Waiting for ElastiCache serverless to be created...")
            time.sleep(5)

        return cache_cluster