main.tf 8.1 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" "phantom-server-interface" {
  16. count = local.instance_count
  17. subnet_id = var.public_subnets[0] # Phantom is on a public subnet for direct comms
  18. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.phantom_server.id]
  19. description = "phantom-server"
  20. tags = merge(local.standard_tags, var.tags, { Name = "phantom-${count.index}" })
  21. }
  22. resource "aws_eip" "instance" {
  23. # checkov:skip=CKV2_AWS_19: EIPs are attached to VPC
  24. count = local.instance_count
  25. vpc = true
  26. tags = merge(local.standard_tags, var.tags, { Name = "phantom-${count.index}" })
  27. }
  28. resource "aws_eip_association" "instance" {
  29. count = local.instance_count
  30. network_interface_id = aws_network_interface.phantom-server-interface[count.index].id
  31. allocation_id = aws_eip.instance[count.index].id
  32. }
  33. resource "aws_instance" "phantom-server-instance" {
  34. count = local.instance_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 = var.environment == "prod" ? "m5a.4xlarge" : "t3a.xlarge"
  40. key_name = "msoc-build"
  41. monitoring = false # checkov:skip=CKV_AWS_126:Detailed monitoring not needed at this time
  42. iam_instance_profile = module.instance_profile.profile_id
  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. #lifecycle { ignore_changes = [ ami, key_name, user_data ] }
  55. # These device definitions are optional, but added for clarity.
  56. root_block_device {
  57. volume_type = "gp3"
  58. # volume_size = "100"
  59. delete_on_termination = true
  60. encrypted = true
  61. kms_key_id = data.aws_kms_key.ebs-key.arn
  62. }
  63. ebs_block_device {
  64. # /opt - NOTE: Not in ami
  65. device_name = "/dev/xvdf"
  66. volume_type = "gp3"
  67. volume_size = var.environment == "test" ? 60 : 1000 # Phantom needs extra space for upgrades
  68. delete_on_termination = var.environment == "test" ? true : false # extra protection against deleting phantom drive
  69. encrypted = true
  70. kms_key_id = data.aws_kms_key.ebs-key.arn
  71. # Phantom is io intensive, so provisionign extra iops and throughput. Cost is about $50/mo
  72. iops = var.environment == "test" ? 3000 : 9000
  73. throughput = var.environment == "test" ? 125 : 375
  74. }
  75. ebs_block_device {
  76. # swap
  77. device_name = "/dev/xvdm"
  78. volume_type = "gp3"
  79. volume_size = 8
  80. delete_on_termination = true
  81. encrypted = true
  82. kms_key_id = data.aws_kms_key.ebs-key.arn
  83. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  84. # This may prompt replacement when the AMI is updated.
  85. # See:
  86. # https://github.com/hashicorp/terraform/issues/19958
  87. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  88. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  89. }
  90. ebs_block_device {
  91. # /home
  92. device_name = "/dev/xvdn"
  93. volume_type = "gp3"
  94. volume_size = 8
  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/xvdn"].ebs.snapshot_id
  99. }
  100. ebs_block_device {
  101. # /var
  102. device_name = "/dev/xvdo"
  103. volume_type = "gp3"
  104. volume_size = 30
  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/xvdo"].ebs.snapshot_id
  109. }
  110. ebs_block_device {
  111. # /var/tmp
  112. device_name = "/dev/xvdp"
  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/xvdp"].ebs.snapshot_id
  119. }
  120. ebs_block_device {
  121. # /var/log
  122. device_name = "/dev/xvdq"
  123. volume_type = "gp3"
  124. volume_size = 30
  125. delete_on_termination = true
  126. encrypted = true
  127. kms_key_id = data.aws_kms_key.ebs-key.arn
  128. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  129. }
  130. ebs_block_device {
  131. # /var/log/audit
  132. device_name = "/dev/xvdr"
  133. volume_type = "gp3"
  134. # volume_size = xx
  135. delete_on_termination = true
  136. encrypted = true
  137. kms_key_id = data.aws_kms_key.ebs-key.arn
  138. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  139. }
  140. ebs_block_device {
  141. # /tmp
  142. device_name = "/dev/xvds"
  143. volume_type = "gp3"
  144. # volume_size = xx
  145. delete_on_termination = true
  146. encrypted = true
  147. kms_key_id = data.aws_kms_key.ebs-key.arn
  148. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  149. }
  150. network_interface {
  151. device_index = 0
  152. network_interface_id = aws_network_interface.phantom-server-interface[count.index].id
  153. }
  154. user_data = data.template_cloudinit_config.cloud-init[count.index].rendered
  155. tags = merge(local.standard_tags, var.tags, { Name = "phantom-${count.index}" })
  156. volume_tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = "phantom-${count.index}" })
  157. }
  158. # Render a multi-part cloud-init config making use of the part
  159. # above, and other source files
  160. data "template_cloudinit_config" "cloud-init" {
  161. count = local.instance_count
  162. gzip = true
  163. base64_encode = true
  164. # Main cloud-config configuration file.
  165. part {
  166. filename = "init.cfg"
  167. content_type = "text/cloud-config"
  168. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  169. {
  170. hostname = "phantom-${count.index}"
  171. fqdn = "phantom-${count.index}.${var.dns_info["private"]["zone"]}"
  172. environment = var.environment
  173. salt_master = local.salt_master
  174. proxy = local.proxy
  175. aws_partition = var.aws_partition
  176. aws_partition_alias = var.aws_partition_alias
  177. aws_region = var.aws_region
  178. }
  179. )
  180. }
  181. # mount /dev/xvdf at /opt/
  182. part {
  183. content_type = "text/cloud-boothook"
  184. content = file("${path.module}/cloud-init/opt.boothook")
  185. }
  186. }
  187. module "private_dns_record_phantom-server" {
  188. count = local.instance_count
  189. source = "../../submodules/dns/private_A_record"
  190. name = "phantom-${count.index}"
  191. ip_addresses = [aws_instance.phantom-server-instance[count.index].private_ip]
  192. dns_info = var.dns_info
  193. reverse_enabled = var.reverse_enabled
  194. providers = {
  195. aws.c2 = aws.c2
  196. }
  197. }