main.tf 6.8 KB

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