main.tf 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. }
  5. data "aws_security_group" "typical-host" {
  6. name = "typical-host"
  7. vpc_id = var.vpc_id
  8. }
  9. # Use the default EBS key
  10. data "aws_kms_key" "ebs-key" {
  11. key_id = "alias/ebs_root_encrypt_decrypt"
  12. }
  13. resource "aws_network_interface" "nessus-receiver-interface" {
  14. count = var.nessus_receiver_count
  15. subnet_id = var.public_subnets[count.index % 3]
  16. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.nessus_receiver.id ]
  17. description = "nessus-receiver-${count.index}"
  18. tags = merge(var.standard_tags, var.tags, { Name = "nessus-receiver-${count.index}" })
  19. }
  20. resource "aws_eip" "instance" {
  21. count = var.nessus_receiver_count
  22. vpc = true
  23. tags = merge(var.standard_tags, var.tags, { Name = "nessus-receiver-${count.index}" })
  24. }
  25. resource "aws_eip_association" "instance" {
  26. count = var.nessus_receiver_count
  27. network_interface_id = aws_network_interface.nessus-receiver-interface[count.index].id
  28. allocation_id = aws_eip.instance[count.index].id
  29. }
  30. resource "aws_instance" "nessus-receiver-instance" {
  31. count = var.nessus_receiver_count
  32. tenancy = "default"
  33. ebs_optimized = true
  34. disable_api_termination = var.instance_termination_protection
  35. instance_initiated_shutdown_behavior = "stop"
  36. instance_type = var.instance_type
  37. key_name = "msoc-build"
  38. monitoring = false
  39. iam_instance_profile = "msoc-default-instance-profile"
  40. ami = local.ami_map[local.ami_selection]
  41. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  42. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  43. # that could be removed.
  44. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  45. # These device definitions are optional, but added for clarity.
  46. root_block_device {
  47. volume_type = "gp3"
  48. volume_size = "40"
  49. delete_on_termination = true
  50. encrypted = true
  51. kms_key_id = data.aws_kms_key.ebs-key.arn
  52. }
  53. ebs_block_device {
  54. # swap
  55. device_name = "/dev/xvdm"
  56. volume_type = "gp3"
  57. volume_size = 8
  58. delete_on_termination = true
  59. encrypted = true
  60. kms_key_id = data.aws_kms_key.ebs-key.arn
  61. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  62. # This may prompt replacement when the AMI is updated.
  63. # See:
  64. # https://github.com/hashicorp/terraform/issues/19958
  65. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  66. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  67. }
  68. ebs_block_device {
  69. # /home
  70. device_name = "/dev/xvdn"
  71. volume_type = "gp3"
  72. # volume_size = xx
  73. delete_on_termination = true
  74. encrypted = true
  75. kms_key_id = data.aws_kms_key.ebs-key.arn
  76. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  77. }
  78. ebs_block_device {
  79. # /var
  80. device_name = "/dev/xvdo"
  81. volume_type = "gp3"
  82. # volume_size = xx
  83. delete_on_termination = true
  84. encrypted = true
  85. kms_key_id = data.aws_kms_key.ebs-key.arn
  86. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  87. }
  88. ebs_block_device {
  89. # /var/tmp
  90. device_name = "/dev/xvdp"
  91. volume_type = "gp3"
  92. # volume_size = xx
  93. delete_on_termination = true
  94. encrypted = true
  95. kms_key_id = data.aws_kms_key.ebs-key.arn
  96. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  97. }
  98. ebs_block_device {
  99. # /var/log
  100. device_name = "/dev/xvdq"
  101. volume_type = "gp3"
  102. # volume_size = xx
  103. delete_on_termination = true
  104. encrypted = true
  105. kms_key_id = data.aws_kms_key.ebs-key.arn
  106. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  107. }
  108. ebs_block_device {
  109. # /var/log/audit
  110. device_name = "/dev/xvdr"
  111. volume_type = "gp3"
  112. # volume_size = xx
  113. delete_on_termination = true
  114. encrypted = true
  115. kms_key_id = data.aws_kms_key.ebs-key.arn
  116. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  117. }
  118. ebs_block_device {
  119. # /tmp
  120. device_name = "/dev/xvds"
  121. volume_type = "gp3"
  122. # volume_size = xx
  123. delete_on_termination = true
  124. encrypted = true
  125. kms_key_id = data.aws_kms_key.ebs-key.arn
  126. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  127. }
  128. network_interface {
  129. device_index = 0
  130. network_interface_id = aws_network_interface.nessus-receiver-interface[count.index].id
  131. }
  132. user_data = data.template_cloudinit_config.cloud-init[count.index].rendered
  133. tags = merge( var.standard_tags, var.tags, { Name = "nessus-receiver-${count.index}" })
  134. volume_tags = merge( var.standard_tags, var.tags, { Name = "nessus-receiver-${count.index}" })
  135. }
  136. data "template_file" "cloud-init" {
  137. count = var.nessus_receiver_count
  138. template = file("${path.module}/cloud-init/cloud-init.tpl")
  139. vars = {
  140. hostname = "nessus-receiver-${count.index}"
  141. fqdn = "nessus-receiver-${count.index}.${var.dns_info["private"]["zone"]}"
  142. environment = var.environment
  143. salt_master = var.salt_master
  144. proxy = var.proxy
  145. aws_partition = var.aws_partition
  146. aws_partition_alias = var.aws_partition_alias
  147. aws_region = var.aws_region
  148. }
  149. }
  150. # Render a multi-part cloud-init config making use of the part
  151. # above, and other source files
  152. data "template_cloudinit_config" "cloud-init" {
  153. count = var.nessus_receiver_count
  154. gzip = true
  155. base64_encode = true
  156. # Main cloud-config configuration file.
  157. part {
  158. filename = "init.cfg"
  159. content_type = "text/cloud-config"
  160. content = data.template_file.cloud-init[count.index].rendered
  161. }
  162. # Additional parts as needed
  163. #part {
  164. # content_type = "text/x-shellscript"
  165. # content = "ffbaz"
  166. #}
  167. }
  168. module "private_dns_record_nessus-receiver" {
  169. count = var.nessus_receiver_count
  170. source = "../../../submodules/dns/private_A_record"
  171. name = "nessus-receiver-${count.index}"
  172. ip_addresses = [ aws_instance.nessus-receiver-instance[count.index].private_ip ]
  173. dns_info = var.dns_info
  174. reverse_enabled = var.reverse_enabled
  175. providers = {
  176. aws.c2 = aws.c2
  177. }
  178. }
  179. module "public_dns_record_nessus-receiver" {
  180. count = var.nessus_receiver_count
  181. source = "../../../submodules/dns/public_A_record"
  182. name = "nessus-receiver-${count.index}"
  183. ip_addresses = [ aws_eip.instance[count.index].public_ip ]
  184. dns_info = var.dns_info
  185. providers = {
  186. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  187. }
  188. }