forked from Guimove/terraform-aws-bastion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
269 lines (217 loc) · 6.44 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
data "template_file" "user_data" {
template = file("${path.module}/user_data.sh")
vars = {
aws_region = var.region
bucket_name = var.bucket_name
}
}
resource "aws_s3_bucket" "bucket" {
bucket = var.bucket_name
acl = "bucket-owner-full-control"
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_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 = "${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
}
resource "aws_iam_role" "bastion_host_role" {
path = "/"
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_route53_record" "bastion_record_name" {
name = var.bastion_record_name
zone_id = var.hosted_zone_name
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"
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_configuration" "bastion_launch_configuration" {
name_prefix = var.bastion_launch_configuration_name
image_id = data.aws_ami.amazon-linux-2.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 = data.template_file.user_data.rendered
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,
]
termination_policies = [
"OldestLaunchConfiguration",
]
tags = concat(
list(map("key", "Name", "value", "ASG-${aws_launch_configuration.bastion_launch_configuration.name}", "propagate_at_launch", true)),
local.tags_asg_format
)
lifecycle {
create_before_destroy = true
}
}