main.tf 5.8 KB

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