-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform.tf
301 lines (246 loc) · 6.71 KB
/
terraform.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# Configure the CloudStack Provider
# Will use CLOUDSTACK_API_KEY and CLOUDSTACK_SECRET_KEY env vars
provider "cloudstack" {
api_url = "https://api.exoscale.ch/compute"
}
resource "cloudstack_ssh_keypair" "default" {
name = "SSH key"
public_key = "${file("yubikey_id_rsa.pub")}"
}
resource "cloudstack_security_group" "web" {
name = "web"
description = "Web Servers"
}
resource "cloudstack_security_group_rule" "web" {
security_group_id = "${cloudstack_security_group.web.id}"
# Allow HTTP and HTTPS traffic from entire Internet
rule {
cidr_list = [
"0.0.0.0/0",
]
ports = [
80,
443,
]
protocol = "tcp"
traffic_type = "ingress"
}
# Allow management connections from bastion
rule {
user_security_group_list = [
"${cloudstack_security_group.bastion.name}",
]
ports = [
22,
]
protocol = "tcp"
traffic_type = "ingress"
}
}
variable "zone" {
description = "Cloudstack Availability Zone"
type = "string"
#default = "ch-dk-2"
default = "ch-gva-2"
}
variable "nb_web_servers" {
type = "string"
description = "Number of web servers"
default = "2"
}
resource "cloudstack_instance" "web" {
display_name = "web${count.index}"
count = "${var.nb_web_servers}"
name = ""
template = "Linux CentOS 7.2 64-bit"
service_offering = "Micro"
zone = "${var.zone}"
root_disk_size = "50"
expunge = true
keypair = "${cloudstack_ssh_keypair.default.id}"
affinity_group_ids = [
"${cloudstack_affinity_group.web.id}",
]
security_group_ids = [
"${cloudstack_security_group.web.id}",
]
# Install and run Apache httpd right after
# provisioning instance
provisioner "remote-exec" {
inline = [
"yum -y install httpd",
"yum -y install mod_ssl",
"systemctl start httpd",
]
}
# Connect through bastion host
connection {
type = "ssh"
user = "root"
bastion_host = "${cloudstack_instance.bastion.ip_address}"
agent = true
}
}
# Make sure web servers don't run on same host
resource "cloudstack_affinity_group" "web" {
name = "web-affinity-group"
type = "host anti-affinity"
}
resource "cloudstack_security_group" "bastion" {
name = "bastion"
description = "Bastion Servers"
}
resource "cloudstack_security_group_rule" "bastion" {
security_group_id = "${cloudstack_security_group.bastion.id}"
# Allow management connection
# from well known IP range
rule {
protocol = "tcp"
ports = [
22,
]
traffic_type = "ingress"
cidr_list = [
# Replace this with your IP address
"0.0.0.0/0",
]
}
# Allow management connection to web servers through bastion
rule {
protocol = "tcp"
ports = [
22,
]
traffic_type = "egress"
user_security_group_list = [
"${cloudstack_security_group.web.name}",
]
}
}
resource "cloudstack_instance" "bastion" {
display_name = "bastion"
template = "Linux CentOS 7.2 64-bit"
service_offering = "Micro"
expunge = true
zone = "${var.zone}"
root_disk_size = "50"
keypair = "${cloudstack_ssh_keypair.default.id}"
security_group_ids = [
"${cloudstack_security_group.bastion.id}",
]
}
output "Bastion IP" {
value = "${cloudstack_instance.bastion.ip_address}"
}
output "Web IPs" {
value = "${join(", ",cloudstack_instance.web.*.ip_address)}"
}
# Configure the AWS Provider
# Will use AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
provider "aws" {
region = "eu-central-1"
}
# Looked up manually in aws route 53 console
# Consider using aws_route53_zone resource instead
variable "dns_zone_id" {
default = "Z2X1UBSEPFNQNM"
description = "DNS hosted zone id"
type = "string"
}
# Create DNS records for exoscale web servers
resource "aws_route53_record" "exoscale" {
zone_id = "${var.dns_zone_id}"
name = "exoscale.landro.io."
type = "A"
ttl = 60
records = [
"${cloudstack_instance.web.*.ip_address}",
]
}
# Configure the Arukas Provider
# Will use ARUKAS_JSON_API_TOKEN and ARUKAS_JSON_API_SECRET
provider "arukas" {}
resource "arukas_container" "arukas" {
name = "Arukas"
image = "landro/httpd-centos-alpine:latest"
instances = 1
memory = 256
ports = {
protocol = "tcp"
number = "80"
}
}
# Create DNS CNAME record for exoscale vms
# in order to support DNS routing
resource "aws_route53_record" "cloudstack" {
zone_id = "${var.dns_zone_id}"
name = "cloudstack.landro.io."
type = "CNAME"
ttl = 60
records = [
"${aws_route53_record.exoscale.fqdn}",
]
}
# Create DNS CNAME record for arukas containers
# in order to support DNS routing
resource "aws_route53_record" "arukas" {
zone_id = "${var.dns_zone_id}"
name = "arukas.landro.io."
type = "CNAME"
ttl = 60
records = [
"${arukas_container.arukas.endpoint_full_hostname}",
]
}
# Create DNS record with weighted routing policy and health checking
# targeting cloudstack
resource "aws_route53_record" "cloudstack_www" {
zone_id = "${var.dns_zone_id}"
name = "www.landro.io."
type = "CNAME"
weighted_routing_policy {
weight = "${var.nb_web_servers}"
}
set_identifier = "cloustack"
alias {
name = "${aws_route53_record.cloudstack.fqdn}"
zone_id = "${aws_route53_record.cloudstack.zone_id}"
evaluate_target_health = true
}
health_check_id = "${aws_route53_health_check.cloudstack.id}"
}
# Create DNS record with weighted routing policy and health checking
# targeting arukas
resource "aws_route53_record" "arukas_www" {
zone_id = "${var.dns_zone_id}"
name = "www.landro.io."
type = "CNAME"
weighted_routing_policy {
weight = "${arukas_container.arukas.instances}"
}
set_identifier = "arukas"
alias {
name = "${aws_route53_record.arukas.fqdn}"
zone_id = "${aws_route53_record.arukas.zone_id}"
evaluate_target_health = true
}
health_check_id = "${aws_route53_health_check.arukas.id}"
}
# Create health check targeting Apache httpd on centos
resource "aws_route53_health_check" "cloudstack" {
fqdn = "${aws_route53_record.cloudstack.fqdn}"
port = 80
type = "HTTP"
resource_path = "/images/poweredby.png"
failure_threshold = "3"
request_interval = "10"
}
# Create health check targeting Apache httpd on alpine
resource "aws_route53_health_check" "arukas" {
fqdn = "${arukas_container.arukas.endpoint_full_hostname}"
port = 443
type = "HTTPS"
resource_path = "/"
failure_threshold = "3"
request_interval = "10"
}