Is there a way to get the EC2 instances which belongs to an EKS cluster via AWS SDK library?

547 Views Asked by At

I created a function that will generate reports corresponding to vulnerabilities found in EC2 instances and push them to a bucket in S3 by using the SDK library for Node.js, I'm currently using the Inspector2 module for that task and everything works properly. But now I need to create a filter in the reports to match only the EC2 instances that belongs to EKS clusters. So I decided to take a look at the EKS client module but neither the describeCluster nor the listClusters functions give information about the instances included in the different clusters. So after investigating I found a function in the EKS client that retrieves the node groups corresponding to an EKS cluster and its autoscaling groups associated, and with that information I think I can use the Auto-scaling client to describe the auto scaling groups and get information about the instances which belongs to those groups. But I'm not sure if that would work and I think putting all that pieces together will cost much time. So if someone knows about a better way to achieve this kind of task I would appreciate so much. Thanks in advance.

1

There are 1 best solutions below

0
Jeremy Cowan On

If you use eksctl to provision your clusters, you can interrogate the instance's tags to get the cluster they belong to. Instances provisioned by eksctl will include the keys kubernetes.io/cluster/<CLUSTER_NAME> and eks:cluster-name (the value of this key is set to the CLUSTER_NAME).