From 8679199ef9b6676cd385b7bb2197763944e294cb Mon Sep 17 00:00:00 2001 From: Mark Boyd Date: Mon, 23 Feb 2026 12:36:50 -0500 Subject: [PATCH] fix cost estimator script to not double count database replicas and to get service plan names more reliably --- cost-estimator/estimate-costs.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/cost-estimator/estimate-costs.py b/cost-estimator/estimate-costs.py index 930ba931..35e589bc 100755 --- a/cost-estimator/estimate-costs.py +++ b/cost-estimator/estimate-costs.py @@ -36,17 +36,11 @@ def get_instance_plan_name(self, instance_guid): return cf_data.get("included", "N/A")["service_plans"][0]["name"] def get_service_plan_name(self, instance_guid): - # FIXME: Maybe we shouldn't trust the plan name in the tag, but it's faster service_plan_name = "Not_Found" try: - service_plan_name = [ - tag["Value"] for tag in self.tags if tag["Key"] == "Service plan name" - ][0] + service_plan_name = self.get_instance_plan_name(instance_guid) except: - try: - service_plan_name = self.get_instance_plan_name(instance_guid) - except: - service_plan_name = "Not_Found" + service_plan_name = "Not_Found" return service_plan_name @@ -292,9 +286,15 @@ def get_aws_instances(self, client, resource_type): def get_rds_instances(self, client): response = self.get_aws_instances(client, "rds") + rds_instance_guids = [] for resource in response["ResourceTagMappingList"]: rds = Rds(resource["ResourceARN"], resource["Tags"]) - self.rds_instances.append(rds) + # replica databases will appear twice in the list of + # tagged resources, but they should only be tracked once + # for cost purposes + if rds.instance_guid not in rds_instance_guids: + self.rds_instances.append(rds) + rds_instance_guids.append(rds.instance_guid) def get_redis_instances(self, client): response = self.get_aws_instances(client, "redis") @@ -357,6 +357,8 @@ def report_rds(self, tags_client, reporter): self.get_rds_instances(tags_client) for rds in self.rds_instances: rds.get_db_instance(rds_client) + if "replica" in rds.service_plan_name: + print(rds.instance_guid) self.rds_instance_plans[rds.service_plan_name] += 1 self.rds_allocation += rds.allocated_storage