Skip to content

Commit

Permalink
resolver: use ssm describe_instance_information instead of get_inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
andreclaro committed Oct 11, 2024
1 parent 8006575 commit 923f4fd
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions ssm_tools/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,22 @@ def _try_append(_list: list, _dict: dict, _key: str) -> None:
items = {}

# List instances from SSM
logger.debug("Fetching SSM inventory")
paginator = self.ssm_client.get_paginator("get_inventory")
logger.debug("Fetching SSM instance information")
paginator = self.ssm_client.get_paginator("describe_instance_information")
response_iterator = paginator.paginate(
Filters=[
{"Key": "AWS:InstanceInformation.ResourceType", "Values": ["EC2Instance", "ManagedInstance"], "Type": "Equal"},
{"Key": "AWS:InstanceInformation.InstanceStatus", "Values": ["Terminated", "Stopped", "ConnectionLost"], "Type": "NotEqual"},
{"Key": "PingStatus", "Values": ["Online"]},
]
)

for inventory in response_iterator:
for entity in inventory["Entities"]:
logger.debug(entity)
content = entity["Data"]["AWS:InstanceInformation"]["Content"][0]
instance_id = content["InstanceId"]
for page in response_iterator:
for instance in page["InstanceInformationList"]:
instance_id = instance["InstanceId"]
items[instance_id] = {
"InstanceId": instance_id,
"InstanceName": "",
"HostName": content.get("ComputerName", ""),
"Addresses": [content.get("IpAddress")],
"HostName": instance.get("Name", ""),
"Addresses": [instance.get("IPAddress")],
}
logger.debug("Added instance: %s: %r", instance_id, items[instance_id])

Expand Down

0 comments on commit 923f4fd

Please sign in to comment.