Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to connect to server via SSH and use remote-exec provisioner.
resource "digitalocean_droplet" "web" {
image = "ubuntu-16-04-x64"
name = "web-1"
region = "sgp1"
size = "512mb"
ssh_keys = [12345]
connection {
type = "ssh"
user = "root"
private_key = "${file("~/.ssh/id_rsa")}"
}
provisioner "remote-exec" {
inline = [
]
}
}
@javavenkats5

This comment has been minimized.

Copy link

commented May 8, 2018

Nice.

@jonatasfreitasv

This comment has been minimized.

Copy link

commented Jul 28, 2018

thx

@scheung38

This comment has been minimized.

Copy link

commented Mar 16, 2019

Hi @toshimaru although for AWS but not sure why

Error: Error applying plan:

1 error(s) occurred:

  • aws_instance.example: timeout - last error: dial tcp 63.35.183.138:22: i/o timeout
resource "aws_instance" "example" {
  ami = "${lookup(var.aws_amis, var.aws_region)}"
  instance_type = "t2.micro"
 
  connection {
    type = "ssh"
    user = "root" // "ec2-user"?
    private_key = "${file("~/.ssh/terraform")}"
    timeout = "2m"
//    agent = false . // true?
  }

  provisioner "remote-exec" {
    inline = [
    ]
  }
}
@mkempster

This comment has been minimized.

Copy link

commented Sep 8, 2019

Error: Error applying plan:

1 error(s) occurred:

* aws_instance.example: timeout - last error: dial tcp 63.35.183.138:22: i/o timeout

I needed a security-group that allowed ingress on port 22 in to the ec2 instance that was created.

resource "aws_security_group" "port_22_ingress_globally_accessible" {
    name = "port_22_ingress_globally_accessible"

    ingress { 
        from_port = 22    
        to_port = 22
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"] // global access! Don't do this for real.
    }
}

I suppose the proper way to do this outside of a learning context is to add a bastion host and ssh through it to your ec2 instance. Terraform seems to have built-in support for using bastion hosts in the remote-exec provisioner, but I can't find a module to create the bastion host. Looks like we're on our own for that task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.