-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.tf
121 lines (97 loc) · 3.88 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
resource "google_compute_address" "instances" {
count = "${var.amount}"
name = "${var.name_prefix}-${count.index}"
region = "${var.region}"
}
resource "google_compute_disk" "instances" {
count = "${var.amount}"
name = "${var.name_prefix}-${count.index+1}"
type = "${var.disk_type}"
size = "${var.disk_size}"
# optional
zone = "${var.zone}"
image = "${var.disk_image}"
provisioner "local-exec" {
command = "${var.disk_create_local_exec_command_or_fail}"
on_failure = "fail"
}
provisioner "local-exec" {
command = "${var.disk_create_local_exec_command_and_continue}"
on_failure = "continue"
}
provisioner "local-exec" {
when = "destroy"
command = "${var.disk_destroy_local_exec_command_or_fail}"
on_failure = "fail"
}
provisioner "local-exec" {
when = "destroy"
command = "${var.disk_destroy_local_exec_command_and_continue}"
on_failure = "continue"
}
}
# https://www.terraform.io/docs/providers/google/r/compute_instance.html
resource "google_compute_instance" "instances" {
count = "${var.amount}"
name = "${var.name_prefix}-${count.index+1}"
zone = "${var.zone}"
machine_type = "${var.machine_type}"
boot_disk = {
source = "${google_compute_disk.instances.*.name[count.index]}"
auto_delete = false
}
# reference: https://cloud.google.com/compute/docs/storing-retrieving-metadata
metadata {
description = "Managed by Terraform"
user-data = "${replace(replace(var.user_data, "$$ZONE", var.zone), "$$REGION", var.region)}"
ssh-keys = "${var.username}:${file("${var.public_key_path}")}"
}
network_interface = {
network = "default"
access_config = {
nat_ip = "${google_compute_address.instances.*.address[count.index]}"
}
}
scheduling {
on_host_maintenance = "MIGRATE"
automatic_restart = "${var.automatic_restart}"
}
allow_stopping_for_update = "true"
}
# ========================================================================================= #
# provisioner actions #
# ========================================================================================= #
# resource "null_resource" "provisioner" {
# triggers {
# vm = "${google_compute_instance.instances.name}"
# }
# # generic connection block for all provisioners
# connection {
# type = "ssh"
# host = "${google_compute_address.instances.*.address[count.index]}"
# user = "${var.username}"
# private_key = "${file("${var.private_key_path}")}"
# }
# reference: https://github.com/jonmorehouse/terraform-provisioner-ansible
# fails: not maintained, not compatible with latest tf version
# provisioner "ansible" {
# playbook = "awx.yml"
# hosts = ["all"]
# }
# }
# ========================================================================================= #
# binding a DNS name to the ephemeral IP of a new instance #
# requires google_dns_managed_zone #
# ========================================================================================= #
resource "google_dns_record_set" "dns_record" {
# name = "${google_compute_instance.instances.*.name[count.index]}.${google_dns_managed_zone.dns_zone.dns_name}"
# for example: dns_record_name=ansible-dev
# will be: ansible-dev.cloud.eimertvink.nl
# name = "${var.dns_record_name}.${google_dns_managed_zone.managed_zone.dns_name}"
# managed_zone = "${google_dns_managed_zone.managed_zone.name}"
name = "${var.dns_record_name}.${var.dns_zone}"
managed_zone = "${var.dns_name}"
type = "A"
ttl = 300
rrdatas = ["${google_compute_instance.instances.*.network_interface.0.access_config.0.assigned_nat_ip}"]
}