Skip to content

Commit 59fe94c

Browse files
twarnockMark Beacom
authored andcommitted
change to using describe_instances vs _status. linted. (#88)
1 parent c48a438 commit 59fe94c

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
REQUIRES_PYTHON: str = ">=3.7.0"
2626
VERSION: str = "0.1.5"
2727

28-
REQUIRED: List[str] = ["requests", "boto3", "fire", ]
28+
REQUIRED: List[str] = ["requests", "boto3", "fire"]
2929
EXTRAS: Dict[str, List[str]] = {
3030
"test": [
3131
"coverage",

step/lambdas/get_instance_status.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,24 @@ def lambda_handler(event: Dict[str, Any], context: Any) -> str:
2929
instance_name: str = event.get("name", "")
3030
state: str = "unknown"
3131

32-
resp = ec2_client.describe_instance_status(InstanceIds=[instance_id])
33-
for status in resp.get("InstanceStatuses", []):
34-
# check if running
35-
state = status["InstanceState"].get("Name")
36-
if state == "running":
37-
# check instance
38-
if status["InstanceStatus"].get("Status") != "ok":
39-
state = "instance_failed"
40-
41-
# check system
42-
if status["SystemStatus"].get("Status") != "ok":
43-
state = "system_failed"
32+
resp = ec2_client.describe_instances(InstanceIds=[instance_id])
33+
reservations = resp.get("Reservations", [])
34+
for reservation in reservations:
35+
for instance in reservation.get("Instances", []):
36+
state = instance.get("State", {}).get("Name")
37+
38+
# check if running
39+
if state == "running":
40+
resp = ec2_client.describe_instance_status(InstanceIds=[instance_id])
41+
status = resp.get("InstanceStatuses", [])[0]
42+
43+
# check instance
44+
if status["InstanceStatus"].get("Status", "") != "ok":
45+
state = "instance_failed"
46+
47+
# check system
48+
if status["SystemStatus"].get("Status", "") != "ok":
49+
state = "system_failed"
4450

4551
if state == "running":
4652
MigrationStateHandler().update_state(

step/lambdas/update_status.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55

66
import json
77
import os
8-
9-
import boto3
10-
118
from typing import Any, Dict
129

10+
import boto3
1311

1412
print("Loading function update_status")
1513

0 commit comments

Comments
 (0)