main.tf 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 : 500 # 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. }
  63. ebs_block_device {
  64. # swap
  65. device_name = "/dev/xvdm"
  66. volume_type = "gp3"
  67. volume_size = 8
  68. delete_on_termination = true
  69. encrypted = true
  70. kms_key_id = data.aws_kms_key.ebs-key.arn
  71. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  72. # This may prompt replacement when the AMI is updated.
  73. # See:
  74. # https://github.com/hashicorp/terraform/issues/19958
  75. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  76. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  77. }
  78. ebs_block_device {
  79. # /home
  80. device_name = "/dev/xvdn"
  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/xvdn"].ebs.snapshot_id
  87. }
  88. ebs_block_device {
  89. # /var
  90. device_name = "/dev/xvdo"
  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/xvdo"].ebs.snapshot_id
  97. }
  98. ebs_block_device {
  99. # /var/tmp
  100. device_name = "/dev/xvdp"
  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/xvdp"].ebs.snapshot_id
  107. }
  108. ebs_block_device {
  109. # /var/log
  110. device_name = "/dev/xvdq"
  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/xvdq"].ebs.snapshot_id
  117. }
  118. ebs_block_device {
  119. # /var/log/audit
  120. device_name = "/dev/xvdr"
  121. volume_type = "gp3"
  122. # volume_size = xx
  123. delete_on_termination = true
  124. encrypted = true
  125. kms_key_id = data.aws_kms_key.ebs-key.arn
  126. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  127. }
  128. ebs_block_device {
  129. # /tmp
  130. device_name = "/dev/xvds"
  131. volume_type = "gp3"
  132. # volume_size = xx
  133. delete_on_termination = true
  134. encrypted = true
  135. kms_key_id = data.aws_kms_key.ebs-key.arn
  136. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  137. }
  138. network_interface {
  139. device_index = 0
  140. network_interface_id = aws_network_interface.phantom-server-interface[count.index].id
  141. }
  142. user_data = data.template_cloudinit_config.cloud-init[count.index].rendered
  143. tags = merge( var.standard_tags, var.tags, { Name = "phantom-${count.index}" })
  144. volume_tags = merge( var.standard_tags, var.tags, { Name = "phantom-${count.index}" })
  145. }
  146. data "template_file" "cloud-init" {
  147. count = var.phantom_instance_count
  148. template = file("${path.module}/cloud-init/cloud-init.tpl")
  149. vars = {
  150. hostname = "phantom-${count.index}"
  151. fqdn = "phantom-${count.index}.${var.dns_info["private"]["zone"]}"
  152. environment = var.environment
  153. salt_master = var.salt_master
  154. proxy = var.proxy
  155. aws_partition = var.aws_partition
  156. aws_partition_alias = var.aws_partition_alias
  157. aws_region = var.aws_region
  158. }
  159. }
  160. # Render a multi-part cloud-init config making use of the part
  161. # above, and other source files
  162. data "template_cloudinit_config" "cloud-init" {
  163. count = var.phantom_instance_count
  164. gzip = true
  165. base64_encode = true
  166. # Main cloud-config configuration file.
  167. part {
  168. filename = "init.cfg"
  169. content_type = "text/cloud-config"
  170. content = data.template_file.cloud-init[count.index].rendered
  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. }