Terraform provisioner timeout - last error: dial tcp

Hello Team,
When i use terraform provisioner “file”. at that time It will be given error timeout
Anyone can please help me on this.
Below mentioned the code I use.

resource "aws_instance" "jenkins_server" {
   instance_type = var.instance_type
   ami = var.ami
   subnet_id = var.public_subnet
   vpc_security_group_ids = [var.security_group]
   key_name = "testaccount"
   # user_data = "${file("${path.module}/install_jenkins.sh")}"  
  connection {
     type = "ssh"
     port = "22"
     host = "self.public_ip"
     user = "ubuntu"
     private_key = file("testaccount.pem")
     timeout = "2m"
   }
   provisioner "file" {
     source = "/mnt/terraform-jenkins-main/certificate"
     destination = "/tmp"
   }
   tags = {
      Name = "jenkins_server"
   }

Hi @asd99557!

You have specified the hostname as literally “self.public_ip”, rather than the value of that symbol, and so I think Terraform is trying to find a DNS hostname called self.public_ip on your network and if course then failing because there is no such domain.

To use the dynamically-chosen IP address of the instance you will need to write the reference to self.public_ip without the quotes:

    host = self.public_ip

Terraform will then understand this as a reference to a symbol rather than as a literal string value.


Note that provisioners are a last resort and so I would recommend against using the file provisioner to achieve this result.

Instead, you can use the user_data argument you currently have commented out to both send the file to the instance and run a particular script, by setting user_data to contain Cloud Config YAML that will be handled by the software cloud-init that is preinstalled installed in your AMI.

Cloud-init is external software installed in most standard AMIs and not directly part of Terraform, but you can use it with Terraform by constructing cloud config YAML inside your Terraform configuration and submitting it in the user_data argument.