I wanted to share this solution:
Problem
We have been using Terraform to build Kubernetes clusters using Amazon Elastic Kubernetes Service (EKS) and plagued with intermittent errors below when running terraform init, plan or apply. The errors are specific to using any of the providers:
- hashicorp/kubernetes
- gavinbunney/kubectl
- hashicorp/helm
Here are some examples:
Error : Get “https://ED5369UF1274843BF38BU€DE11111111.gr7.us-east-I . eks . amazonaws . com/api/vl/namespaces/test”: getting credentials: exec: executable aws failed with exit code 1
Error : Kubernetes cluster unreachable: Get “https://ED5369UF1277853BF38BU2DE77777777.gr7.us-east-l.eks.amazonaws.com/versi
on”: getting credentials: exec: executable aws failed with exit code 1
Error : failed to create kubernetes rest client for read of resource: Get “https://3U2D2U6D6DUEFC3U6€BF6A3688888888.gr7.us-east-I.eks.amazonaws.com/api?timeout=32s”: getting credentials: exec: executable aws failed with exit code 1
Solution
The problem was having the wrong configuration for these providers, this is how to resolve the problem.
Use aws_eks_cluster_auth
to retrieve a token, then use this token for your providers. This is what actually resolved the intermittent problems.
Were using Terraform Cloud and retrieve the host and certificate from the state file.
# obtain a cluster token for providers, tokens are short lived (15 minutes)
data "aws_eks_cluster_auth" "cluster_auth" {
name = module.eks-cluster.eks_cluster.name
}
provider "kubernetes" {
host = data.tfe_outputs.this.values.eks_cluster_endpoint
cluster_ca_certificate = base64decode(data.tfe_outputs.this.values.cluster_certificate_authority)
token = data.aws_eks_cluster_auth.cluster_auth.token
}
provider "kubectl" {
host = data.tfe_outputs.this.values.eks_cluster_endpoint
cluster_ca_certificate = base64decode(data.tfe_outputs.this.values.cluster_certificate_authority)
load_config_file = false
token = data.aws_eks_cluster_auth.cluster_auth.token
}
provider "helm" {
kubernetes {
host = data.tfe_outputs.this.values.eks_cluster_endpoint
cluster_ca_certificate = base64decode(data.tfe_outputs.this.values.cluster_certificate_authority)
token = data.aws_eks_cluster_auth.cluster_auth.token
}
}