elb.tf 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.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.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 = "8081"
  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. #count = 0 in test. No need to let customers connect to test.
  186. resource "aws_security_group_rule" "sensu-afs-pop" {
  187. count = var.environment == "test" ? 0 : 1
  188. description = "Sensu - AFS POP"
  189. type = "ingress"
  190. from_port = "443"
  191. to_port = "443"
  192. protocol = "tcp"
  193. cidr_blocks = var.afs_pop
  194. security_group_id = aws_security_group.sensu_alb_server_external.id
  195. }
  196. #count = 0 in test. No need to let customers connect to test.
  197. resource "aws_security_group_rule" "sensu-afs-azure-pop" {
  198. count = var.environment == "test" ? 0 : 1
  199. description = "Sensu - AFS Azure POP"
  200. type = "ingress"
  201. from_port = "443"
  202. to_port = "443"
  203. protocol = "tcp"
  204. cidr_blocks = var.afs_azure_pop
  205. security_group_id = aws_security_group.sensu_alb_server_external.id
  206. }
  207. #count = 0 in test. No need to let customers connect to test.
  208. resource "aws_security_group_rule" "sensu-nga-pop" {
  209. count = var.environment == "test" ? 0 : 1
  210. description = "Sensu - NGA POP"
  211. type = "ingress"
  212. from_port = "443"
  213. to_port = "443"
  214. protocol = "tcp"
  215. cidr_blocks = var.nga_pop
  216. security_group_id = aws_security_group.sensu_alb_server_external.id
  217. }
  218. #----------------------------------------------------------------------------
  219. # EGRESS
  220. #----------------------------------------------------------------------------
  221. resource "aws_security_group_rule" "alb_to_sensu_server" {
  222. type = "egress"
  223. from_port = 8081
  224. to_port = 8081
  225. protocol = "tcp"
  226. source_security_group_id = aws_security_group.instance_security_group.id
  227. description = "Allows the ALB to talk to the Sensu servers"
  228. security_group_id = aws_security_group.sensu_alb_server_external.id
  229. }
  230. resource "aws_security_group_rule" "alb_to_sensu_health" {
  231. type = "egress"
  232. from_port = 8080
  233. to_port = 8080
  234. protocol = "tcp"
  235. source_security_group_id = aws_security_group.instance_security_group.id
  236. description = "Allows the ALB to talk to the Sensu Health check"
  237. security_group_id = aws_security_group.sensu_alb_server_external.id
  238. }