terraform.tf 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. ## Security Group for ALB
  2. resource "aws_security_group" "alb_sg" {
  3. name = "alb-security-group"
  4. description = "ALB security group to allow HTTP and HTTPS traffic"
  5. vpc_id = var.vpc_id
  6. tags = var.tags
  7. }
  8. ## Ingress Security Group Rule for ALB Security Group
  9. resource "aws_security_group_rule" "ingress_alb_sg_rule" {
  10. for_each = var.ingress_alb_sg_rule
  11. type = "ingress"
  12. from_port = each.value.from_port
  13. to_port = each.value.to_port
  14. protocol = each.value.protocol
  15. cidr_blocks = try(each.value.cidr_blocks, null)
  16. source_security_group_id = try(each.value.source_security_group_id, null)
  17. security_group_id = aws_security_group.alb_sg.id
  18. }
  19. ## Egress Security Group Rule for ALB Security Group
  20. resource "aws_security_group_rule" "egress_alb_sg_rule" {
  21. for_each = var.egress_alb_sg_rule
  22. type = "egress"
  23. from_port = each.value.from_port
  24. to_port = each.value.to_port
  25. protocol = each.value.protocol
  26. cidr_blocks = try(each.value.cidr_blocks, null)
  27. source_security_group_id = lookup(each.value, "cidr_blocks", null) == null ? try(each.value.source_security_group_id, aws_security_group.ec2_sg.id, null) : null
  28. security_group_id = aws_security_group.alb_sg.id
  29. }
  30. ## Security Group for EC2
  31. resource "aws_security_group" "ec2_sg" {
  32. name = "ec2-security-group"
  33. description = "EC2 security group to allow traffic only from ALB"
  34. vpc_id = var.vpc_id
  35. tags = var.tags
  36. }
  37. ## Ingress Security Group Rule for EC2 Security Group
  38. resource "aws_security_group_rule" "ingress_ec2_sg_rule" {
  39. for_each = var.ingress_ec2_sg_rule
  40. type = "ingress"
  41. from_port = each.value.from_port
  42. to_port = each.value.to_port
  43. protocol = each.value.protocol
  44. cidr_blocks = try(each.value.cidr_blocks, null)
  45. source_security_group_id = lookup(each.value, "cidr_blocks", null) == null ? try(each.value.source_security_group_id, aws_security_group.alb_sg.id, null) : null
  46. security_group_id = aws_security_group.ec2_sg.id
  47. }
  48. ## Egress Security Group Rule for EC2 Security Group
  49. resource "aws_security_group_rule" "egress_ec2_sg_rule" {
  50. for_each = var.egress_ec2_sg_rule
  51. type = "egress"
  52. from_port = each.value.from_port
  53. to_port = each.value.to_port
  54. protocol = each.value.protocol
  55. cidr_blocks = try(each.value.cidr_blocks, null)
  56. source_security_group_id = try(each.value.source_security_group_id, null)
  57. security_group_id = aws_security_group.ec2_sg.id
  58. }
  59. ## Fetch Ubuntu Family AMI Id
  60. data "aws_ami" "ec2_ami" {
  61. filter {
  62. name = "name"
  63. values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
  64. }
  65. filter {
  66. name = "virtualization-type"
  67. values = ["hvm"]
  68. }
  69. most_recent = true
  70. owners = ["amazon"]
  71. }
  72. ## Target Group
  73. resource "aws_lb_target_group" "ec2_alb_target_group" {
  74. name = var.ec2_alb_target_group.name
  75. port = var.ec2_alb_target_group.port
  76. protocol = var.ec2_alb_target_group.protocol
  77. vpc_id = var.vpc_id
  78. deregistration_delay = var.ec2_alb_target_group.deregistration_delay
  79. tags = var.tags
  80. }
  81. ## ALB
  82. resource "aws_lb" "alb_ec2" {
  83. name = var.alb_ec2.name
  84. enable_deletion_protection = var.alb_ec2.enable_deletion_protection
  85. internal = var.alb_ec2.internal
  86. load_balancer_type = var.alb_ec2.load_balancer_type
  87. security_groups = [aws_security_group.alb_sg.id]
  88. subnets = var.alb_subnet_ids
  89. tags = var.tags
  90. depends_on = [
  91. aws_lb_target_group.ec2_alb_target_group
  92. ]
  93. }
  94. ## ALB Listener
  95. resource "aws_lb_listener" "alb_listener" {
  96. for_each = var.alb_listener
  97. load_balancer_arn = aws_lb.alb_ec2.arn
  98. port = each.value.port
  99. protocol = each.value.protocol
  100. certificate_arn = each.value.protocol == "HTTPS" ? try(each.value.certificate_arn, null) : null
  101. ssl_policy = each.value.protocol == "HTTPS" ? try(each.value.ssl_policy, null) : null
  102. default_action {
  103. type = each.value.action_type
  104. target_group_arn = each.value.action_type == "forward" ? aws_lb_target_group.ec2_alb_target_group.arn : null
  105. dynamic "redirect" {
  106. for_each = each.value.action_type == "redirect" ? [each.value.redirect] : []
  107. content {
  108. status_code = redirect.value.status_code
  109. port = redirect.value.port
  110. protocol = redirect.value.protocol
  111. }
  112. }
  113. }
  114. tags = var.tags
  115. depends_on = [
  116. aws_lb.alb_ec2
  117. ]
  118. }
  119. ## Launch Template
  120. resource "aws_launch_template" "ec2_launch_template" {
  121. name = var.ec2_launch_template.name
  122. block_device_mappings {
  123. device_name = var.ec2_launch_template.device_name
  124. ebs {
  125. volume_size = var.ec2_launch_template.ebs_volume_size
  126. delete_on_termination = true
  127. volume_type = var.ec2_launch_template.ebs_volume_type
  128. }
  129. }
  130. ebs_optimized = var.ec2_launch_template.ebs_optimized
  131. image_id = data.aws_ami.ec2_ami.id
  132. instance_type = var.ec2_launch_template.instance_type
  133. key_name = var.ec2_launch_template.key_name
  134. vpc_security_group_ids = [aws_security_group.ec2_sg.id]
  135. update_default_version = true
  136. user_data = filebase64("${path.module}/userdata.sh")
  137. tag_specifications {
  138. resource_type = "instance"
  139. tags = var.tags
  140. }
  141. }
  142. ## AutoScaling Group
  143. resource "aws_autoscaling_group" "ec2_autoscaling_group" {
  144. name = var.ec2_autoscaling_group.name
  145. min_size = var.ec2_autoscaling_group.min_size
  146. max_size = var.ec2_autoscaling_group.max_size
  147. desired_capacity = var.ec2_autoscaling_group.desired_capacity
  148. vpc_zone_identifier = [var.ec2_subnet_id]
  149. default_cooldown = var.ec2_autoscaling_group.default_cooldown
  150. health_check_grace_period = var.ec2_autoscaling_group.health_check_grace_period
  151. launch_template {
  152. id = aws_launch_template.ec2_launch_template.id
  153. version = "$Latest"
  154. }
  155. target_group_arns = [aws_lb_target_group.ec2_alb_target_group.arn]
  156. dynamic "tag" {
  157. for_each = var.tags
  158. content {
  159. key = tag.key
  160. value = tag.value
  161. propagate_at_launch = true
  162. }
  163. }
  164. depends_on = [
  165. aws_lb.alb_ec2
  166. ]
  167. }