This repository has been archived by the owner on Oct 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
120 lines (97 loc) · 2.89 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
resource "aws_security_group" "bastion" {
name = var.name
vpc_id = var.vpc_id
description = "Bastion security group (only SSH inbound access is allowed)"
tags = {
Name = var.name
}
ingress {
protocol = "tcp"
from_port = 22
to_port = 22
cidr_blocks = var.ingress_source_cidrs
}
egress {
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = [
"0.0.0.0/0",
]
}
lifecycle {
create_before_destroy = true
}
}
data "template_file" "user_data" {
template = file("${path.module}/${var.user_data_file}")
vars = {
s3_bucket_name = local.s3_full_bucket_name
s3_bucket_uri = var.s3_bucket_uri
ssh_user = var.ssh_user
keys_update_frequency = var.keys_update_frequency
enable_hourly_cron_updates = var.enable_hourly_cron_updates
additional_user_data_script = var.additional_user_data_script
}
}
/* Launch Configuration */
resource "aws_launch_configuration" "bastion" {
name_prefix = var.name
image_id = data.aws_ami.ubuntu.id
instance_type = var.instance_type
user_data = data.template_file.user_data.rendered
security_groups = [aws_security_group.bastion.id]
iam_instance_profile = aws_iam_instance_profile.default.name
lifecycle {
create_before_destroy = true
}
}
/* Autoscaling Group */
resource "aws_autoscaling_group" "bastion" {
name = var.name
vpc_zone_identifier = [var.subnet_ids[0]]
desired_capacity = "1"
min_size = "0"
max_size = "1"
health_check_grace_period = "60"
health_check_type = "EC2"
force_delete = false
wait_for_capacity_timeout = 0
launch_configuration = aws_launch_configuration.bastion.name
enabled_metrics = [
"GroupMinSize",
"GroupMaxSize",
"GroupDesiredCapacity",
"GroupInServiceInstances",
"GroupPendingInstances",
"GroupStandbyInstances",
"GroupTerminatingInstances",
"GroupTotalInstances",
]
# TF-UPGRADE-TODO: In Terraform v0.10 and earlier, it was sometimes necessary to
# force an interpolation expression to be interpreted as a list by wrapping it
# in an extra set of list brackets. That form was supported for compatibility in
# v0.11, but is no longer supported in Terraform v0.12.
#
# If the expression in the following list itself returns a list, remove the
# brackets to avoid interpretation as a list of lists. If the expression
# returns a single list item then leave it as-is and remove this TODO comment.
tags = concat(
[
{
key = "Owner"
value = "Bastion"
propagate_at_launch = true
},
{
key = "Name"
value = var.name
propagate_at_launch = true
},
],
var.asg_tags,
)
lifecycle {
create_before_destroy = true
}
}