2015-08-02 17:37:52 +02:00
|
|
|
# See https://cloud.google.com/compute/docs/load-balancing/network/example
|
|
|
|
|
|
|
|
provider "google" {
|
2015-08-31 10:19:02 +02:00
|
|
|
region = "${var.region}"
|
|
|
|
project = "${var.project_name}"
|
2016-03-29 23:18:54 +02:00
|
|
|
credentials = "${file("${var.credentials_file_path}")}"
|
2015-08-02 17:37:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_http_health_check" "default" {
|
2015-08-31 10:19:02 +02:00
|
|
|
name = "tf-www-basic-check"
|
|
|
|
request_path = "/"
|
|
|
|
check_interval_sec = 1
|
|
|
|
healthy_threshold = 1
|
|
|
|
unhealthy_threshold = 10
|
|
|
|
timeout_sec = 1
|
2015-08-02 17:37:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_target_pool" "default" {
|
2015-08-31 10:19:02 +02:00
|
|
|
name = "tf-www-target-pool"
|
|
|
|
instances = ["${google_compute_instance.www.*.self_link}"]
|
|
|
|
health_checks = ["${google_compute_http_health_check.default.name}"]
|
2015-08-02 17:37:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_forwarding_rule" "default" {
|
2015-08-31 10:19:02 +02:00
|
|
|
name = "tf-www-forwarding-rule"
|
|
|
|
target = "${google_compute_target_pool.default.self_link}"
|
|
|
|
port_range = "80"
|
2015-08-02 17:37:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_instance" "www" {
|
2015-08-31 10:19:02 +02:00
|
|
|
count = 3
|
2015-08-02 17:37:52 +02:00
|
|
|
|
2015-08-31 10:19:02 +02:00
|
|
|
name = "tf-www-${count.index}"
|
2016-03-29 23:18:54 +02:00
|
|
|
machine_type = "f1-micro"
|
2015-08-31 10:19:02 +02:00
|
|
|
zone = "${var.region_zone}"
|
|
|
|
tags = ["www-node"]
|
2015-08-02 17:37:52 +02:00
|
|
|
|
2015-08-31 10:19:02 +02:00
|
|
|
disk {
|
2016-03-29 23:18:54 +02:00
|
|
|
image = "ubuntu-os-cloud/ubuntu-1404-trusty-v20160314"
|
2015-08-31 10:19:02 +02:00
|
|
|
}
|
2015-08-02 17:37:52 +02:00
|
|
|
|
2015-08-31 10:19:02 +02:00
|
|
|
network_interface {
|
|
|
|
network = "default"
|
|
|
|
access_config {
|
2016-03-29 23:18:54 +02:00
|
|
|
# Ephemeral
|
2015-08-02 17:37:52 +02:00
|
|
|
}
|
2015-08-31 10:19:02 +02:00
|
|
|
}
|
2015-08-02 17:37:52 +02:00
|
|
|
|
2015-08-31 10:19:02 +02:00
|
|
|
metadata {
|
2016-03-29 23:18:54 +02:00
|
|
|
ssh-keys = "root:${file("${var.public_key_path}")}"
|
|
|
|
}
|
|
|
|
|
|
|
|
provisioner "file" {
|
|
|
|
source = "${var.install_script_src_path}"
|
|
|
|
destination = "${var.install_script_dest_path}"
|
|
|
|
connection {
|
|
|
|
type = "ssh"
|
|
|
|
user = "root"
|
|
|
|
private_key = "${file("${var.private_key_path}")}"
|
|
|
|
agent = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
provisioner "remote-exec" {
|
|
|
|
connection {
|
|
|
|
type = "ssh"
|
|
|
|
user = "root"
|
|
|
|
private_key = "${file("${var.private_key_path}")}"
|
|
|
|
agent = false
|
|
|
|
}
|
|
|
|
inline = [
|
|
|
|
"chmod +x ${var.install_script_dest_path}",
|
|
|
|
"${var.install_script_dest_path} ${count.index}"
|
|
|
|
]
|
2015-08-31 10:19:02 +02:00
|
|
|
}
|
2015-08-02 17:37:52 +02:00
|
|
|
|
2015-08-31 10:19:02 +02:00
|
|
|
service_account {
|
|
|
|
scopes = ["https://www.googleapis.com/auth/compute.readonly"]
|
|
|
|
}
|
2015-08-02 17:37:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_firewall" "default" {
|
2015-08-31 10:19:02 +02:00
|
|
|
name = "tf-www-firewall"
|
|
|
|
network = "default"
|
2015-08-02 17:37:52 +02:00
|
|
|
|
2015-08-31 10:19:02 +02:00
|
|
|
allow {
|
|
|
|
protocol = "tcp"
|
|
|
|
ports = ["80"]
|
|
|
|
}
|
2015-08-02 17:37:52 +02:00
|
|
|
|
2015-08-31 10:19:02 +02:00
|
|
|
source_ranges = ["0.0.0.0/0"]
|
|
|
|
target_tags = ["www-node"]
|
2015-08-02 17:37:52 +02:00
|
|
|
}
|