main.tf 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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" "phantom-server-interface" {
  14. count = var.phantom_instance_count
  15. subnet_id = var.public_subnets[0] # Phantom is on a public subnet for direct comms
  16. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.phantom_server.id ]
  17. description = "phantom-server"
  18. tags = merge(var.standard_tags, var.tags, { Name = "phantom-${count.index}" })
  19. }
  20. resource "aws_eip" "instance" {
  21. count = var.phantom_instance_count
  22. vpc = true
  23. tags = merge(var.standard_tags, var.tags, { Name = "phantom-${count.index}" })
  24. }
  25. resource "aws_eip_association" "instance" {
  26. count = var.phantom_instance_count
  27. network_interface_id = aws_network_interface.phantom-server-interface[count.index].id
  28. allocation_id = aws_eip.instance[count.index].id
  29. }
  30. resource "aws_instance" "phantom-server-instance" {
  31. count = var.phantom_instance_count
  32. tenancy = "default"
  33. ebs_optimized = true
  34. disable_api_termination = var.instance_termination_protection
  35. instance_initiated_shutdown_behavior = "stop"
  36. instance_type = var.instance_type
  37. key_name = "msoc-build"
  38. monitoring = false
  39. iam_instance_profile = "msoc-default-instance-profile"
  40. ami = local.ami_map[local.ami_selection]
  41. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  42. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  43. # that could be removed.
  44. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  45. #lifecycle { ignore_changes = [ ami, key_name, user_data ] }
  46. # These device definitions are optional, but added for clarity.
  47. root_block_device {
  48. volume_type = "gp3"
  49. # volume_size = "100"
  50. delete_on_termination = true
  51. encrypted = true
  52. kms_key_id = data.aws_kms_key.ebs-key.arn
  53. }
  54. ebs_block_device {
  55. # /opt - NOTE: Not in ami
  56. device_name = "/dev/xvdf"
  57. volume_type = "gp3"
  58. volume_size = var.environment == "test" ? 60 : 1000 # Phantom needs extra space for upgrades
  59. delete_on_termination = var.environment == "test" ? true : false # extra protection against deleting phantom drive
  60. encrypted = true
  61. kms_key_id = data.aws_kms_key.ebs-key.arn
  62. # Phantom is io intensive, so provisionign extra iops and throughput. Cost is about $50/mo
  63. iops = var.environment == "test" ? 3000 : 9000
  64. throughput = var.environment == "test" ? 125 : 375
  65. }
  66. ebs_block_device {
  67. # swap
  68. device_name = "/dev/xvdm"
  69. volume_type = "gp3"
  70. volume_size = 8
  71. delete_on_termination = true
  72. encrypted = true
  73. kms_key_id = data.aws_kms_key.ebs-key.arn
  74. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  75. # This may prompt replacement when the AMI is updated.
  76. # See:
  77. # https://github.com/hashicorp/terraform/issues/19958
  78. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  79. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  80. }
  81. ebs_block_device {
  82. # /home
  83. device_name = "/dev/xvdn"
  84. volume_type = "gp3"
  85. # volume_size = xx
  86. delete_on_termination = true
  87. encrypted = true
  88. kms_key_id = data.aws_kms_key.ebs-key.arn
  89. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  90. }
  91. ebs_block_device {
  92. # /var
  93. device_name = "/dev/xvdo"
  94. volume_type = "gp3"
  95. # volume_size = xx
  96. delete_on_termination = true
  97. encrypted = true
  98. kms_key_id = data.aws_kms_key.ebs-key.arn
  99. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  100. }
  101. ebs_block_device {
  102. # /var/tmp
  103. device_name = "/dev/xvdp"
  104. volume_type = "gp3"
  105. # volume_size = xx
  106. delete_on_termination = true
  107. encrypted = true
  108. kms_key_id = data.aws_kms_key.ebs-key.arn
  109. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  110. }
  111. ebs_block_device {
  112. # /var/log
  113. device_name = "/dev/xvdq"
  114. volume_type = "gp3"
  115. # volume_size = xx
  116. delete_on_termination = true
  117. encrypted = true
  118. kms_key_id = data.aws_kms_key.ebs-key.arn
  119. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  120. }
  121. ebs_block_device {
  122. # /var/log/audit
  123. device_name = "/dev/xvdr"
  124. volume_type = "gp3"
  125. # volume_size = xx
  126. delete_on_termination = true
  127. encrypted = true
  128. kms_key_id = data.aws_kms_key.ebs-key.arn
  129. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  130. }
  131. ebs_block_device {
  132. # /tmp
  133. device_name = "/dev/xvds"
  134. volume_type = "gp3"
  135. # volume_size = xx
  136. delete_on_termination = true
  137. encrypted = true
  138. kms_key_id = data.aws_kms_key.ebs-key.arn
  139. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  140. }
  141. network_interface {
  142. device_index = 0
  143. network_interface_id = aws_network_interface.phantom-server-interface[count.index].id
  144. }
  145. user_data = data.template_cloudinit_config.cloud-init[count.index].rendered
  146. tags = merge( var.standard_tags, var.tags, { Name = "phantom-${count.index}" })
  147. volume_tags = merge( var.standard_tags, var.tags, { Name = "phantom-${count.index}" })
  148. }
  149. # Render a multi-part cloud-init config making use of the part
  150. # above, and other source files
  151. data "template_cloudinit_config" "cloud-init" {
  152. count = var.phantom_instance_count
  153. gzip = true
  154. base64_encode = true
  155. # Main cloud-config configuration file.
  156. part {
  157. filename = "init.cfg"
  158. content_type = "text/cloud-config"
  159. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  160. {
  161. hostname = "phantom-${count.index}"
  162. fqdn = "phantom-${count.index}.${var.dns_info["private"]["zone"]}"
  163. environment = var.environment
  164. salt_master = var.salt_master
  165. proxy = var.proxy
  166. aws_partition = var.aws_partition
  167. aws_partition_alias = var.aws_partition_alias
  168. aws_region = var.aws_region
  169. }
  170. )
  171. }
  172. # mount /dev/xvdf at /opt/
  173. part {
  174. content_type = "text/cloud-boothook"
  175. content = file("${path.module}/cloud-init/opt.boothook")
  176. }
  177. }
  178. module "private_dns_record_phantom-server" {
  179. count = var.phantom_instance_count
  180. source = "../../submodules/dns/private_A_record"
  181. name = "phantom-${count.index}"
  182. ip_addresses = [ aws_instance.phantom-server-instance[count.index].private_ip ]
  183. dns_info = var.dns_info
  184. reverse_enabled = var.reverse_enabled
  185. providers = {
  186. aws.c2 = aws.c2
  187. }
  188. }