Using Boto3-style clients (fakes provided by the harness), find running instances whose average CPU over
the period is below threshold percent.
ec2.describe_instances(NextToken=...) returns {"Reservations": [{"Instances": [...]}], "NextToken": ...}
(NextToken absent on the last page). Each instance has InstanceId, State: {"Name": ...} and
optional Tags: [{"Key", "Value"}].cloudwatch.get_average_cpu(instance_id) returns a float, or None when there is no data.Skip instances that aren't running, that have the tag keep-alive = true, or that have no CPU data.
Return the sorted list of idle instance ids. The harness counts your calls: don't fetch metrics for
instances you will skip.
Implement idle_instances. Input: ec2, cloudwatch (fake clients), threshold: float. Returns: list[str].
This code runs before yours (fake clients, clocks and the test harness). You can use its names; don't edit them.
class FakeEC2:
def __init__(self, pages):
self.pages, self.calls = pages, 0
def describe_instances(self, NextToken=None):
self.calls += 1
i = 0 if NextToken is None else int(NextToken)
page = {"Reservations": self.pages[i]}
if i + 1 < len(self.pages):
page["NextToken"] = str(i + 1)
return page
class FakeCloudWatch:
def __init__(self, cpu):
self.cpu, self.calls = cpu, []
def get_average_cpu(self, instance_id):
self.calls.append(instance_id)
return self.cpu.get(instance_id)
def __harness__(fn, pages, cpu, threshold):
ec2, cw = FakeEC2(pages), FakeCloudWatch(cpu)
return {"idle": fn(ec2, cw, threshold), "describe_calls": ec2.calls, "metric_calls": sorted(cw.calls)}
def idle_instances(ec2, cloudwatch, threshold: float) -> list[str]:
page = ec2.describe_instances()
return []
Run executes the public tests. Submit grades against public and hidden tests.
Using Boto3-style clients (fakes provided by the harness), find running instances whose average CPU over
the period is below threshold percent.
ec2.describe_instances(NextToken=...) returns {"Reservations": [{"Instances": [...]}], "NextToken": ...}
(NextToken absent on the last page). Each instance has InstanceId, State: {"Name": ...} and
optional Tags: [{"Key", "Value"}].cloudwatch.get_average_cpu(instance_id) returns a float, or None when there is no data.Skip instances that aren't running, that have the tag keep-alive = true, or that have no CPU data.
Return the sorted list of idle instance ids. The harness counts your calls: don't fetch metrics for
instances you will skip.
Implement idle_instances. Input: ec2, cloudwatch (fake clients), threshold: float. Returns: list[str].
This code runs before yours (fake clients, clocks and the test harness). You can use its names; don't edit them.
class FakeEC2:
def __init__(self, pages):
self.pages, self.calls = pages, 0
def describe_instances(self, NextToken=None):
self.calls += 1
i = 0 if NextToken is None else int(NextToken)
page = {"Reservations": self.pages[i]}
if i + 1 < len(self.pages):
page["NextToken"] = str(i + 1)
return page
class FakeCloudWatch:
def __init__(self, cpu):
self.cpu, self.calls = cpu, []
def get_average_cpu(self, instance_id):
self.calls.append(instance_id)
return self.cpu.get(instance_id)
def __harness__(fn, pages, cpu, threshold):
ec2, cw = FakeEC2(pages), FakeCloudWatch(cpu)
return {"idle": fn(ec2, cw, threshold), "describe_calls": ec2.calls, "metric_calls": sorted(cw.calls)}
def idle_instances(ec2, cloudwatch, threshold: float) -> list[str]:
page = ec2.describe_instances()
return []
Run executes the public tests. Submit grades against public and hidden tests.