main.tf 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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-manager-interface" {
  14. count = var.nessus_manager_count
  15. subnet_id = var.public_subnets[count.index % 3]
  16. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.nessus_manager.id ]
  17. description = "nessus-manager-${count.index}"
  18. tags = merge(var.standard_tags, var.tags, { Name = "nessus-manager-${count.index}" })
  19. }
  20. resource "aws_eip" "instance" {
  21. count = var.nessus_manager_count
  22. vpc = true
  23. tags = merge(var.standard_tags, var.tags, { Name = "nessus-manager-${count.index}" })
  24. }
  25. resource "aws_eip_association" "instance" {
  26. count = var.nessus_manager_count
  27. network_interface_id = aws_network_interface.nessus-manager-interface[count.index].id
  28. allocation_id = aws_eip.instance[count.index].id
  29. }
  30. resource "aws_instance" "nessus-manager-instance" {
  31. count = var.nessus_manager_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-manager-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-manager-${count.index}" })
  134. volume_tags = merge( var.standard_tags, var.tags, { Name = "nessus-manager-${count.index}" })
  135. }
  136. # Render a multi-part cloud-init config making use of the part
  137. # above, and other source files
  138. data "template_cloudinit_config" "cloud-init" {
  139. count = var.nessus_manager_count
  140. gzip = true
  141. base64_encode = true
  142. # Main cloud-config configuration file.
  143. part {
  144. filename = "init.cfg"
  145. content_type = "text/cloud-config"
  146. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  147. {
  148. hostname = "nessus-manager-${count.index}"
  149. fqdn = "nessus-manager-${count.index}.${var.dns_info["private"]["zone"]}"
  150. environment = var.environment
  151. salt_master = var.salt_master
  152. proxy = var.proxy
  153. aws_partition = var.aws_partition
  154. aws_partition_alias = var.aws_partition_alias
  155. aws_region = var.aws_region
  156. }
  157. )
  158. }
  159. # Additional parts as needed
  160. #part {
  161. # content_type = "text/x-shellscript"
  162. # content = "ffbaz"
  163. #}
  164. }
  165. module "private_dns_record_nessus-manager" {
  166. count = var.nessus_manager_count
  167. source = "../../../submodules/dns/private_A_record"
  168. name = "nessus-manager-${count.index}"
  169. ip_addresses = [ aws_instance.nessus-manager-instance[count.index].private_ip ]
  170. dns_info = var.dns_info
  171. reverse_enabled = var.reverse_enabled
  172. providers = {
  173. aws.c2 = aws.c2
  174. }
  175. }
  176. module "public_dns_record_nessus-manager" {
  177. count = var.nessus_manager_count
  178. source = "../../../submodules/dns/public_A_record"
  179. name = "nessus-manager-${count.index}"
  180. ip_addresses = [ aws_eip.instance[count.index].public_ip ]
  181. dns_info = var.dns_info
  182. providers = {
  183. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  184. }
  185. }