<p>I'm working on writing custom controller for our kubernetes cluster and that'll listen to node events and perform some operation on the node.I'm using kubernetes client-go library and able to capture kubernetes events whenever a node is attached or removed from the cluster. But is it possible to get AWS instance details of kubernetes node that has been created like instance id, tags etc ? Thanks in advance.</p>

<p>PS: I have installed the kubernetes cluster using kops</p>

<p>On a Kubernetes node in AWS, you'll have some things populated as part of the node labels and various other parts of the node's metadata:</p>

<pre><code>kubectl get nodes -o json | jq '.items[].metadata.labels'

{
  "beta.kubernetes.io/arch": "amd64",
  "beta.kubernetes.io/instance-type": "c5.large",
  "beta.kubernetes.io/os": "linux",
  "failure-domain.beta.kubernetes.io/region": "us-east-1",
  "failure-domain.beta.kubernetes.io/zone": "us-east-1b",
  "kubernetes.io/hostname": "&lt;hostname&gt;",
  "kubernetes.io/role": "master",
  "manufacturer": "amazon_ec2",
  "node-role.kubernetes.io/master": "",
  "operatingsystem": "centos",
  "tier": "production",
  "virtual": "kvm"
}
</code></pre>

<p>The node information is in <code>client-go</code> in the <a href="https://github.com/kubernetes/client-go/blob/master/kubernetes/typed/core/v1/node.go" rel="nofollow noreferrer">node package here</a> using the <code>Get</code> method. Here's an example:</p>

<pre><code>  client := kubernetes.NewForConfigOrDie(config)

  list, err := client.CoreV1().Nodes().List(metav1.ListOptions{})
  if err != nil {
    fmt.Fprintf(os.Stderr, "error listing nodes: %v", err)
    os.Exit(1)
  }

  for _, node := range list.Items {
    fmt.Printf("Node: %s\n", node.Name)
    node, err := client.CoreV1().Nodes().Get(node.Name, metav1.GetOptions{})
    if err != nil {
      fmt.Fprintf(os.Stderr, "error getting node: %v", err)
      os.Exit(1)
    }
    fmt.Println(node)
  }
</code></pre>

<p><em>However</em> this is really probably not the way you want to go about it. If you're running this on a kops cluster in AWS, the node your workload is running on already has access to the AWS API and also the <a href="https://aws.amazon.com/iam/faqs/" rel="nofollow noreferrer">IAM role</a> needed to query node data.</p>

<p>With that in mind, please consider using the <a href="https://github.com/aws/aws-sdk-go" rel="nofollow noreferrer">AWS Go SDK</a> instead. You can query EC2 quite easily, here's an <a href="https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/go/example_code/ec2/describing_instances.go" rel="nofollow noreferrer">adapted example</a>:</p>

<pre><code>package main

import (
    "fmt"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/ec2"
)

func main() {
    // Load session from shared config
    sess := session.Must(session.NewSessionWithOptions(session.Options{
        SharedConfigState: session.SharedConfigEnable,
    }))

    // Create new EC2 client
    ec2Svc := ec2.New(sess)

    // Call to get detailed information on each instance
    result, err := ec2Svc.DescribeInstances(nil)
    if err != nil {
        fmt.Println("Error", err)
    } else {
        fmt.Println("Success", result)
    }
}
</code></pre>
