-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__main__.py
More file actions
201 lines (190 loc) · 5.95 KB
/
__main__.py
File metadata and controls
201 lines (190 loc) · 5.95 KB
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
191
192
193
194
195
196
197
198
199
200
201
import pulumi
import pulumi_aws as aws
# Get AWS region from Pulumi configuration and create an explicit provider.
config = pulumi.Config()
default_region = config.require("region")
aws_provider = aws.Provider("aws-provider",
region=default_region,
default_tags=aws.ProviderDefaultTagsArgs(
tags={
"Environment": pulumi.get_stack(),
"Project": pulumi.get_project(),
}
)
)
# A shared S3 bucket
bucket = aws.s3.Bucket("shared-bucket",
acl="private",
opts=pulumi.ResourceOptions(provider=aws_provider)
)
# Lambda execution role.
lambda_role = aws.iam.Role("uptime-role",
assume_role_policy='''{
"Version": "2012-10-17",
"Statement": [{
"Action": "sts:AssumeRole",
"Principal": { "Service": "lambda.amazonaws.com" },
"Effect": "Allow",
"Sid": ""
}]
}''',
opts=pulumi.ResourceOptions(provider=aws_provider)
)
# Attach basic Lambda execution policy.
aws.iam.RolePolicyAttachment("uptime-role-policy",
role=lambda_role.name,
policy_arn="arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
opts=pulumi.ResourceOptions(provider=aws_provider)
)
# Allow Lambda to access the S3 bucket.
aws.iam.RolePolicy("uptime-bucket-policy",
role=lambda_role.id,
policy=pulumi.Output.all(bucket.arn).apply(lambda args: f'''{{
"Version": "2012-10-17",
"Statement": [{{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:ListBucket"],
"Resource": ["{args[0]}", "{args[0]}/*"]
}}]
}}'''),
opts=pulumi.ResourceOptions(provider=aws_provider)
)
# Lambda function definition.
function = aws.lambda_.Function("uptime-function",
code=pulumi.AssetArchive({
".": pulumi.FileArchive("app")
}),
role=lambda_role.arn,
handler="index.handler",
runtime="python3.8",
environment=aws.lambda_.FunctionEnvironmentArgs(
variables={"BUCKET_NAME": bucket.id}
),
opts=pulumi.ResourceOptions(provider=aws_provider)
)
# Lambda function URL.
function_url = aws.lambda_.FunctionUrl("uptime-function-url",
function_name=function.name,
authorization_type="NONE",
cors=aws.lambda_.FunctionUrlCorsArgs(
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"]
),
opts=pulumi.ResourceOptions(provider=aws_provider)
)
# CloudWatch alarm for Lambda errors.
error_rate_alarm = aws.cloudwatch.MetricAlarm("uptime-error-rate-alarm",
name="uptime-function-error-rate",
comparison_operator="GreaterThanThreshold",
evaluation_periods=2,
threshold=0.1, # error rate threshold in percentage
metric_queries=[
{
"id": "m1",
"metric": {
"namespace": "AWS/Lambda",
"metricName": "Invocations",
"dimensions": {"FunctionName": function.name},
"period": 60,
"stat": "Sum",
},
"return_data": False,
},
{
"id": "m2",
"metric": {
"namespace": "AWS/Lambda",
"metricName": "Errors",
"dimensions": {"FunctionName": function.name},
"period": 60,
"stat": "Sum",
},
"return_data": False,
},
{
"id": "e1",
"expression": "m2/m1*100",
"label": "Error Rate (%)",
"return_data": True,
}
],
alarm_description="Alarm when Lambda error rate exceeds threshold",
opts=pulumi.ResourceOptions(provider=aws_provider)
)
# Create a Route53 health check for the Lambda function URL.
health_check = aws.route53.HealthCheck("uptime-health-check",
type="HTTPS",
fqdn=function_url.function_url.apply(lambda url: url.replace("https://", "").split("/")[0]),
resource_path="/", # adjust if your endpoint requires a different path
port=443,
request_interval=10,
failure_threshold=2,
opts=pulumi.ResourceOptions(provider=aws_provider)
)
# CloudWatch dashboard with SRE metrics (percentages).
dashboard = aws.cloudwatch.Dashboard("uptime-service-dashboard",
dashboard_name="uptime-service-dashboard",
dashboard_body=pulumi.Output.all(function.name).apply(
lambda args: f'''{{
"widgets": [
{{
"type": "metric",
"properties": {{
"metrics": [
["AWS/Lambda", "Invocations", "FunctionName", "{args[0]}"]
],
"period": 60,
"stat": "Sum",
"region": "{default_region}",
"title": "Traffic: Total Requests"
}}
}},
{{
"type": "metric",
"properties": {{
"metrics": [
["AWS/Lambda", "Duration", "FunctionName", "{args[0]}"]
],
"period": 60,
"stat": "Average",
"region": "{default_region}",
"title": "Latency: Average Duration (ms)"
}}
}},
{{
"type": "metric",
"properties": {{
"metrics": [
["AWS/Lambda", "Invocations", "FunctionName", "{args[0]}", {{"id": "m1", "visible": false}}],
["AWS/Lambda", "Errors", "FunctionName", "{args[0]}", {{"id": "m2", "visible": false}}],
[{{"expression": "m2/m1*100", "label": "Error Rate (%)", "id": "e1"}}]
],
"period": 60,
"region": "{default_region}",
"title": "Errors: Error Rate (%)"
}}
}},
{{
"type": "metric",
"properties": {{
"metrics": [
["AWS/Lambda", "ConcurrentExecutions", "FunctionName", "{args[0]}"]
],
"period": 60,
"stat": "Maximum",
"region": "{default_region}",
"title": "Saturation: Concurrent Executions"
}}
}}
]
}}'''
),
opts=pulumi.ResourceOptions(provider=aws_provider)
)
# Export relevant outputs.
pulumi.export("uptime_function_url", function_url.function_url)
pulumi.export("uptime_dashboard_url", pulumi.Output.concat(
"https://console.aws.amazon.com/cloudwatch/home?region=", default_region,
"#dashboards:name=", dashboard.dashboard_name
))