main.tf 5.9 KB

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