Skip to content

Instantly share code, notes, and snippets.

@toshimaru
Last active August 19, 2023 08:09
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save toshimaru/89bf5ab2d3c2c359f07bcdfc835154bb to your computer and use it in GitHub Desktop.
Save toshimaru/89bf5ab2d3c2c359f07bcdfc835154bb to your computer and use it in GitHub Desktop.
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 = [
]
}
}
@NikosSpanos
Copy link

@mkempster I have the same issue with you. And part of the solution was to open ssh traffic to all the internet. A not very secure aware move as you also wrote. Have you found any other alternative to that?

@BertCatsburg
Copy link

2 years late to the party,

Following up on mkempster's code:

data "http" "icanhazip" {
  url = "https://icanhazip.com/"

  request_headers = {
    Accept = "text/*"
  }
}

# The IP Address of my laptop. Pass it to the Security Group ingress-rule, to restict SSH Access to the Instance
variable "my_ip" {
	type = string
	default = chomp(data.http.icanhazip.response_body)
}

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 = [var.my_ip] // IP of my own laptop
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment