11data "template_file" "user_data" {
2- template = " ${ file (" ${ path . module } /user_data.sh" )} "
2+ template = file (" ${ path . module } /user_data.sh" )
33
4- vars {
5- aws_region = " ${ var . region } "
6- bucket_name = " ${ var . bucket_name } "
4+ vars = {
5+ aws_region = var.region
6+ bucket_name = var.bucket_name
77 }
88}
99
1010resource "aws_s3_bucket" "bucket" {
11- bucket = " ${ var . bucket_name } "
11+ bucket = var. bucket_name
1212 acl = " bucket-owner-full-control"
1313
14- force_destroy = " ${ var . bucket_force_destroy } "
14+ force_destroy = var. bucket_force_destroy
1515
1616 versioning {
17- enabled = " ${ var . bucket_versioning } "
17+ enabled = var. bucket_versioning
1818 }
1919
2020 lifecycle_rule {
2121 id = " log"
22- enabled = " ${ var . log_auto_clean } "
22+ enabled = var. log_auto_clean
2323
2424 prefix = " logs/"
2525
26- tags {
27- " rule" = " log"
28- " autoclean" = " ${ var . log_auto_clean } "
26+ tags = {
27+ rule = " log"
28+ autoclean = var.log_auto_clean
2929 }
3030
3131 transition {
32- days = " ${ var . log_standard_ia_days } "
32+ days = var. log_standard_ia_days
3333 storage_class = " STANDARD_IA"
3434 }
3535
3636 transition {
37- days = " ${ var . log_glacier_days } "
37+ days = var. log_glacier_days
3838 storage_class = " GLACIER"
3939 }
4040
4141 expiration {
42- days = " ${ var . log_expiry_days } "
42+ days = var. log_expiry_days
4343 }
4444 }
4545
46- tags = " ${ merge (var. tags )} "
46+ tags = merge (var. tags )
4747}
4848
4949resource "aws_s3_bucket_object" "bucket_public_keys_readme" {
50- bucket = " ${ aws_s3_bucket . bucket . id } "
50+ bucket = aws_s3_bucket. bucket . id
5151 key = " public-keys/README.txt"
5252 content = " Drop here the ssh public keys of the instances you want to control"
5353}
5454
5555resource "aws_security_group" "bastion_host_security_group" {
5656 description = " Enable SSH access to the bastion host from external via SSH port"
5757 name = " ${ local . name_prefix } -host"
58- vpc_id = " ${ var . vpc_id } "
58+ vpc_id = var. vpc_id
5959
60- tags = " ${ merge (var. tags )} "
60+ tags = merge (var. tags )
6161}
6262
6363resource "aws_security_group_rule" "ingress_bastion" {
6464 description = " Incoming traffic to bastion"
6565 type = " ingress"
66- from_port = " ${ var . public_ssh_port } "
67- to_port = " ${ var . public_ssh_port } "
66+ from_port = var. public_ssh_port
67+ to_port = var. public_ssh_port
6868 protocol = " TCP"
69- cidr_blocks = [ " ${ concat (data. aws_subnet . subnets . * . cidr_block , var. cidrs )} " ]
69+ cidr_blocks = concat (data. aws_subnet . subnets . * . cidr_block , var. cidrs )
7070
71- security_group_id = " ${ aws_security_group . bastion_host_security_group . id } "
71+ security_group_id = aws_security_group. bastion_host_security_group . id
7272}
7373
7474resource "aws_security_group_rule" "egress_bastion" {
@@ -79,27 +79,27 @@ resource "aws_security_group_rule" "egress_bastion" {
7979 protocol = " -1"
8080 cidr_blocks = [" 0.0.0.0/0" ]
8181
82- security_group_id = " ${ aws_security_group . bastion_host_security_group . id } "
82+ security_group_id = aws_security_group. bastion_host_security_group . id
8383}
8484
8585resource "aws_security_group" "private_instances_security_group" {
8686 description = " Enable SSH access to the Private instances from the bastion via SSH port"
8787 name = " ${ local . name_prefix } -priv-instances"
88- vpc_id = " ${ var . vpc_id } "
88+ vpc_id = var. vpc_id
8989
90- tags = " ${ merge (var. tags )} "
90+ tags = merge (var. tags )
9191}
9292
9393resource "aws_security_group_rule" "ingress_instances" {
9494 description = " Incoming traffic from bastion"
9595 type = " ingress"
96- from_port = " ${ var . public_ssh_port } "
97- to_port = " ${ var . public_ssh_port } "
96+ from_port = var. public_ssh_port
97+ to_port = var. public_ssh_port
9898 protocol = " TCP"
9999
100- source_security_group_id = " ${ aws_security_group . bastion_host_security_group . id } "
100+ source_security_group_id = aws_security_group. bastion_host_security_group . id
101101
102- security_group_id = " ${ aws_security_group . private_instances_security_group . id } "
102+ security_group_id = aws_security_group. private_instances_security_group . id
103103}
104104
105105resource "aws_iam_role" "bastion_host_role" {
@@ -123,10 +123,11 @@ resource "aws_iam_role" "bastion_host_role" {
123123 ]
124124}
125125EOF
126+
126127}
127128
128129resource "aws_iam_role_policy" "bastion_host_role_policy" {
129- role = " ${ aws_iam_role . bastion_host_role . id } "
130+ role = aws_iam_role. bastion_host_role . id
130131
131132 policy = << EOF
132133{
@@ -158,78 +159,77 @@ resource "aws_iam_role_policy" "bastion_host_role_policy" {
158159 ]
159160}
160161EOF
162+
161163}
162164
163165resource "aws_route53_record" "bastion_record_name" {
164- name = " ${ var . bastion_record_name } "
165- zone_id = " ${ var . hosted_zone_name } "
166+ name = var. bastion_record_name
167+ zone_id = var. hosted_zone_name
166168 type = " A"
167- count = " ${ var . create_dns_record } "
169+ count = var. create_dns_record ? 1 : 0
168170
169171 alias {
170172 evaluate_target_health = true
171- name = " ${ aws_lb . bastion_lb . dns_name } "
172- zone_id = " ${ aws_lb . bastion_lb . zone_id } "
173+ name = aws_lb. bastion_lb . dns_name
174+ zone_id = aws_lb. bastion_lb . zone_id
173175 }
174176}
175177
176178resource "aws_lb" "bastion_lb" {
177- internal = " ${ var . is_lb_private } "
179+ internal = var. is_lb_private
178180 name = " ${ local . name_prefix } -lb"
179181
180- subnets = [
181- " ${ var . elb_subnets } " ,
182- ]
182+ subnets = var. elb_subnets
183183
184184 load_balancer_type = " network"
185- tags = " ${ merge (var. tags )} "
185+ tags = merge (var. tags )
186186}
187187
188188resource "aws_lb_target_group" "bastion_lb_target_group" {
189189 name = " ${ local . name_prefix } -lb-target"
190- port = " ${ var . public_ssh_port } "
190+ port = var. public_ssh_port
191191 protocol = " TCP"
192- vpc_id = " ${ var . vpc_id } "
192+ vpc_id = var. vpc_id
193193 target_type = " instance"
194194
195195 health_check {
196196 port = " traffic-port"
197197 protocol = " TCP"
198198 }
199199
200- tags = " ${ merge (var. tags )} "
200+ tags = merge (var. tags )
201201}
202202
203203resource "aws_lb_listener" "bastion_lb_listener_22" {
204- " default_action" {
205- target_group_arn = " ${ aws_lb_target_group . bastion_lb_target_group . arn } "
204+ default_action {
205+ target_group_arn = aws_lb_target_group. bastion_lb_target_group . arn
206206 type = " forward"
207207 }
208208
209- load_balancer_arn = " ${ aws_lb . bastion_lb . arn } "
210- port = " ${ var . public_ssh_port } "
209+ load_balancer_arn = aws_lb. bastion_lb . arn
210+ port = var. public_ssh_port
211211 protocol = " TCP"
212212}
213213
214214resource "aws_iam_instance_profile" "bastion_host_profile" {
215- role = " ${ aws_iam_role . bastion_host_role . name } "
215+ role = aws_iam_role. bastion_host_role . name
216216 path = " /"
217217}
218218
219219resource "aws_launch_configuration" "bastion_launch_configuration" {
220- name_prefix = " ${ var . bastion_launch_configuration_name } "
221- image_id = " ${ data . aws_ami . amazon-linux-2 . id } "
220+ name_prefix = var. bastion_launch_configuration_name
221+ image_id = data. aws_ami . amazon-linux-2 . id
222222 instance_type = " t2.nano"
223- associate_public_ip_address = " ${ var . associate_public_ip_address } "
223+ associate_public_ip_address = var. associate_public_ip_address
224224 enable_monitoring = true
225- iam_instance_profile = " ${ aws_iam_instance_profile . bastion_host_profile . name } "
226- key_name = " ${ var . bastion_host_key_pair } "
225+ iam_instance_profile = aws_iam_instance_profile. bastion_host_profile . name
226+ key_name = var. bastion_host_key_pair
227227
228228 security_groups = [
229- " ${ aws_security_group . bastion_host_security_group . id } " ,
229+ aws_security_group . bastion_host_security_group . id ,
230230 ]
231231
232- user_data = " ${ data . template_file . user_data . rendered } "
232+ user_data = data. template_file . user_data . rendered
233233
234234 lifecycle {
235235 create_before_destroy = true
@@ -238,33 +238,32 @@ resource "aws_launch_configuration" "bastion_launch_configuration" {
238238
239239resource "aws_autoscaling_group" "bastion_auto_scaling_group" {
240240 name = " ASG-${ aws_launch_configuration . bastion_launch_configuration . name } "
241- launch_configuration = " ${ aws_launch_configuration . bastion_launch_configuration . name } "
242- max_size = " ${ var . bastion_instance_count } "
243- min_size = " ${ var . bastion_instance_count } "
244- desired_capacity = " ${ var . bastion_instance_count } "
241+ launch_configuration = aws_launch_configuration. bastion_launch_configuration . name
242+ max_size = var. bastion_instance_count
243+ min_size = var. bastion_instance_count
244+ desired_capacity = var. bastion_instance_count
245245
246- vpc_zone_identifier = [
247- " ${ var . auto_scaling_group_subnets } " ,
248- ]
246+ vpc_zone_identifier = var. auto_scaling_group_subnets
249247
250248 default_cooldown = 180
251249 health_check_grace_period = 180
252250 health_check_type = " EC2"
253251
254252 target_group_arns = [
255- " ${ aws_lb_target_group . bastion_lb_target_group . arn } " ,
253+ aws_lb_target_group . bastion_lb_target_group . arn ,
256254 ]
257255
258256 termination_policies = [
259257 " OldestLaunchConfiguration" ,
260258 ]
261259
262- tags = [ " ${ concat (
263- list (map (" key" , " Name" , " value" , " ASG-${ aws_launch_configuration . bastion_launch_configuration . name } " , " propagate_at_launch" , true )),
264- local. tags_asg_format
265- ) } " ]
260+ tags = concat (
261+ list (map (" key" , " Name" , " value" , " ASG-${ aws_launch_configuration . bastion_launch_configuration . name } " , " propagate_at_launch" , true )),
262+ local. tags_asg_format
263+ )
266264
267265 lifecycle {
268266 create_before_destroy = true
269267 }
270268}
269+
0 commit comments