fix: AWS VM counts treat terminated instances as running and misclassify Karpenter nodes#73
Open
amanfredi wants to merge 1 commit into
Conversation
…ify Karpenter nodes is_vm_running() counted every instance whose state was not 'stopped' as running, including 'terminated' and 'shutting-down'. DescribeInstances keeps terminated instances visible for up to ~1 hour, so accounts with high instance churn (e.g. Karpenter or Spot autoscaling) report hundreds of phantom running VMs. Only 'pending' and 'running' states now count as running. Karpenter-launched EKS nodes are tagged with eks:eks-cluster-name and karpenter.sh/* rather than the managed-nodegroup tags in EKS_TAGS, so they were counted in the VM columns instead of the Kubernetes node columns. The Karpenter tag keys are now recognized. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two related counting bugs in
AWS/aws_cspm_benchmark.pycause large overcounts of running VMs in accounts with high EC2 instance churn (Karpenter / Spot autoscaling):Terminated instances are counted as running.
is_vm_running()returnedstate != "stopped", which classifiesterminatedandshutting-downinstances as running.DescribeInstanceskeeps terminated instances visible for up to ~1 hour after termination, so any account that churns instances quickly carries a standing pool of terminated instances that all land in thevms_runningcolumn. Fixed by counting onlypendingandrunningstates as running.Karpenter-launched EKS nodes are classified as VMs, not Kubernetes nodes.
EKS_TAGSonly contains managed-nodegroup/eksctl tag keys (eks:cluster-name,aws:eks:cluster-name,eks:nodegroup-name,alpha.eksctl.io/nodegroup-type). Karpenter-launched nodes instead carryeks:eks-cluster-nameandkarpenter.sh/*tags, so they fall into the VM columns. Fixed by addingeks:eks-cluster-name,karpenter.sh/nodepool(Karpenter v1beta1/v1), andkarpenter.sh/provisioner-name(legacy Karpenter) toEKS_TAGS.The two bugs compound: every recently-terminated Karpenter node is reported as a running VM.
Real-world impact
Observed in an AWS account running EKS with Karpenter (short-lived nodes, high churn). The EC2 console showed ~150 running instances, while the script reported 468 running VMs + 212 running Kubernetes nodes. Comparing old vs. fixed logic against the same live
DescribeInstancessnapshot:Since these counts feed Falcon sizing/licensing estimates, the overcount is nearly 3x for this account.
Notes
*_terminatedcolumns now include instances interminated,shutting-down,stopping, andstoppedstates (previously onlystopped), which matches the column label inAWS/README.md.flake8,pylint, andbanditpass with the same configuration as CI (the one pre-existingR1720pylint hint onmainis untouched).🤖 Generated with Claude Code