skip to Main Content

I am seeing the following error:

Failed to parse host: parse "https://"https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xx.us-east-1.eks.amazonaws.com"n": net/url: invalid control character in URL, while performing terraform apply

All I was trying to do is to use a null resource, to execute eks command, and output it to a txt file and using data "local_file", tried passing the returned value to host argument to the provider block below

resource "null_resource" "endpoint" {
  //Trigger everytime
  triggers = {
    build_number = timestamp()
  }
  provisioner "local-exec" {
    command = "aws eks describe-cluster --name ${var.cluster_name} --query cluster.endpoint >> ${path.module}/output.txt"
  }

}

data "local_file" "output" {
  filename   = "${path.module}/outputtxt"
  depends_on = ["null_resource.cluster_endpoint"]
}

provider "kubernetes" {
  host                   = data.local_file.output.content
  cluster_ca_certificate = base64decode(data.local_file.output.certificate)
  exec {
    command     = "aws"
    args        = ["eks", "get-token", "--cluster-name", ${var.cluster_name}]
  }
}

How can I make sure, to pass the cluster endpoint to the provider block using the null resource & local_file data block?

2

Answers


  1. Instead of

    $var.cluster_name
    

    it should be

    "${var.cluster_name}"
    

    or just

    var.cluster_name
    
    Login or Signup to reply.
  2. What you could do is use the data source that exists for EKS. That way you wouldn’t have to worry about the formatting of the file etc. So for example, this is how I would approach configuring the kubernetes provider:

    data "aws_eks_cluster" "eks" {
      name = var.cluster_name
    }
    
    provider "kubernetes" {
      host                   = data.aws_eks_cluster.eks.endpoint
      cluster_ca_certificate = base64decode(data.aws_eks_cluster.eks.certificate_authority[0].data)
      exec {
        api_version = "client.authentication.k8s.io/v1beta1"
        args        = ["eks", "get-token", "--cluster-name", var.cluster_name]
        command     = "aws"
      }
    }
    

    Note that you can use the new HCL syntax without the need to use quotes and $ to reference the variable for the cluster name. One additional thing to note: the command for configuring the kubernetes provider works with AWS CLI v2.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search