This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
forked from Guimove/terraform-aws-bastion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
375 lines (317 loc) · 9.74 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
resource "aws_s3_bucket_lifecycle_configuration" "bucket" {
bucket = aws_s3_bucket.bucket.id
rule {
id = "log"
status = "Enabled"
prefix = "logs/"
transition {
days = var.log_standard_ia_days
storage_class = "STANDARD_IA"
}
transition {
days = var.log_glacier_days
storage_class = "GLACIER"
}
expiration {
days = var.log_expiry_days
}
}
}
resource "aws_s3_bucket_versioning" "bucket" {
bucket = aws_s3_bucket.bucket.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "bucket" {
bucket = aws_s3_bucket.bucket.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_acl" "bucket" {
bucket = aws_s3_bucket.bucket.id
acl = "private"
}
resource "aws_s3_bucket" "bucket" {
bucket = var.bucket_name
force_destroy = var.bucket_force_destroy
tags = merge(var.tags)
}
resource "aws_s3_bucket_public_access_block" "block" {
bucket = aws_s3_bucket.bucket.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_object" "bucket_public_keys_readme" {
bucket = aws_s3_bucket.bucket.id
key = "public-keys/README.txt"
content = "Drop here the ssh public keys of the instances you want to control"
}
resource "aws_security_group" "bastion_host_security_group" {
description = "Enable SSH access to the bastion host from external via SSH port"
name_prefix = var.resource_name_prefix
vpc_id = var.vpc_id
tags = merge(var.tags)
}
resource "aws_security_group_rule" "ingress_bastion" {
description = "Incoming traffic to bastion"
type = "ingress"
from_port = var.public_ssh_port
to_port = var.public_ssh_port
protocol = "TCP"
cidr_blocks = concat(data.aws_subnet.subnets.*.cidr_block, var.cidrs)
security_group_id = aws_security_group.bastion_host_security_group.id
}
resource "aws_security_group_rule" "egress_bastion" {
description = "Outgoing traffic from bastion to instances"
count = var.bastion_open_egress == true ? 1 : 0
type = "egress"
from_port = "0"
to_port = "65535"
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.bastion_host_security_group.id
}
resource "aws_security_group" "private_instances_security_group" {
description = "Enable SSH access to the Private instances from the bastion via SSH port"
name_prefix = "${var.resource_name_prefix}private-instances"
vpc_id = var.vpc_id
tags = merge(var.tags)
}
resource "aws_security_group_rule" "ingress_instances" {
description = "Incoming traffic from bastion"
type = "ingress"
from_port = var.public_ssh_port
to_port = var.public_ssh_port
protocol = "TCP"
source_security_group_id = aws_security_group.bastion_host_security_group.id
security_group_id = aws_security_group.private_instances_security_group.id
}
resource "aws_iam_role" "bastion_host_role" {
path = "/"
name_prefix = var.resource_name_prefix
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"ec2.amazonaws.com"
]
},
"Action": [
"sts:AssumeRole"
]
}
]
}
EOF
}
resource "aws_iam_role_policy" "bastion_host_role_policy" {
role = aws_iam_role.bastion_host_role.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:PutObjectAcl"
],
"Resource": "arn:aws:s3:::${var.bucket_name}/logs/*"
},
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::${var.bucket_name}/public-keys/*"
},
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::${var.bucket_name}",
"Condition": {
"StringEquals": {
"s3:prefix": "public-keys/"
}
}
}
]
}
EOF
}
resource "aws_iam_role_policy" "read_onelogin_keys" {
role = aws_iam_role.bastion_host_role.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssm:DescribeParameters"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ssm:GetParameter"
],
"Resource": "arn:aws:ssm:${var.region}:*:parameter/bastion/*"
}
]
}
EOF
}
resource "aws_route53_record" "bastion_record_name" {
name = var.bastion_record_name
zone_id = var.hosted_zone_name != "" ? var.hosted_zone_name : "empty"
type = "A"
count = var.create_dns_record == true ? 1 : 0
alias {
evaluate_target_health = true
name = aws_lb.bastion_lb.dns_name
zone_id = aws_lb.bastion_lb.zone_id
}
}
resource "aws_lb" "bastion_lb" {
internal = var.is_lb_private
name_prefix = substr(var.resource_name_prefix, 0, 6)
subnets = var.elb_subnets
load_balancer_type = "network"
tags = merge(var.tags)
}
resource "aws_lb_target_group" "bastion_lb_target_group" {
port = var.public_ssh_port
protocol = "TCP"
vpc_id = var.vpc_id
target_type = "instance"
deregistration_delay = 120
health_check {
port = "traffic-port"
protocol = "TCP"
}
tags = merge(var.tags)
}
resource "aws_lb_listener" "bastion_lb_listener_22" {
default_action {
target_group_arn = aws_lb_target_group.bastion_lb_target_group.arn
type = "forward"
}
load_balancer_arn = aws_lb.bastion_lb.arn
port = var.public_ssh_port
protocol = "TCP"
}
resource "aws_lb" "share_keys_web_server_lb" {
count = var.share_keys_web_server == true ? 1 : 0
internal = true
name = "${var.resource_name_prefix}authorized-keys"
subnets = var.share_keys_elb_subnets
load_balancer_type = "network"
}
resource "aws_lb_target_group" "share_keys_web_server_lb_target_group" {
port = 443
protocol = "TCP"
vpc_id = var.vpc_id
deregistration_delay = 120
tags = merge(var.tags)
stickiness {
type = "source_ip"
enabled = false
}
}
resource "aws_lb_listener" "share_keys_web_server_lb_listener" {
count = var.share_keys_web_server == true ? 1 : 0
default_action {
target_group_arn = aws_lb_target_group.share_keys_web_server_lb_target_group.arn
type = "forward"
}
load_balancer_arn = aws_lb.share_keys_web_server_lb[0].arn
port = "443"
protocol = "TCP"
}
resource "aws_security_group_rule" "ingress_share_keys_web_server_cidrs" {
count = length(var.share_keys_allowed_cidrs)
description = "Incoming traffic to share keys"
type = "ingress"
from_port = 443
to_port = 443
protocol = "TCP"
cidr_blocks = [var.share_keys_allowed_cidrs[count.index]]
security_group_id = aws_security_group.bastion_host_security_group.id
}
resource "aws_security_group_rule" "ingress_share_keys_web_server_sec_groups" {
count = length(var.share_keys_allowed_sec_groups)
description = "Incoming traffic to share keys"
type = "ingress"
from_port = 443
to_port = 443
protocol = "TCP"
source_security_group_id = var.share_keys_allowed_sec_groups[count.index]
security_group_id = aws_security_group.bastion_host_security_group
}
resource "aws_iam_instance_profile" "bastion_host_profile" {
role = aws_iam_role.bastion_host_role.name
path = "/"
name_prefix = var.resource_name_prefix
}
resource "aws_launch_configuration" "bastion_launch_configuration" {
name_prefix = var.resource_name_prefix
image_id = var.bastion_ami_id == "" ? data.aws_ami.amazon-linux-2.id : var.bastion_ami_id
instance_type = "t2.nano"
associate_public_ip_address = var.associate_public_ip_address
enable_monitoring = true
iam_instance_profile = aws_iam_instance_profile.bastion_host_profile.name
key_name = var.bastion_host_key_pair
security_groups = [
aws_security_group.bastion_host_security_group.id,
]
user_data = templatefile("${path.module}/user_data.sh", {
static_ssh_users = var.static_ssh_users,
aws_region = var.region
bucket_name = var.bucket_name
ssh_tunnel_only_users = var.ssh_tunnel_only_users
onelogin_sync = var.onelogin_sync
onelogin_sync_role_ids = var.onelogin_sync_role_ids
onelogin_sync_script = file("${path.module}/onelogin_sync/onelogin_sync.py")
onelogin_sync_requirements = file("${path.module}/onelogin_sync/requirements.txt")
share_keys_web_server = var.share_keys_web_server
})
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "bastion_auto_scaling_group" {
name = "ASG-${aws_launch_configuration.bastion_launch_configuration.name}"
launch_configuration = aws_launch_configuration.bastion_launch_configuration.name
max_size = var.bastion_instance_count
min_size = var.bastion_instance_count
desired_capacity = var.bastion_instance_count
vpc_zone_identifier = var.auto_scaling_group_subnets
default_cooldown = 180
health_check_grace_period = 180
health_check_type = "EC2"
target_group_arns = [
aws_lb_target_group.bastion_lb_target_group.arn,
aws_lb_target_group.share_keys_web_server_lb_target_group.arn
]
termination_policies = [
"OldestLaunchConfiguration",
]
tag {
key = "Name"
value = "ASG-${aws_launch_configuration.bastion_launch_configuration.name}"
propagate_at_launch = true
}
lifecycle {
create_before_destroy = true
}
}