<p>When using Amazon's K8s offering, the <strong>EKS</strong> service, at some point you need to connect the Kubernetes API and configuration to the infrastructure established within AWS. Especially we need a <em>kubeconfig</em> with proper credentials and URLs to connect to the k8s control plane provided by EKS.</p>

<p>The Amazon commandline tool <code>aws</code> provides a routine for this task</p>

<pre><code>aws eks update-kubeconfig --kubeconfig /path/to/kubecfg.yaml --name &lt;EKS-cluster-name&gt;
</code></pre>

<h2>Question: do the same through Python/boto3</h2>

<p>When looking at the <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/eks.html" rel="noreferrer">Boto API documentation</a>, I seem to be unable to spot the equivalent for the above mentioned <code>aws</code> routine. Maybe I am looking at the wrong place.</p>

<ul>
<li>is there a ready-made function in <em>boto</em> to achieve this?</li>
<li>otherwise how would you approach this directly within python (other than calling out to <code>aws</code> in a subprocess)?</li>
</ul>

<p>There isn't a method function to do this, but you can build the configuration file yourself like this:</p>

<pre><code># Set up the client
s = boto3.Session(region_name=region)
eks = s.client("eks")

# get cluster details
cluster = eks.describe_cluster(name=cluster_name)
cluster_cert = cluster["cluster"]["certificateAuthority"]["data"]
cluster_ep = cluster["cluster"]["endpoint"]

# build the cluster config hash
cluster_config = {
        "apiVersion": "v1",
        "kind": "Config",
        "clusters": [
            {
                "cluster": {
                    "server": str(cluster_ep),
                    "certificate-authority-data": str(cluster_cert)
                },
                "name": "kubernetes"
            }
        ],
        "contexts": [
            {
                "context": {
                    "cluster": "kubernetes",
                    "user": "aws"
                },
                "name": "aws"
            }
        ],
        "current-context": "aws",
        "preferences": {},
        "users": [
            {
                "name": "aws",
                "user": {
                    "exec": {
                        "apiVersion": "client.authentication.k8s.io/v1alpha1",
                        "command": "heptio-authenticator-aws",
                        "args": [
                            "token", "-i", cluster_name
                        ]
                    }
                }
            }
        ]
    }

# Write in YAML.
config_text=yaml.dump(cluster_config, default_flow_style=False)
open(config_file, "w").write(config_text)
</code></pre>
