-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.tf
305 lines (254 loc) · 6.75 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
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
302
303
304
305
//
// The contents of this file are Copyright (c) 2019. HAProxy Technologies. All Rights Reserved.
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
//
provider "aws" {
region = "${var.aws_region}"
}
// Lookup latest HAPEE AWS AMI (1.8r1 at this moment)
data "aws_ami" "hapee_aws_amis" {
most_recent = true
filter {
name = "product-code"
values = ["483gxnuft87jy44d3q8n4kvt1"]
}
filter {
name = "name"
values = ["hapee-ubuntu-xenial-amd64-hvm-1.8*"]
}
owners = ["aws-marketplace"]
}
// Lookup latest Ubuntu Xenial 16.04 AMI
data "aws_ami" "ubuntu_aws_amis" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
}
owners = ["099720109477"]
}
// Default VPC definition
resource "aws_vpc" "default" {
cidr_block = "20.0.0.0/16"
enable_dns_hostnames = true
tags {
Name = "hapee_test_vpc"
}
}
// Default subnet definition; in real world this sould span over at least two AZ
resource "aws_subnet" "tf_test_subnet" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "20.0.0.0/24"
map_public_ip_on_launch = true
tags {
Name = "hapee_test_subnet"
}
}
// Define our IGW
resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.default.id}"
tags {
Name = "hapee_test_ig"
}
}
// Define our standard routing table
resource "aws_route_table" "r" {
vpc_id = "${aws_vpc.default.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
}
tags {
Name = "hapee_test_route_table"
}
}
// Routing table association for default subnet
resource "aws_route_table_association" "a" {
subnet_id = "${aws_subnet.tf_test_subnet.id}"
route_table_id = "${aws_route_table.r.id}"
}
// Security group for Web backends
resource "aws_security_group" "web_node_sg" {
name = "web_node_sg"
description = "Instance Web SG: pass SSH, permit HTTP only from HAPEE"
vpc_id = "${aws_vpc.default.id}"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
self = true
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
security_groups = ["${aws_security_group.hapee_node_sg.id}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
self = true
}
tags {
Name = "hapee_web_node_sg"
}
}
// Security group for HAPEE LB nodes
resource "aws_security_group" "hapee_node_sg" {
name = "hapee_node_sg"
description = "Instance HAPEE SG: pass SSH, HTTP, HTTPS and Dashboard traffic by default"
vpc_id = "${aws_vpc.default.id}"
ingress {
from_port = 3
to_port = 4
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
self = true
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
self = true
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
self = true
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
self = true
}
ingress {
from_port = 694
to_port = 694
protocol = "udp"
self = true
}
ingress {
from_port = 9022
to_port = 9022
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
self = true
}
ingress {
from_port = 9023
to_port = 9023
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
self = true
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
self = true
}
tags {
Name = "hapee_node_sg"
}
}
// IAM policy document - Assume role policy
data "aws_iam_policy_document" "instance_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
// IAM policy document - EIP permissions policy
data "aws_iam_policy_document" "eip_policy" {
statement {
sid = "1"
actions = [
"ec2:DescribeAddresses",
"ec2:AllocateAddress",
"ec2:ReleaseAddress",
"ec2:DescribeInstances",
"ec2:AssociateAddress",
"ec2:DisassociateAddress",
"ec2:DescribeNetworkInterfaces",
"ec2:AssignPrivateIpAddresses",
"ec2:UnassignPrivateIpAddresses",
]
resources = ["*"]
}
}
// IAM role - EIP role
resource "aws_iam_role" "eip_role" {
name = "hapee_eip_role"
assume_role_policy = "${data.aws_iam_policy_document.instance_assume_role_policy.json}"
}
// IAM role policy - EIP role policy
resource "aws_iam_role_policy" "eip_role_policy" {
name = "hapee_eip_role_policy"
role = "${aws_iam_role.eip_role.id}"
policy = "${data.aws_iam_policy_document.eip_policy.json}"
}
// IAM instance profile - EIP instance profile
resource "aws_iam_instance_profile" "eip_instance_profile" {
name = "hapee_instance_profile"
role = "${aws_iam_role.eip_role.id}"
}
// Instance definition for Web backends
// Variable instance count
resource "aws_instance" "web_node" {
count = "${var.web_cluster_size}"
instance_type = "${var.aws_web_instance_type}"
ami = "${data.aws_ami.ubuntu_aws_amis.id}"
key_name = "${var.key_name}"
vpc_security_group_ids = ["${aws_security_group.web_node_sg.id}"]
subnet_id = "${aws_subnet.tf_test_subnet.id}"
user_data = <<EOF
#cloud-config
runcmd:
- systemctl stop apt-daily.service
- systemctl kill --kill-who=all apt-daily.service
- systemctl stop apt-daily.timer
EOF
tags {
Name = "hapee_web_node"
}
}
// Instance definition for HAPEE LB nodes
// Static instance count at 2
resource "aws_instance" "hapee_node" {
count = 2
instance_type = "${var.aws_hapee_instance_type}"
ami = "${data.aws_ami.hapee_aws_amis.id}"
key_name = "${var.key_name}"
iam_instance_profile = "${aws_iam_instance_profile.eip_instance_profile.id}"
vpc_security_group_ids = ["${aws_security_group.hapee_node_sg.id}"]
subnet_id = "${aws_subnet.tf_test_subnet.id}"
user_data = <<EOF
#cloud-config
runcmd:
- systemctl stop apt-daily.service
- systemctl kill --kill-who=all apt-daily.service
- systemctl stop apt-daily.timer
EOF
tags {
Name = "hapee_lb_node"
}
}
// EIP allocation for primary static address for each HAPEE LB instance
resource "aws_eip" "hapee_node_eip1" {
count = 2
network_interface = "${element(aws_instance.hapee_node.*.network_interface_id, count.index)}"
vpc = true
}