Terraform remote_exec that runs kubectl fails?

Viewed 29

Terraform v1.2.0

I have a generic module that just copies a passed in YAML file name, where that file exists in the same directory as the module, copies it onto the /tmp directory on the remote node, and then executes kubectl apply -f <yaml file>. kubectl is installed on the node; I can run the command manually on the node.

// Copy local file to remote location
resource "null_resource" "copy_script" {
  connection {
    type        = "ssh"
    user        = "ubuntu"
    private_key = file(var.ssh_key_file)
    host        = var.ec2_pub_ip
  }

  provisioner "file" {
    source      = "../modules/nginx/${var.conf_file}"
    destination = "/tmp/${var.conf_file}"
  }
}

// Run kubectl apply on the passed-in YAML
// configuration file.
resource "null_resource" "kubectl_apply" {
  connection {
    type        = "ssh"
    user        = "ubuntu"
    private_key = file(var.ssh_key_file)
    host        = var.ec2_pub_ip
  }

  provisioner "remote-exec" {
    inline = [
      "sudo kubectl apply -f /tmp/${var.conf_file}"
    ]
  }
}

I call the above module this way

// Install nginx deployment
module "k8s_install_nginx_deploy" {
  source                 = "../modules/nginx"
  ssh_key_file           = var.ssh_key_file
  ec2_pub_ip             = module.k8s_ec2_master_0.ec2_pub_ip
  conf_file              = "nginx-deployment.yaml"
}
// Install nginx service
module "k8s_install_nginx_svc" {
  source                 = "../modules/nginx"
  ssh_key_file           = var.ssh_key_file
  ec2_pub_ip             = module.k8s_ec2_master_0.ec2_pub_ip
  conf_file              = "nginx-service.yaml"
}

However Terraform gives me

╷
│ Error: remote-exec provisioner error
│ 
│   with module.k8s_install_nginx_svc.null_resource.kubectl_apply,
│   on ../modules/nginx/main.tf line 24, in resource "null_resource" "kubectl_apply":
│   24:   provisioner "remote-exec" {
│ 
│ error executing "/tmp/terraform_738306599.sh": Process exited with status 1
╵

I used the same technique running shell scripts, that is, the provisioner just runs a "sudo /tmp/${var.shell_script}" and it works.

What am I missing? TIA

0 Answers
Related