Created
May 21, 2018 19:15
-
-
Save smford22/54aa5e96701430f1bb0ea6e1a502d23a to your computer and use it in GitHub Desktop.
terraform GCP remote exec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is a test server definition for GCE+Terraform for GH-9564 | |
*/ | |
provider "google" { | |
project = "${var.project}" // Your project ID here. | |
region = "${var.region}" | |
} | |
resource "google_compute_firewall" "gh-9564-firewall-externalssh" { | |
name = "gh-9564-firewall-externalssh" | |
network = "default" | |
allow { | |
protocol = "tcp" | |
ports = ["22"] | |
} | |
source_ranges = ["0.0.0.0/0"] | |
target_tags = ["externalssh"] | |
} | |
resource "google_compute_instance" "dev1" { | |
name = "gcp-rhel7-dev1-tf" | |
machine_type = "f1-micro" | |
zone = "us-central1-a" | |
tags = ["externalssh"] | |
boot_disk { | |
initialize_params { | |
image = "centos-cloud/centos-7" | |
} | |
} | |
network_interface { | |
network = "default" | |
access_config { | |
# Ephemeral | |
} | |
} | |
provisioner "remote-exec" { | |
connection { | |
type = "ssh" | |
user = "${var.user}" | |
timeout = "500s" | |
private_key = "${file("~/.ssh/google_compute_engine")}" | |
} | |
inline = [ | |
"touch /tmp/temp.txt", | |
] | |
} | |
# Ensure firewall rule is provisioned before server, so that SSH doesn't fail. | |
depends_on = ["google_compute_firewall.gh-9564-firewall-externalssh"] | |
service_account { | |
scopes = ["compute-ro"] | |
} | |
metadata { | |
ssh-keys = "USERNAME:${file("~/.ssh/google_compute_engine.pub")}" | |
} | |
} |
@mattnworb I'm trying the same thing with no passphrase but I get key mismatch error even though I'm using a valid key pair.
How syntax do I use for remote-exec provisioner in terraform v0.12 ?
I used below one..
provisioner "file" {
source = "scripts/bootstrap.sh"
destination = "/tmp/bootstrap.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/bootstrap.sh",
"sudo sed -i -e 's/\r$//' /tmp/bootstrap.sh", # Remove the spurious CR characters.
"sudo /tmp/bootstrap.sh",
]
}
connection {
type = "ssh"
host = "${google_compute_instance.vm_instance.network_interface.0.access_config.0.nat_ip}"
user = var.username
private_key = file(var.private_key_path)
}
how to fetch private key path
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe that
connection
requires ahost
field since Terraform 0.12this should work:
also, I believe the SSH key needs to not have a passphrase: