This repository was archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
101 lines (82 loc) · 2.3 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
resource "google_compute_image" "redis-image" {
name = "redis-image"
raw_disk {
source = var.raw_image_source
}
timeouts {
create = "10m"
}
}
data "template_file" "redis_stackdriver" {
count = var.node_count
template = file("${path.module}/redis-stackdriver.conf.tpl")
vars = {
hostname = "${var.instance_name}-${count.index}"
}
}
resource "google_compute_instance" "redis_instance" {
name = "${var.instance_name}-${count.index}"
machine_type = "n1-standard-1"
zone = var.zone
count = var.node_count
tags = ["redis"]
boot_disk {
initialize_params {
image = google_compute_image.redis-image.self_link
type = "pd-ssd"
size = "20"
}
}
network_interface {
network = "default"
access_config {
// Ephemeral IP
}
}
metadata = {
ssh-keys = "devops:${tls_private_key.provision_key.public_key_openssh}"
}
service_account {
scopes = ["userinfo-email", "compute-ro", "storage-rw", "monitoring-write", "logging-write", "https://www.googleapis.com/auth/trace.append"]
}
provisioner "file" {
content = data.template_file.redis_stackdriver[count.index].rendered
destination = "/tmp/redis-stackdriver.conf"
connection {
host = "${self.network_interface.0.access_config.0.nat_ip}"
type = "ssh"
user = "devops"
private_key = tls_private_key.provision_key.private_key_pem
agent = false
}
}
provisioner "remote-exec" {
connection {
host = "${self.network_interface.0.access_config.0.nat_ip}"
type = "ssh"
user = "devops"
private_key = tls_private_key.provision_key.private_key_pem
agent = false
}
inline = [
"sudo systemctl start redis.service",
"sudo mv /tmp/redis-stackdriver.conf /opt/stackdriver/collectd/etc/collectd.d/redis.conf",
]
}
allow_stopping_for_update = false
}
resource "tls_private_key" "provision_key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "google_compute_firewall" "redis-allow-cluster" {
name = "redis-allow-cluster-${var.instance_name}"
network = "default"
priority = "1000"
allow {
protocol = "tcp"
ports = ["6379"]
}
source_ranges = [var.cluster_ipv4_cidr]
source_tags = ["redis"]
}