File size: 5,856 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 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | import uuid
import jsii
from aws_cdk import (
Aspects,
CfnCondition,
CfnParameter,
CfnResource,
CustomResource,
Duration,
Fn,
IAspect,
Stack,
)
from aws_cdk.aws_iam import (
Effect,
ManagedPolicy,
PolicyStatement,
Role,
ServicePrincipal,
)
from aws_cdk.aws_lambda import Architecture, Code, Function, LayerVersion, Runtime
from aws_cdk.aws_logs import RetentionDays
from aws_cdk.aws_ssm import StringParameter
from aws_cdk.custom_resources import Provider
from constructs import Construct
VERSION_TRACKING_EVENT_BUS_ARN: str = "arn:aws:events:eu-central-1:027876851704:event-bus/VersionTrackingEventBus"
@jsii.implements(IAspect)
class ApplyCondition:
def __init__(self, condition: CfnCondition):
self.condition = condition
def visit(self, node):
if isinstance(node, CfnResource):
node.cfn_options.condition = self.condition
class CanaryStack(Stack):
def __init__(
self,
scope: Construct,
construct_id: str,
powertools_version: str,
python_version: str,
ssm_paramter_layer_arn: str,
ssm_parameter_layer_arm64_arn: str,
**kwargs,
) -> None:
super().__init__(scope, construct_id, **kwargs)
deploy_stage = CfnParameter(self, "DeployStage", description="Deployment stage for canary").value_as_string
has_arm64_support = CfnParameter(
self,
"HasARM64Support",
description="Has ARM64 Support Condition",
type="String",
allowed_values=["true", "false"],
)
has_arm64_condition = CfnCondition(
self,
"HasARM64SupportCondition",
expression=Fn.condition_equals(has_arm64_support, "true"),
)
layer_arn = StringParameter.from_string_parameter_attributes(
self,
"LayerVersionArnParam",
parameter_name=ssm_paramter_layer_arn,
).string_value
Canary(
self,
"Canary-x86-64",
layer_arn=layer_arn,
powertools_version=powertools_version,
python_version=python_version,
architecture=Architecture.X86_64,
stage=deploy_stage,
)
layer_arm64_arn = StringParameter.from_string_parameter_attributes(
self,
"LayerArm64VersionArnParam",
parameter_name=ssm_parameter_layer_arm64_arn,
).string_value
arm64_canary = Canary(
self,
"Canary-arm64",
layer_arn=layer_arm64_arn,
powertools_version=powertools_version,
python_version=python_version,
architecture=Architecture.ARM_64,
stage=deploy_stage,
)
Aspects.of(arm64_canary).add(ApplyCondition(has_arm64_condition))
class Canary(Construct):
def __init__(
self,
scope: Construct,
construct_id: str,
layer_arn: str,
powertools_version: str,
python_version: str,
architecture: Architecture,
stage: str,
):
super().__init__(scope, construct_id)
python_version_normalized = python_version.replace(".", "")
layer = LayerVersion.from_layer_version_arn(self, "PowertoolsLayer", layer_version_arn=layer_arn)
execution_role = Role(
self,
"LambdaExecutionRole",
assumed_by=ServicePrincipal("lambda.amazonaws.com"),
)
execution_role.add_managed_policy(
ManagedPolicy.from_aws_managed_policy_name("service-role/AWSLambdaBasicExecutionRole"),
)
execution_role.add_to_policy(
PolicyStatement(effect=Effect.ALLOW, actions=["lambda:GetFunction"], resources=["*"]),
)
if python_version == "python3.8":
runtime = Runtime.PYTHON_3_8
elif python_version == "python3.9":
runtime = Runtime.PYTHON_3_9
elif python_version == "python3.10":
runtime = Runtime.PYTHON_3_10
elif python_version == "python3.11":
runtime = Runtime.PYTHON_3_11
elif python_version == "python3.12":
runtime = Runtime.PYTHON_3_12
elif python_version == "python3.13":
runtime = Runtime.PYTHON_3_13
else:
raise ValueError("Unsupported Python version")
canary_lambda = Function(
self,
f"CanaryLambdaFunction-{python_version_normalized}",
code=Code.from_asset("layer/canary"),
handler="app.on_event",
layers=[layer],
memory_size=512,
timeout=Duration.seconds(10),
runtime=runtime,
architecture=architecture,
log_retention=RetentionDays.TEN_YEARS,
role=execution_role,
environment={
"POWERTOOLS_VERSION": powertools_version,
"POWERTOOLS_LAYER_ARN": layer_arn,
"VERSION_TRACKING_EVENT_BUS_ARN": VERSION_TRACKING_EVENT_BUS_ARN,
"LAYER_PIPELINE_STAGE": stage,
},
)
canary_lambda.add_to_role_policy(
PolicyStatement(
effect=Effect.ALLOW,
actions=["events:PutEvents"],
resources=[VERSION_TRACKING_EVENT_BUS_ARN],
),
)
# custom resource provider configuration
provider = Provider(
self,
"CanaryCustomResource",
on_event_handler=canary_lambda,
log_retention=RetentionDays.TEN_YEARS,
)
# force to recreate resource on each deployment with randomized name
CustomResource(
self,
f"CanaryTrigger-{str(uuid.uuid4())[0:7]}",
service_token=provider.service_token,
)
|