File size: 5,149 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
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
import json
from typing import List

from aws_cdk import CfnOutput, Duration
from aws_cdk import aws_appconfig as appconfig
from aws_cdk import aws_iam as iam
from aws_cdk import aws_ssm as ssm
from aws_cdk.aws_lambda import Function

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


class ParametersStack(BaseInfrastructure):
    def create_resources(self):
        parameters = self._create_ssm_parameters()

        env_vars = {"parameters": json.dumps(parameters)}
        functions = self.create_lambda_functions(
            function_props={"environment": env_vars, "timeout": Duration.seconds(30)},
        )

        self._create_app_config(function=functions["ParameterAppconfigFreeformHandler"])

        # NOTE: Enforce least-privilege for our param tests only
        functions["ParameterSsmGetParametersByName"].add_to_role_policy(
            iam.PolicyStatement(
                effect=iam.Effect.ALLOW,
                actions=[
                    "ssm:GetParameters",
                ],
                resources=[f"arn:aws:ssm:{self.region}:{self.account_id}:parameter/powertools/e2e/parameters/*"],
            ),
        )

    def _create_app_config(self, function: Function):
        service_name = build_service_name()

        cfn_application = appconfig.CfnApplication(
            self.stack,
            id="appconfig-app",
            name=f"powertools-e2e-{service_name}",
            description="Powertools for AWS Lambda (Python) End-to-End testing for AppConfig",
        )
        CfnOutput(self.stack, "AppConfigApplication", value=cfn_application.name)

        cfn_environment = appconfig.CfnEnvironment(
            self.stack,
            "appconfig-env",
            application_id=cfn_application.ref,
            name=f"powertools-e2e{service_name}",
            description="Powertools for AWS Lambda (Python) End-to-End testing environment",
        )
        CfnOutput(self.stack, "AppConfigEnvironment", value=cfn_environment.name)

        cfn_deployment_strategy = appconfig.CfnDeploymentStrategy(
            self.stack,
            "appconfig-deployment-strategy",
            deployment_duration_in_minutes=0,
            final_bake_time_in_minutes=0,
            growth_factor=100,
            name=f"deploymente2e{service_name}",
            description="deploymente2e",
            replicate_to="NONE",
            growth_type="LINEAR",
        )

        self._create_app_config_freeform(
            app=cfn_application,
            environment=cfn_environment,
            strategy=cfn_deployment_strategy,
            function=function,
            service_name=service_name,
        )

    def _create_app_config_freeform(
        self,
        app: appconfig.CfnApplication,
        environment: appconfig.CfnEnvironment,
        strategy: appconfig.CfnDeploymentStrategy,
        function: Function,
        service_name: str,
    ):
        cfn_configuration_profile = appconfig.CfnConfigurationProfile(
            self.stack,
            "appconfig-profile",
            application_id=app.ref,
            location_uri="hosted",
            type="AWS.Freeform",
            name=f"profilee2e{service_name}",
            description="profilee2e",
        )
        CfnOutput(self.stack, "AppConfigProfile", value=cfn_configuration_profile.name)

        cfn_hosted_configuration_version = appconfig.CfnHostedConfigurationVersion(
            self.stack,
            "appconfig-hosted-deploy",
            application_id=app.ref,
            configuration_profile_id=cfn_configuration_profile.ref,
            content='{"save_history": {"default": true}}',
            content_type="application/json",
            description="hostedconfiguratione2e",
        )
        CfnOutput(self.stack, "AppConfigConfigurationValue", value=cfn_hosted_configuration_version.content)

        appconfig.CfnDeployment(
            self.stack,
            "appconfig-deployment",
            application_id=app.ref,
            configuration_profile_id=cfn_configuration_profile.ref,
            configuration_version=cfn_hosted_configuration_version.ref,
            deployment_strategy_id=strategy.ref,
            environment_id=environment.ref,
            description="deployment",
        )

        function.add_to_role_policy(
            iam.PolicyStatement(
                effect=iam.Effect.ALLOW,
                actions=[
                    "appconfig:GetLatestConfiguration",
                    "appconfig:StartConfigurationSession",
                ],
                resources=["*"],
            ),
        )

    def _create_ssm_parameters(self) -> List[str]:
        parameters: List[str] = []

        for _ in range(10):
            param = f"/powertools/e2e/parameters/{build_random_value()}"
            rand = build_random_value()
            ssm.StringParameter(self.stack, f"param-{rand}", parameter_name=param, string_value=rand)
            parameters.append(param)

        CfnOutput(self.stack, "ParametersNameList", value=json.dumps(parameters))

        return parameters