Skip to content
This repository was archived by the owner on Oct 23, 2025. It is now read-only.

Commit c429e24

Browse files
lmineirojmcs
authored andcommitted
change in the strategy to report instances (#547)
Use `Name` tag instead of `aws:cloudformation:stack-name` to filter instances.
1 parent a2142b5 commit c429e24

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

senza/cli.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,43 +1271,48 @@ def instances(
12711271
if all:
12721272
filters = []
12731273
else:
1274-
# filter out instances not part of any stack
1275-
filters = [{"Name": "tag-key", "Values": ["aws:cloudformation:stack-name"]}]
1274+
# filter out instances without a Name tag
1275+
filters = [{"Name": "tag-key", "Values": ["Name"]}]
1276+
1277+
# Semantically equivalent to the less efficient client side filtering on != TERMINATED
1278+
if not terminated:
1279+
filters.append(
1280+
{"Name": "instance-state-name", "Values": ["pending", "running", "shutting-down", "stopping", "stopped"]})
12761281

12771282
opt_docker_column = " docker_source" if docker_image else ""
12781283

12791284
for _ in watching(w, watch):
12801285
rows = []
12811286

12821287
for instance in ec2.instances.filter(Filters=filters):
1283-
cf_stack_name = get_tag(instance.tags, "aws:cloudformation:stack-name")
1288+
cf_stack_name = get_tag(instance.tags, "Name")
12841289
stack_name = get_tag(instance.tags, "StackName")
12851290
stack_version = get_tag(instance.tags, "StackVersion")
12861291
if not stack_refs or matches_any(cf_stack_name, stack_refs):
12871292
instance_health = get_instance_health(cf_stack_name, region)
1288-
if instance.state["Name"].upper() != "TERMINATED" or terminated:
1289-
docker_source = (
1290-
get_instance_docker_image_source(instance)
1291-
if docker_image
1292-
else ""
1293-
)
12941293

1295-
rows.append(
1296-
{
1297-
"stack_name": stack_name or "",
1298-
"version": stack_version or "",
1299-
"resource_id": get_tag(
1300-
instance.tags, "aws:cloudformation:logical-id"
1301-
),
1302-
"instance_id": instance.id,
1303-
"public_ip": instance.public_ip_address,
1304-
"private_ip": instance.private_ip_address,
1305-
"state": instance.state["Name"].upper().replace("-", "_"),
1306-
"lb_status": instance_health.get(instance.id),
1307-
"docker_source": docker_source,
1308-
"launch_time": instance.launch_time.timestamp(),
1309-
}
1310-
)
1294+
docker_source = (
1295+
get_instance_docker_image_source(instance)
1296+
if docker_image
1297+
else ""
1298+
)
1299+
1300+
rows.append(
1301+
{
1302+
"stack_name": stack_name or "",
1303+
"version": stack_version or "",
1304+
"resource_id": get_tag(
1305+
instance.tags, "aws:cloudformation:logical-id"
1306+
),
1307+
"instance_id": instance.id,
1308+
"public_ip": instance.public_ip_address,
1309+
"private_ip": instance.private_ip_address,
1310+
"state": instance.state["Name"].upper().replace("-", "_"),
1311+
"lb_status": instance_health.get(instance.id),
1312+
"docker_source": docker_source,
1313+
"launch_time": instance.launch_time.timestamp()
1314+
}
1315+
)
13111316

13121317
rows.sort(key=lambda r: (r["stack_name"], r["version"], r["instance_id"]))
13131318

tests/test_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def my_resource(rtype, *args):
619619
instance.public_ip_address = '8.8.8.8'
620620
instance.private_ip_address = '10.0.0.1'
621621
instance.state = {'Name': 'Test-instance'}
622-
instance.tags = [{'Key': 'aws:cloudformation:stack-name', 'Value': 'test-1'},
622+
instance.tags = [{'Key': 'Name', 'Value': 'test-1'},
623623
{'Key': 'aws:cloudformation:logical-id', 'Value': 'local-id-123'},
624624
{'Key': 'StackName', 'Value': 'test'},
625625
{'Key': 'StackVersion', 'Value': '1'}]

0 commit comments

Comments
 (0)