forked from Guimove/terraform-aws-bastion
/
main.tf
345 lines (281 loc) · 8.21 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
data "template_file" "user_data" {
template = file("${path.module}/user_data.sh")
vars = {
aws_region = var.region
bucket_name = var.bucket_name
extra_user_data_content = var.extra_user_data_content
allow_ssh_commands = var.allow_ssh_commands
}
}
resource "aws_kms_key" "key" {
tags = merge(var.tags)
}
resource "aws_kms_alias" "alias" {
name = "alias/${var.bucket_name}"
target_key_id = aws_kms_key.key.arn
}
resource "aws_s3_bucket" "bucket" {
bucket = var.bucket_name
acl = "bucket-owner-full-control"
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = aws_kms_key.key.id
sse_algorithm = "aws:kms"
}
}
}
force_destroy = var.bucket_force_destroy
versioning {
enabled = var.bucket_versioning
}
lifecycle_rule {
id = "log"
enabled = var.log_auto_clean
prefix = "logs/"
tags = {
rule = "log"
autoclean = var.log_auto_clean
}
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
}
}
tags = merge(var.tags)
}
resource "aws_s3_bucket_policy" "github_bucket_policy" {
bucket = aws_s3_bucket.bucket.arn
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReadWriteAccess",
"Effect": "Allow",
"Principal": {
"AWS": "${var.service_user_arn}"
},
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::${aws_s3_bucket.bucket.name}",
"arn:aws:s3:::${aws_s3_bucket.bucket.name}/*",
]
}
]
}
EOF
}
resource "aws_s3_bucket_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"
kms_key_id = aws_kms_key.key.arn
}
resource "aws_security_group" "bastion_host_security_group" {
description = "Enable SSH access to the bastion host from external via SSH port"
name = "${local.name_prefix}-host"
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"
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 = "${local.name_prefix}-priv-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
}
data "aws_iam_policy_document" "assume_policy_document" {
statement {
actions = [
"sts:AssumeRole"
]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "bastion_host_role" {
path = "/"
assume_role_policy = data.aws_iam_policy_document.assume_policy_document.json
}
data "aws_iam_policy_document" "bastion_host_policy_document" {
statement {
actions = [
"s3:PutObject",
"s3:PutObjectAcl"
]
resources = ["${aws_s3_bucket.bucket.arn}/logs/*"]
}
statement {
actions = [
"s3:GetObject"
]
resources = ["${aws_s3_bucket.bucket.arn}/public-keys/*"]
}
statement {
actions = [
"s3:ListBucket"
]
resources = [
aws_s3_bucket.bucket.arn]
condition {
test = "ForAnyValue:StringEquals"
values = ["public-keys/"]
variable = "s3:prefix"
}
}
statement {
actions = [
"kms:Encrypt",
"kms:Decrypt"
]
resources = [aws_kms_key.key.arn]
}
}
resource "aws_iam_policy" "bastion_host_policy" {
name_prefix = "BastionHost"
policy = data.aws_iam_policy_document.bastion_host_policy_document.json
}
resource "aws_iam_role_policy_attachment" "bastion_host" {
policy_arn = aws_iam_policy.bastion_host_policy.arn
role = aws_iam_role.bastion_host_role.name
}
resource "aws_route53_record" "bastion_record_name" {
name = var.bastion_record_name
zone_id = var.hosted_zone_id
type = "A"
count = var.create_dns_record ? 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 = "${local.name_prefix}-lb"
subnets = var.elb_subnets
load_balancer_type = "network"
enable_cross_zone_load_balancing = var.cross_zone_lb
tags = merge(var.tags)
}
resource "aws_lb_target_group" "bastion_lb_target_group" {
name = "${local.name_prefix}-lb-target"
port = var.public_ssh_port
protocol = "TCP"
vpc_id = var.vpc_id
target_type = "instance"
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_iam_instance_profile" "bastion_host_profile" {
role = aws_iam_role.bastion_host_role.name
path = "/"
}
resource "aws_launch_template" "bastion_launch_template" {
name_prefix = local.name_prefix
image_id = var.bastion_ami != "" ? var.bastion_ami : data.aws_ami.amazon-linux-2.id
instance_type = "t3.nano"
monitoring {
enabled = true
}
network_interfaces {
associate_public_ip_address = var.associate_public_ip_address
security_groups = [aws_security_group.bastion_host_security_group.id]
delete_on_termination = true
}
iam_instance_profile {
name = aws_iam_instance_profile.bastion_host_profile.name
}
key_name = var.bastion_host_key_pair
user_data = base64encode(data.template_file.user_data.rendered)
tag_specifications {
resource_type = "instance"
tags = merge(map("Name", var.bastion_launch_template_name), merge(var.tags))
}
tag_specifications {
resource_type = "volume"
tags = merge(map("Name", var.bastion_launch_template_name), merge(var.tags))
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "bastion_auto_scaling_group" {
name_prefix = "ASG-${local.name_prefix}"
launch_template {
id = aws_launch_template.bastion_launch_template.id
version = "$Latest"
}
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,
]
termination_policies = [
"OldestLaunchConfiguration",
]
tags = concat(
list(map("key", "Name", "value", "ASG-${local.name_prefix}", "propagate_at_launch", true)),
local.tags_asg_format
)
lifecycle {
create_before_destroy = true
}
}