-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.tf
282 lines (233 loc) · 6.49 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
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
locals {
backup_bucket_name = var.s3_bucket_name != "" ? var.s3_bucket_name : format("%s-%s", var.resource_name_prefix, "backup")
}
data "template_file" "user_data" {
template = templatefile("${path.module}/templates/user_data.sh.tpl",
{
aws_region = data.aws_region.current.name
s3_backup_bucket = local.backup_bucket_name
healthchecks_io_key = "/pritunl/${var.resource_name_prefix}/healthchecks-io-key"
}
)
}
data "template_file" "kms_policy" {
template = templatefile("${path.module}/templates/key_policy.json.tpl", {
resource_name_prefix = var.resource_name_prefix
account_id = data.aws_caller_identity.current.account_id
key_admin_arn = aws_iam_role.role.arn
}
)
}
data "template_file" "iam_instance_role_policy" {
template = templatefile("${path.module}/templates/iam_instance_role_policy.json.tpl", {
s3_backup_bucket = local.backup_bucket_name
resource_name_prefix = var.resource_name_prefix
aws_region = data.aws_region.current.name
account_id = data.aws_caller_identity.current.account_id
ssm_key_prefix = "/pritunl/${var.resource_name_prefix}/*"
}
)
}
resource "null_resource" "waiter" {
depends_on = [aws_iam_instance_profile.ec2_profile]
provisioner "local-exec" {
command = "sleep 15"
}
}
resource "aws_kms_key" "parameter_store" {
depends_on = [null_resource.waiter]
description = "Parameter store and backup key for ${var.resource_name_prefix}"
policy = data.template_file.kms_policy.rendered
deletion_window_in_days = 30
is_enabled = true
enable_key_rotation = true
tags = merge(
tomap({"Name" = format("%s-%s", var.resource_name_prefix, "parameter-store")}),
var.tags,
)
}
resource "aws_kms_alias" "parameter_store" {
depends_on = [aws_kms_key.parameter_store]
name = "alias/${var.resource_name_prefix}-parameter-store"
target_key_id = aws_kms_key.parameter_store.key_id
}
resource "aws_ssm_parameter" "healthchecks_io_key" {
name = "/pritunl/${var.resource_name_prefix}/healthchecks-io-key"
type = "SecureString"
value = var.healthchecks_io_key
key_id = aws_kms_key.parameter_store.arn
overwrite = true
tags = merge(
tomap({"Name" = format("%s/%s/%s", "pritunl", var.resource_name_prefix, "healthchecks-io-key")}),
var.tags,
)
}
resource "aws_s3_bucket" "backup" {
depends_on = [aws_kms_key.parameter_store]
bucket = local.backup_bucket_name
acl = "private"
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = aws_kms_key.parameter_store.arn
sse_algorithm = "aws:kms"
}
}
}
lifecycle_rule {
prefix = "backups"
enabled = true
expiration {
days = 30
}
abort_incomplete_multipart_upload_days = 7
}
tags = merge(
tomap({"Name" = local.backup_bucket_name}),
var.tags,
)
}
# ec2 iam role
resource "aws_iam_role" "role" {
name = var.resource_name_prefix
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy" "policy" {
depends_on = [aws_iam_role.role]
name = "${var.resource_name_prefix}-instance-policy"
role = aws_iam_role.role.id
policy = data.template_file.iam_instance_role_policy.rendered
}
resource "aws_iam_instance_profile" "ec2_profile" {
depends_on = [aws_iam_role.role, aws_iam_role_policy.policy]
name = "${var.resource_name_prefix}-instance"
role = aws_iam_role.role.name
}
resource "aws_security_group" "pritunl" {
name = "${var.resource_name_prefix}-vpn"
description = "${var.resource_name_prefix}-vpn"
vpc_id = var.vpc_id
# SSH access
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.internal_cidrs
}
# HTTP access for Let's Encrypt validation
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = var.whitelist_http
}
# HTTPS access
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.internal_cidrs
}
# VPN WAN access
ingress {
from_port = 10000
to_port = 19999
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
# ICMP
ingress {
from_port = -1
to_port = -1
protocol = "icmp"
cidr_blocks = var.internal_cidrs
}
# outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge(
tomap({"Name" = format("%s-%s", var.resource_name_prefix, "vpn")}),
var.tags,
)
}
resource "aws_security_group" "allow_from_office" {
name = "${var.resource_name_prefix}-whitelist"
description = "Allows SSH connections and HTTP(s) connections from office"
vpc_id = var.vpc_id
# SSH access
ingress {
description = "Allow SSH access from select CIDRs"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.whitelist
}
# HTTPS access
ingress {
description = "Allow HTTPS access from select CIDRs"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.whitelist
}
# ICMP
ingress {
description = "Allow ICMPv4 from select CIDRs"
from_port = -1
to_port = -1
protocol = "icmp"
cidr_blocks = var.whitelist
}
# outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge(
tomap({"Name" = format("%s-%s", var.resource_name_prefix, "whitelist")}),
var.tags,
)
}
resource "aws_instance" "pritunl" {
ami = var.ami_id
instance_type = var.instance_type
key_name = var.aws_key_name
ebs_optimized = var.ebs_optimized
user_data = data.template_file.user_data.rendered
vpc_security_group_ids = [
aws_security_group.pritunl.id,
aws_security_group.allow_from_office.id,
]
subnet_id = var.public_subnet_id
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
tags = merge(
tomap({"Name" = format("%s-%s", var.resource_name_prefix, "vpn")}),
var.tags,
)
}
resource "aws_eip" "pritunl" {
instance = aws_instance.pritunl.id
vpc = true
}