-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.tf
162 lines (138 loc) · 3.41 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
data "aws_subnet" "environment" {
vpc_id = var.vpc_id
id = var.public_subnet_id
}
data "template_file" "master" {
template = file("${path.module}/files/master.json.tpl")
vars = {
environment = var.environment
token = var.token
encryption_key = var.encryption_key
}
}
resource "aws_instance" "server" {
ami = var.ami[var.region]
instance_type = var.instance_type
key_name = var.key_name
count = var.servers
subnet_id = var.public_subnet_id
vpc_security_group_ids = [
aws_security_group.consul.id,
]
connection {
host = coalesce(self.public_ip, self.private_ip)
type = "ssh"
user = "ubuntu"
private_key = file(var.private_key_path)
}
tags = {
Name = "${var.environment}-consul-server-${count.index}"
}
provisioner "file" {
content = data.template_file.master.rendered
destination = "/tmp/master.json"
}
provisioner "remote-exec" {
inline = [
"sudo mkdir -p /etc/systemd/system/consul.d",
"sudo mv /tmp/master.json /etc/systemd/system/consul.d",
"echo ${var.servers} > /tmp/consul-server-count",
"echo ${aws_instance.server[0].private_dns} > /tmp/consul-server-addr",
"echo ${aws_instance.server[0].private_ip} > /tmp/consul-server-ip",
]
}
provisioner "remote-exec" {
scripts = [
"${path.module}/files/install.sh",
"${path.module}/files/service.sh",
"${path.module}/files/ip_tables.sh",
]
}
}
resource "aws_security_group" "consul" {
name = "${var.environment}-consul"
description = "Consul internal traffic + maintenance."
vpc_id = var.vpc_id
ingress {
from_port = 0
to_port = 65535
protocol = "tcp"
self = true
}
ingress {
from_port = 0
to_port = 65535
protocol = "udp"
self = true
}
// allow traffic for TCP 53 (DNS)
ingress {
from_port = 53
to_port = 53
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
// allow traffic for UDP 53 (DNS)
ingress {
from_port = 53
to_port = 53
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
// allow traffic for TCP 8300 (Server RPC)
ingress {
from_port = 8300
to_port = 8300
protocol = "tcp"
cidr_blocks = [data.aws_subnet.environment.cidr_block]
}
// allow traffic for TCP 8301 (Serf LAN)
ingress {
from_port = 8301
to_port = 8301
protocol = "tcp"
cidr_blocks = [data.aws_subnet.environment.cidr_block]
}
// allow traffic for UDP 8301 (Serf LAN)
ingress {
from_port = 8301
to_port = 8301
protocol = "udp"
cidr_blocks = [data.aws_subnet.environment.cidr_block]
}
// allow traffic for TCP 8400 (Consul RPC)
ingress {
from_port = 8400
to_port = 8400
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
// allow traffic for TCP 8500 (Consul Web UI)
ingress {
from_port = 8500
to_port = 8500
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8
to_port = 0
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.environment}-consul-sg"
}
}