0

I am having trouble to use the provisioner (both "file" and "remote-exec") with aws lightsail. For the "file" provisioner, I kept getting a dialup error to port 22 with connection refused, the "remote-exec" gives me a timeout error. I can see it keeps trying to connect to the instance but it just can not connect to it.

For the file provisioner, I have also tried with scp directly and it works just fine.

A sample snippet of the connection block I am using is as the following:

resource "aws_lightsail_instance" "han-mongo" {
  name              = "han-mongo"
  availability_zone = "us-east-1b"
  blueprint_id      = "ubuntu_16_04"
  bundle_id         = "nano_1_0"
  key_pair_name     = "my_key_pair"
  user_data         = "${file("userdata.sh")}" 

  provisioner "file" {
         source = "file.service"
         destination = "/home/ubuntu"
         connection {
            type = "ssh"
            private_key =  "${file("my_key.pem")}"
            user = "ubuntu"
            timeout = "20s"
        }
  }
}
Chen
  • 1,566
  • 1
  • 12
  • 16
  • To answer this question it would help to see more of your config. In particular the `resource "aws_lightsail_instance` surrounding this provisioner and the `resource "aws_lightsail_key_pair"` it refers to. (with any sensitive info redacted, of course!) – Martin Atkins May 15 '17 at 16:44
  • @MartinAtkins I updated the script. in fact, I did not use "aws_lightsail_key_pair" I have created a key pair in the web console and use it directly in the script. e.g my_key_pair, the private key for this key pair is my_key.pem. – Chen May 15 '17 at 20:16

1 Answers1

1

In addition to the authentication information, it's also necessary to tell Terraform which IP address it should use to connect, like this:

connection {
  type        = "ssh"
  host        = "${self.public_ip_address}"
  private_key = "${file("my_key.pem")}"
  user        = "ubuntu"
  timeout     = "20s"
}

For some resources Terraform is able to automatically infer some of the connection details from the resource attributes, but at present that is not supported for Lightsail instances and so it's necessary to specify the host argument explicitly.

Martin Atkins
  • 62,420
  • 8
  • 120
  • 138