main.tf 7.6 KB

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