main.tf 5.9 KB

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