Repository navigation
Expand file tree
/
Copy pathdev-auto-stop-lambda.tf
More file actions
384 lines (334 loc) · 14.2 KB
/
Copy pathdev-auto-stop-lambda.tf
File metadata and controls
384 lines (334 loc) · 14.2 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# Lambda-based auto-stop for dev servers (works with spot instances)
# CloudWatch EC2 actions don't work reliably with spot instances
locals {
auto_stop_lambda_code = <<-PYTHON
import boto3
import json
import os
SNS_TOPIC = os.environ.get('SNS_TOPIC_ARN', '')
SNS_REGION = os.environ.get('SNS_REGION', os.environ.get('AWS_REGION', 'us-west-1'))
IDLE_HOURS = int(os.environ.get('IDLE_HOURS', '8'))
# A five-minute window with CPU at or above this keeps the instance alive. 5 suits the metal boxes (a short compile is
# a real burst). An instance with a high IDLE floor needs more: an idle Windows desktop with its streaming agent sits
# at about 6% and peaks near 12%, so at 5 it can never be called idle (wbox ran 4 days).
CPU_THRESHOLD = float(os.environ.get('CPU_THRESHOLD', '5.0'))
CHECK_IO = os.environ.get('CHECK_IO', 'false').lower() == 'true'
# Instance-store activity is direct evidence that the scratch disk is in use, so a
# single MiB in any five-minute period is meaningful. Network needs a higher bar:
# Ubuntu's apt timers can download package indexes in the background, and that must
# not keep an otherwise idle box alive forever. NFS work also hits the disk unless a
# read is fully cached, where 64 MiB of egress is still meaningful activity.
DISK_ACTIVITY_BYTES = int(os.environ.get('DISK_ACTIVITY_BYTES', '1048576'))
NETWORK_ACTIVITY_BYTES = int(os.environ.get('NETWORK_ACTIVITY_BYTES', '67108864'))
ec2 = boto3.client('ec2')
sns = boto3.client('sns', region_name=SNS_REGION)
cloudwatch = boto3.client('cloudwatch')
def lambda_handler(event, context):
"""
Check dev server activity and stop if idle.
Called by CloudWatch Events rule every hour.
"""
instance_ids = os.environ.get('INSTANCE_IDS', '').split(',')
instance_ids = [i.strip() for i in instance_ids if i.strip()]
if not instance_ids:
print("No instance IDs configured")
return {'statusCode': 200, 'body': 'No instances to check'}
results = []
for instance_id in instance_ids:
result = check_and_stop_instance(instance_id)
results.append(result)
return {'statusCode': 200, 'body': json.dumps(results)}
def check_and_stop_instance(instance_id):
"""Check if instance is idle and stop it if so."""
try:
# Get instance state
response = ec2.describe_instances(InstanceIds=[instance_id])
if not response['Reservations']:
return {'instance': instance_id, 'status': 'not_found'}
instance = response['Reservations'][0]['Instances'][0]
state = instance['State']['Name']
if state != 'running':
return {'instance': instance_id, 'status': f'already_{state}'}
# Get instance name
name = 'unknown'
for tag in instance.get('Tags', []):
if tag['Key'] == 'Name':
name = tag['Value']
break
# Check CPU utilization over last IDLE_HOURS (but only since instance started)
import datetime
end_time = datetime.datetime.utcnow()
start_time = end_time - datetime.timedelta(hours=IDLE_HOURS)
# Get instance launch time - only count metrics since instance started
launch_time = instance.get('LaunchTime')
if launch_time:
# LaunchTime is timezone-aware, convert to naive UTC for comparison
launch_time_utc = launch_time.replace(tzinfo=None)
if launch_time_utc > start_time:
# Instance started less than IDLE_HOURS ago
hours_running = (end_time - launch_time_utc).total_seconds() / 3600
return {'instance': instance_id, 'name': name, 'status': 'too_new',
'hours_running': round(hours_running, 1), 'required_hours': IDLE_HOURS}
start_time = max(start_time, launch_time_utc)
metrics = cloudwatch.get_metric_statistics(
Namespace='AWS/EC2',
MetricName='CPUUtilization',
Dimensions=[{'Name': 'InstanceId', 'Value': instance_id}],
StartTime=start_time,
EndTime=end_time,
Period=300, # 5 minutes - catches short compile bursts on 64-core metal
Statistics=['Maximum'] # Use max, not avg - a 5min compile burst should count
)
datapoints = metrics.get('Datapoints', [])
# With 5-min periods over IDLE_HOURS, expect IDLE_HOURS*12 datapoints
# Require at least IDLE_HOURS worth (one per hour minimum)
if len(datapoints) < IDLE_HOURS:
return {'instance': instance_id, 'name': name, 'status': 'insufficient_data',
'datapoints': len(datapoints)}
# Check if ANY 5-min window had CPU >= CPU_THRESHOLD
max_per_period = [d['Maximum'] for d in datapoints]
peak_cpu = max(max_per_period)
if peak_cpu >= CPU_THRESHOLD:
return {'instance': instance_id, 'name': name, 'status': 'active',
'peak_cpu': peak_cpu}
# The ordinary metal dev boxes preserve their established CPU-only behavior.
# The shared I/O box opts into network + instance-store activity checks: NFS can
# move real data while using less than 5% of two CPUs, so CPU alone is unsafe.
peak_io_bytes = 0
io_peaks = {}
if CHECK_IO:
metric_thresholds = {
'NetworkIn': NETWORK_ACTIVITY_BYTES,
'NetworkOut': NETWORK_ACTIVITY_BYTES,
'DiskReadBytes': DISK_ACTIVITY_BYTES,
'DiskWriteBytes': DISK_ACTIVITY_BYTES,
}
active_metrics = []
for metric_name, threshold in metric_thresholds.items():
io_metrics = cloudwatch.get_metric_statistics(
Namespace='AWS/EC2',
MetricName=metric_name,
Dimensions=[{'Name': 'InstanceId', 'Value': instance_id}],
StartTime=start_time,
EndTime=end_time,
Period=300,
Statistics=['Sum']
)
points = io_metrics.get('Datapoints', [])
peak = max([d['Sum'] for d in points], default=0)
io_peaks[metric_name] = round(peak)
peak_io_bytes = max(peak_io_bytes, peak)
if peak >= threshold:
active_metrics.append(metric_name)
if active_metrics:
return {'instance': instance_id, 'name': name, 'status': 'active_io',
'peak_cpu': peak_cpu, 'peak_io_bytes_5m': round(peak_io_bytes),
'active_metrics': active_metrics, 'io_peaks': io_peaks}
# Instance is idle - no five-minute window crossed either activity threshold.
print(f"Stopping idle instance {instance_id} ({name}), peak CPU: {peak_cpu:.2f}%, peak I/O: {peak_io_bytes:.0f} bytes/5m")
try:
ec2.stop_instances(InstanceIds=[instance_id])
notify(f"Auto-stopped {name}",
f"Instance {instance_id} ({name}) was idle for {IDLE_HOURS}+ hours "
f"(peak CPU: {peak_cpu:.2f}%, peak I/O: {peak_io_bytes:.0f} bytes/5m) and has been stopped.")
return {'instance': instance_id, 'name': name, 'status': 'stopped',
'peak_cpu': peak_cpu, 'peak_io_bytes_5m': round(peak_io_bytes)}
except Exception as e:
error_msg = str(e)
print(f"Failed to stop {instance_id}: {error_msg}")
notify(f"FAILED to auto-stop {name}",
f"Instance {instance_id} ({name}) is idle but FAILED to stop!\n\nError: {error_msg}\n\nPlease stop it manually.")
return {'instance': instance_id, 'name': name, 'status': 'stop_failed', 'error': error_msg}
except Exception as e:
print(f"Error checking {instance_id}: {e}")
return {'instance': instance_id, 'status': 'error', 'error': str(e)}
def notify(subject, message):
"""Send SNS notification."""
if not SNS_TOPIC:
print(f"No SNS topic configured. Would send: {subject}")
return
try:
sns.publish(
TopicArn=SNS_TOPIC,
Subject=subject,
Message=message
)
print(f"Sent notification: {subject}")
except Exception as e:
print(f"Failed to send notification: {e}")
PYTHON
}
# IAM role for Lambda
resource "aws_iam_role" "dev_auto_stop" {
name = "dev-auto-stop-lambda-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = { Service = "lambda.amazonaws.com" }
}]
})
tags = { Name = "dev-auto-stop-lambda-role" }
}
# Lambda basic execution (CloudWatch Logs)
resource "aws_iam_role_policy_attachment" "dev_auto_stop_basic" {
role = aws_iam_role.dev_auto_stop.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
# Policy for EC2 and SNS access
resource "aws_iam_role_policy" "dev_auto_stop" {
name = "dev-auto-stop-policy"
role = aws_iam_role.dev_auto_stop.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "EC2Describe"
Effect = "Allow"
Action = [
"ec2:DescribeInstances",
"ec2:DescribeInstanceStatus"
]
Resource = "*"
},
{
Sid = "EC2StopDevServers"
Effect = "Allow"
Action = "ec2:StopInstances"
Resource = [
"arn:aws:ec2:us-west-1:${data.aws_caller_identity.current.account_id}:instance/*",
"arn:aws:ec2:us-west-2:${data.aws_caller_identity.current.account_id}:instance/*",
]
Condition = {
StringLike = {
"ec2:ResourceTag/Name" = ["fcvm-metal-*", "x86-*", "io-box", "wbox"]
}
}
},
{
Sid = "CloudWatchMetrics"
Effect = "Allow"
Action = [
"cloudwatch:GetMetricStatistics",
"cloudwatch:GetMetricData"
]
Resource = "*"
},
{
Sid = "SNSPublish"
Effect = "Allow"
Action = "sns:Publish"
Resource = aws_sns_topic.cost_alerts.arn
}
]
})
}
# Lambda function
resource "aws_lambda_function" "dev_auto_stop" {
function_name = "dev-auto-stop"
role = aws_iam_role.dev_auto_stop.arn
handler = "index.lambda_handler"
runtime = "python3.12"
timeout = 60
memory_size = 128
filename = data.archive_file.dev_auto_stop.output_path
source_code_hash = data.archive_file.dev_auto_stop.output_base64sha256
environment {
variables = {
INSTANCE_IDS = join(",", compact([
var.enable_firecracker_instance ? aws_instance.firecracker_dev[0].id : "",
var.enable_x86_dev_instance ? aws_instance.x86_dev[0].id : ""
]))
SNS_TOPIC_ARN = aws_sns_topic.cost_alerts.arn
SNS_REGION = var.aws_region
IDLE_HOURS = "12"
}
}
tags = { Name = "dev-auto-stop" }
}
# Package Lambda code
data "archive_file" "dev_auto_stop" {
type = "zip"
output_path = "${path.module}/.terraform/dev-auto-stop.zip"
source {
content = local.auto_stop_lambda_code
filename = "index.py"
}
}
# CloudWatch Events rule - run every hour
resource "aws_cloudwatch_event_rule" "dev_auto_stop" {
name = "dev-auto-stop-hourly"
description = "Check dev servers for idle and auto-stop"
schedule_expression = "rate(1 hour)"
tags = { Name = "dev-auto-stop-hourly" }
}
resource "aws_cloudwatch_event_target" "dev_auto_stop" {
rule = aws_cloudwatch_event_rule.dev_auto_stop.name
target_id = "dev-auto-stop-lambda"
arn = aws_lambda_function.dev_auto_stop.arn
}
resource "aws_lambda_permission" "dev_auto_stop" {
statement_id = "AllowEventBridge"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.dev_auto_stop.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.dev_auto_stop.arn
}
# The same 12-hour stop lifecycle for the us-west-2 I/O box. Lambda's EC2 and
# CloudWatch clients are regional, so this must run beside the instance rather than
# adding a west-2 ID to the west-1 function. The shared source opts into network and
# instance-store byte checks here; the existing metal boxes remain CPU-only.
resource "aws_lambda_function" "io_box_auto_stop" {
provider = aws.west2
function_name = "io-box-auto-stop"
role = aws_iam_role.dev_auto_stop.arn
handler = "index.lambda_handler"
runtime = "python3.12"
timeout = 60
memory_size = 128
filename = data.archive_file.dev_auto_stop.output_path
source_code_hash = data.archive_file.dev_auto_stop.output_base64sha256
environment {
variables = {
INSTANCE_IDS = aws_instance.io_box.id
SNS_TOPIC_ARN = aws_sns_topic.cost_alerts.arn
SNS_REGION = var.aws_region
IDLE_HOURS = "12"
CHECK_IO = "true"
DISK_ACTIVITY_BYTES = "1048576"
NETWORK_ACTIVITY_BYTES = "67108864"
}
}
tags = { Name = "io-box-auto-stop" }
}
resource "aws_cloudwatch_event_rule" "io_box_auto_stop" {
provider = aws.west2
name = "io-box-auto-stop-hourly"
description = "Stop the shared I/O box after 12 hours without CPU, network, or disk activity"
schedule_expression = "rate(1 hour)"
tags = { Name = "io-box-auto-stop-hourly" }
}
resource "aws_cloudwatch_event_target" "io_box_auto_stop" {
provider = aws.west2
rule = aws_cloudwatch_event_rule.io_box_auto_stop.name
target_id = "io-box-auto-stop-lambda"
arn = aws_lambda_function.io_box_auto_stop.arn
}
resource "aws_lambda_permission" "io_box_auto_stop" {
provider = aws.west2
statement_id = "AllowEventBridge"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.io_box_auto_stop.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.io_box_auto_stop.arn
}
# Output for testing
output "dev_auto_stop_lambda_arn" {
description = "ARN of the dev auto-stop Lambda"
value = aws_lambda_function.dev_auto_stop.arn
}
output "io_box_auto_stop_lambda_arn" {
description = "ARN of the us-west-2 I/O box auto-stop Lambda"
value = aws_lambda_function.io_box_auto_stop.arn
}