How to Generate a Mock EKS Kubeconfig in Python

Generate a minimal kubeconfig dict with a mock EKS cluster entry and dump it to YAML using PyYAML.

Easy Python 3.9+ Aug 9, 2026 Cloud + Python 15 views 0 copies

Requires third-party packages — install first
pip install pyyaml

Python code

43 lines
Python 3.9+
import yaml
from pathlib import Path


def mock_eks_kubeconfig(cluster_name: str) -> dict:
    """Return a minimal kubeconfig dict with a mock EKS cluster entry."""
    return {
        "apiVersion": "v1",
        "kind": "Config",
        "clusters": [
            {
                "name": f"arn:aws:eks:us-east-1:123456789012:cluster/{cluster_name}",
                "cluster": {
                    "server": f"https://{cluster_name}.eks.amazonaws.com",
                    "certificate-authority-data": "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t",
                },
            }
        ],
        "contexts": [
            {
                "name": f"{cluster_name}@arn:aws:eks:us-east-1:123456789012:cluster/{cluster_name}",
                "context": {
                    "cluster": f"arn:aws:eks:us-east-1:123456789012:cluster/{cluster_name}",
                    "user": f"{cluster_name}@arn:aws:eks:us-east-1:123456789012:cluster/{cluster_name}",
                },
            }
        ],
        "current-context": f"{cluster_name}@arn:aws:eks:us-east-1:123456789012:cluster/{cluster_name}",
        "users": [
            {
                "name": f"{cluster_name}@arn:aws:eks:us-east-1:123456789012:cluster/{cluster_name}",
                "user": {"token": "mock-token-for-testing"},
            }
        ],
    }


if __name__ == "__main__":
    kubeconfig = mock_eks_kubeconfig("my-cluster")
    output_path = Path("mock_kubeconfig.yaml")
    with output_path.open("w") as f:
        yaml.safe_dump(kubeconfig, f, default_flow_style=False)
    print(output_path.resolve())

Output

stdout
/path/to/mock_kubeconfig.yaml

How it works

The function constructs a dictionary that mirrors the structure of a real EKS kubeconfig, including clusters, contexts, users, and the current-context field. It uses an AWS account ID placeholder and a fake certificate authority data string to keep the mock lightweight. The yaml.safe_dump handler writes the dictionary to a YAML file in a readable block format. Wrapping the write in a context manager ensures the file is closed properly even if an error occurs. This is useful for tests or local development when you need to point kubectl at a fake cluster.

Common mistakes

  • Forgetting to install PyYAML with `pip install pyyaml`
  • Using `yaml.dump` instead of `yaml.safe_dump` can allow unsafe YAML loading
  • Not including the `certificate-authority-data` field can cause kubectl to fail
  • Overwriting an existing kubeconfig without backing it up

Variations

  1. Use `json.dumps` to write a JSON kubeconfig instead of YAML
  2. Accept cluster name and AWS region as command-line arguments for reuse

Real-world use cases

  • Creating a fake kubeconfig for unit tests that mock Kubernetes API interactions.
  • Generating placeholder configs for local development environments without a real EKS cluster.
  • Supplying a test kubeconfig to CI pipelines for integration testing of CLI tooling.

Sponsored

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Cloud + Python

Related tutorials and quizzes for this topic.