Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@noushi
Forked from smford22/main.tf
Created January 26, 2022 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noushi/46aa57e0621c21e289a01ea4fe3fff5b to your computer and use it in GitHub Desktop.
Save noushi/46aa57e0621c21e289a01ea4fe3fff5b to your computer and use it in GitHub Desktop.
terraform GCP remote exec
/*
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")}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment