main.tf 6.2 KB

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