-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
216 lines (175 loc) · 5.83 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
# -------------------------------------------------------------------------------------------------
# VPC Resources
# -------------------------------------------------------------------------------------------------
module "aws_vpc" {
source = "github.com/terraform-aws-modules/terraform-aws-vpc?ref=v5.16.0"
cidr = var.vpc_cidr
azs = var.vpc_subnet_azs
private_subnets = var.vpc_private_subnets
public_subnets = var.vpc_public_subnets
map_public_ip_on_launch = true
secondary_cidr_blocks = var.vpc_secondary_cidr_blocks
enable_nat_gateway = var.vpc_enable_nat_gateway
enable_vpn_gateway = var.vpc_enable_vpn_gateway
enable_dns_hostnames = var.vpc_enable_dns_hostnames
enable_dns_support = var.vpc_enable_dns_support
one_nat_gateway_per_az = var.vpc_one_nat_gateway_per_az
reuse_nat_ips = var.vpc_reuse_nat_ips
external_nat_ip_ids = local.ids_of_eips_for_natgws
customer_gateways = var.vpc_customer_gateways
manage_default_route_table = false
manage_default_network_acl = false
manage_default_security_group = var.manage_default_security_group
default_security_group_ingress = var.default_security_group_ingress
default_security_group_egress = var.default_security_group_egress
name = var.name
tags = var.tags
vpc_tags = var.vpc_tags
public_subnet_tags = var.public_subnet_tags
private_subnet_tags = var.private_subnet_tags
}
# -------------------------------------------------------------------------------------------------
# Bastion ELB
# -------------------------------------------------------------------------------------------------
module "aws_elb" {
enable = var.vpc_enable_bastion_host
source = "github.com/Flaconi/terraform-aws-elb?ref=v2.0.0"
name = local.bastion_elb_name
vpc_id = module.aws_vpc.vpc_id
subnet_ids = module.aws_vpc.public_subnets
# Listener
lb_port = 22
instance_port = 22
# Security
inbound_cidr_blocks = var.bastion_ssh_cidr_blocks
security_group_names = var.bastion_security_group_names
# DNS
route53_public_dns_name = var.bastion_route53_public_dns_name
tags = var.tags
}
# -------------------------------------------------------------------------------------------------
# Bastion Host
# -------------------------------------------------------------------------------------------------
data "aws_ami" "bastion" {
count = var.vpc_enable_bastion_host ? 1 : 0
owners = ["amazon"]
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm*"]
}
}
resource "aws_security_group" "bastion" {
count = var.vpc_enable_bastion_host ? 1 : 0
name_prefix = local.bastion_sg_name
description = "Security group for the ${local.bastion_lc_name} launch configuration"
vpc_id = module.aws_vpc.vpc_id
ingress {
from_port = "22"
to_port = "22"
protocol = "tcp"
security_groups = module.aws_elb.security_group_ids
description = "External SSH. Allow SSH access to bastion instances from this security group (by ELB or instance)."
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "AWS default egress rule"
}
revoke_rules_on_delete = true
# Ensure a new sg is in place before destroying the current one.
# This will/should prevent any race-conditions.
lifecycle {
create_before_destroy = true
}
tags = merge(
var.tags,
{
"Name" = local.bastion_asg_name
},
)
}
resource "aws_launch_template" "bastion" {
count = var.vpc_enable_bastion_host ? 1 : 0
name_prefix = local.bastion_lc_name
image_id = var.bastion_ami != null ? var.bastion_ami : data.aws_ami.bastion[0].image_id
instance_type = var.bastion_instance_type
vpc_security_group_ids = [aws_security_group.bastion[0].id]
user_data = length(var.bastion_ssh_keys) > 0 ? base64encode(templatefile("${path.module}/user_data.sh.tftpl",
{
ssh_user = var.bastion_ssh_user
ssh_keys = join("\n", var.bastion_ssh_keys)
}
)) : null
metadata_options {
http_tokens = "required"
http_put_response_hop_limit = 1
http_endpoint = "enabled"
}
block_device_mappings {
device_name = "/dev/sda1"
ebs {
volume_size = "8"
}
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "bastion" {
count = var.vpc_enable_bastion_host ? 1 : 0
name_prefix = local.bastion_asg_name
vpc_zone_identifier = module.aws_vpc.private_subnets
desired_capacity = var.bastion_cluster_size
min_size = var.bastion_cluster_size
max_size = var.bastion_cluster_size
health_check_grace_period = "60"
health_check_type = "EC2"
force_delete = false
wait_for_capacity_timeout = 0
launch_template {
id = aws_launch_template.bastion[0].id
version = aws_launch_template.bastion[0].latest_version
}
load_balancers = [module.aws_elb.id]
enabled_metrics = [
"GroupMinSize",
"GroupMaxSize",
"GroupDesiredCapacity",
"GroupInServiceInstances",
"GroupPendingInstances",
"GroupStandbyInstances",
"GroupTerminatingInstances",
"GroupTotalInstances",
]
dynamic "tag" {
for_each = concat(
[
{
key = "Name"
value = local.bastion_asg_name
propagate_at_launch = true
}
],
local.tags_asg_format,
)
content {
key = tag.value["key"]
value = tag.value["value"]
propagate_at_launch = tag.value["propagate_at_launch"]
}
}
lifecycle {
create_before_destroy = true
}
}