forked from ThoughtWorksInc/infra-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
95 lines (76 loc) · 2.85 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
data "template_file" "hostname" {
count = "${var.instances}"
template = "${var.role}-${count.index + 1}"
}
data "template_file" "fqdn" {
count = "${var.instances}"
template = "${data.template_file.hostname.*.rendered[count.index]}.gcloud-${var.region}.${var.environment}"
}
resource "null_resource" "etcd-discovery" {
provisioner "local-exec" {
command = "curl https://discovery.etcd.io/new?size=${var.instances} > ${format("%s/%s", path.module, var.etcd_discovery_url)}"
}
}
data "template_file" "cloud-config" {
count = "${var.instances}"
template = "${file(format("%s/%s", path.module, var.cloud_config_file))}"
vars {
region = "${var.region}"
etcd_discovery_url = "${file(format("%s/%s", path.module, var.etcd_discovery_url))}"
depends_on = "${null_resource.etcd-discovery.id}"
}
}
resource "google_compute_instance" "thoughtworks" {
count = "${var.instances}"
name = "vm-${data.template_file.hostname.*.rendered[count.index]}"
machine_type = "${var.machine_type}"
zone = "${var.zone[count.index % length(var.zone)]}"
tags = ["${var.role}"]
depends_on = ["google_compute_disk.thoughtworks"]
disk {
image = "${var.image}"
auto_delete = true
}
disk {
disk = "disk-${data.template_file.hostname.*.rendered[count.index]}"
auto_delete = "${var.disk_auto_delete}"
}
network_interface {
network = "default"
access_config = {}
}
metadata {
"sshKeys" = "core:${file(var.public_key_path)}"
"user-data" = "${data.template_file.cloud-config.*.rendered[count.index]}"
}
scheduling {
preemptible = "${var.preemptible}"
automatic_restart = "${var.automatic_restart}"
}
}
resource "google_compute_disk" "thoughtworks" {
count = "${var.instances}"
name = "disk-${data.template_file.hostname.*.rendered[count.index]}"
type = "${var.disk_type}"
zone = "${var.zone[count.index % length(var.zone)]}"
size = "${var.disk_size}"
}
resource "google_compute_firewall" "thoughtworks" {
count = "${length(var.fw_rules)}"
name = "${var.role}-${lookup(var.fw_rules[count.index], "name")}"
network = "default"
allow {
protocol = "${lookup(var.fw_rules[count.index], "protocol", "tcp")}"
ports = "${split(",", lookup(var.fw_rules[count.index], "ports"))}"
}
source_ranges = "${split(",", lookup(var.fw_rules[count.index], "source_ips"))}"
target_tags = ["${var.role}"]
}
resource "google_dns_record_set" "thoughtworks" {
count = "${var.instances}"
name = "${google_compute_instance.thoughtworks.*.name[count.index]}.${var.dns_zone_name}"
type = "A"
ttl = 15
managed_zone = "${var.dns_resource_name}"
rrdatas = ["${google_compute_instance.thoughtworks.*.network_interface.0.access_config.0.assigned_nat_ip[count.index]}"]
}