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.
pip install pyyaml
Python code
43 linesimport 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
/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
- Use `json.dumps` to write a JSON kubeconfig instead of YAML
- 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
More from Cloud + Python
- Build a URL Shortener Client with Python medium
- Create a Cloud Storage Helper Class in Python easy
- Create a Data Helper Class for Beginners in Python easy
- Cross Account Role Chaining Mock Credentials in Python medium
- Exponential Backoff with Jitter for Cloud API Calls in Python medium
- Generate Mock CloudFormation Stack Events in Python easy
Keep learning
Related tutorials and quizzes for this topic.