elb.tf 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # lb ports
  2. locals {
  3. alb_listener_ports = {
  4. ui = "8000"
  5. api = "8080"
  6. agent = "8081"
  7. }
  8. }
  9. #----------------------------------------------------------------------------
  10. # INTERNAL LB
  11. #----------------------------------------------------------------------------
  12. resource "aws_alb" "sensu_internal" {
  13. name = "sensu-alb-internal-${var.environment}"
  14. security_groups = [ aws_security_group.sensu_alb_server_internal.id ]
  15. internal = true
  16. subnets = var.private_subnets
  17. load_balancer_type = "application"
  18. access_logs {
  19. bucket = "xdr-elb-${ var.environment }"
  20. enabled = true
  21. }
  22. tags = merge(var.standard_tags, var.tags, { Name = "sensu-alb-internal-${var.environment}" })
  23. }
  24. resource "aws_alb_target_group" "sensu_internal" {
  25. for_each = local.alb_listener_ports
  26. name = "sensu-alb-targets-${each.key}"
  27. port = each.value
  28. protocol = "HTTPS"
  29. #deregistration_delay = "${local.lb_deregistration_delay}"
  30. vpc_id = var.vpc_id
  31. health_check {
  32. protocol = "HTTPS"
  33. port = "8080"
  34. path = "/health"
  35. matcher = "200"
  36. timeout = "4"
  37. interval = "5"
  38. }
  39. stickiness {
  40. type = "lb_cookie"
  41. enabled = false
  42. }
  43. tags = merge(var.standard_tags, var.tags)
  44. }
  45. resource "aws_lb_target_group_attachment" "sensu_internal" {
  46. for_each = local.alb_listener_ports
  47. target_group_arn = aws_alb_target_group.sensu_internal[each.key].arn
  48. target_id = aws_instance.instance.id
  49. port = each.value
  50. }
  51. # Create a new alb listener
  52. resource "aws_alb_listener" "sensu_internal" {
  53. for_each = local.alb_listener_ports
  54. load_balancer_arn = aws_alb.sensu_internal.arn
  55. port = each.value
  56. protocol = "HTTPS"
  57. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2019-08" # PFS, TLS1.2, most "restrictive" policy (took awhile to find that)
  58. certificate_arn = aws_acm_certificate.cert.arn
  59. default_action {
  60. target_group_arn = aws_alb_target_group.sensu_internal[each.key].arn
  61. type = "forward"
  62. }
  63. }
  64. #DNS Alias for the LB ( the CNAME was required. an Alias did NOT work due to aws/bug. )
  65. resource "aws_route53_record" "sensu_internal" {
  66. zone_id = var.dns_info["private"]["zone_id"]
  67. name = var.instance_name
  68. type = "CNAME"
  69. records = [aws_alb.sensu_internal.dns_name]
  70. ttl = "60"
  71. provider = aws.c2
  72. }
  73. #----------------------------------------------------------------------------
  74. # ALB Security Group
  75. #----------------------------------------------------------------------------
  76. resource "aws_security_group" "sensu_alb_server_internal" {
  77. vpc_id = var.vpc_id
  78. name = "sensu-alb-sg-internal"
  79. description = "Sensu Internal LB SG"
  80. tags = merge(var.standard_tags, var.tags)
  81. }
  82. #----------------------------------------------------------------------------
  83. # INGRESS
  84. #----------------------------------------------------------------------------
  85. resource "aws_security_group_rule" "sensu_from_vpc" {
  86. for_each = local.alb_listener_ports
  87. type = "ingress"
  88. from_port = each.value
  89. to_port = each.value
  90. protocol = "tcp"
  91. cidr_blocks = ["10.0.0.0/8"]
  92. description = "Sensu ${each.key}"
  93. security_group_id = aws_security_group.sensu_alb_server_internal.id
  94. }
  95. #----------------------------------------------------------------------------
  96. # EGRESS
  97. #----------------------------------------------------------------------------
  98. resource "aws_security_group_rule" "sensu_from_alb" {
  99. for_each = local.alb_listener_ports
  100. type = "egress"
  101. from_port = each.value
  102. to_port = each.value
  103. protocol = "tcp"
  104. source_security_group_id = aws_security_group.instance_security_group.id
  105. description = "Sensu ${each.key}"
  106. security_group_id = aws_security_group.sensu_alb_server_internal.id
  107. }
  108. #----------------------------------------------------------------------------
  109. # EXTERNAL LB
  110. #----------------------------------------------------------------------------
  111. resource "aws_alb" "sensu_external" {
  112. name = "sensu-alb-external-${var.environment}"
  113. security_groups = [ aws_security_group.sensu_alb_server_external.id ]
  114. internal = false
  115. subnets = var.public_subnets
  116. load_balancer_type = "application"
  117. access_logs {
  118. bucket = "xdr-elb-${ var.environment }"
  119. enabled = true
  120. }
  121. tags = merge(var.standard_tags, var.tags, { Name = "sensu-alb-external-${var.environment}" })
  122. }
  123. # Create a new target group
  124. resource "aws_alb_target_group" "sensu_external" {
  125. name = "sensu-alb-targets-agent-external"
  126. port = 8081
  127. protocol = "HTTPS"
  128. #deregistration_delay = "${local.lb_deregistration_delay}"
  129. vpc_id = var.vpc_id
  130. health_check {
  131. protocol = "HTTPS"
  132. port = "8080"
  133. path = "/health"
  134. matcher = "200"
  135. timeout = "4"
  136. interval = "5"
  137. }
  138. stickiness {
  139. type = "lb_cookie"
  140. enabled = false
  141. }
  142. tags = merge(var.standard_tags, var.tags)
  143. }
  144. resource "aws_lb_target_group_attachment" "sensu_external" {
  145. target_group_arn = aws_alb_target_group.sensu_external.arn
  146. target_id = aws_instance.instance.id
  147. port = 8081
  148. }
  149. # Create a new alb listener
  150. resource "aws_alb_listener" "sensu_https_external" {
  151. load_balancer_arn = aws_alb.sensu_external.arn
  152. port = "443"
  153. protocol = "HTTPS"
  154. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2019-08" # PFS, TLS1.2, most "restrictive" policy (took awhile to find that)
  155. certificate_arn = aws_acm_certificate.cert_public.arn
  156. default_action {
  157. target_group_arn = aws_alb_target_group.sensu_external.arn
  158. type = "forward"
  159. }
  160. }
  161. # #########################
  162. # # DNS Entry
  163. module "public_dns_record" {
  164. source = "../../submodules/dns/public_ALIAS_record"
  165. name = var.instance_name
  166. target_dns_name = aws_alb.sensu_external.dns_name
  167. target_zone_id = aws_alb.sensu_external.zone_id
  168. dns_info = var.dns_info
  169. providers = {
  170. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  171. }
  172. }
  173. #----------------------------------------------------------------------------
  174. # ALB Security Group
  175. #----------------------------------------------------------------------------
  176. resource "aws_security_group" "sensu_alb_server_external" {
  177. vpc_id = var.vpc_id
  178. name = "sensu-alb-sg-external"
  179. description = "Sensu LB SG"
  180. tags = merge(var.standard_tags, var.tags)
  181. }
  182. #----------------------------------------------------------------------------
  183. # INGRESS
  184. #----------------------------------------------------------------------------
  185. resource "aws_security_group_rule" "sensu-external-ips" {
  186. # This deserves some explanation. Terraform "for_each" expects to be
  187. # getting as input a map of values to iterate over as part of the foreach.
  188. # The keys of the map are used to name each of these objects created. Looking
  189. # in the terraform plan output of a for_each you'll see things like:
  190. #
  191. # aws_security_group_rule.resource_name["key-value-from-foreach"] will be created
  192. #
  193. # Our c2_services_external_ips is a list of maps, not a map of maps. The for-expression
  194. # makes a new thing that is a map of maps, where the key value is the description with
  195. # blanks removed.
  196. #
  197. # We could have made the variable more natively-friendly to for_each but this seemed
  198. # like a better solution for what we were trying to accomplish.
  199. for_each = { for s in var.c2_services_external_ips : replace(s.description,"/\\s*/","") => s }
  200. description = "Sensu - ${each.value.description}"
  201. type = "ingress"
  202. from_port = "443"
  203. to_port = "443"
  204. protocol = "tcp"
  205. cidr_blocks = each.value.cidr_blocks
  206. security_group_id = aws_security_group.sensu_alb_server_external.id
  207. }
  208. #----------------------------------------------------------------------------
  209. # EGRESS
  210. #----------------------------------------------------------------------------
  211. resource "aws_security_group_rule" "alb_to_sensu_server" {
  212. type = "egress"
  213. from_port = 8081
  214. to_port = 8081
  215. protocol = "tcp"
  216. source_security_group_id = aws_security_group.instance_security_group.id
  217. description = "Allows the ALB to talk to the Sensu servers"
  218. security_group_id = aws_security_group.sensu_alb_server_external.id
  219. }
  220. resource "aws_security_group_rule" "alb_to_sensu_health" {
  221. type = "egress"
  222. from_port = 8080
  223. to_port = 8080
  224. protocol = "tcp"
  225. source_security_group_id = aws_security_group.instance_security_group.id
  226. description = "Allows the ALB to talk to the Sensu Health check"
  227. security_group_id = aws_security_group.sensu_alb_server_external.id
  228. }