server.tf 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # Handy tool to find AMIs:
  2. # https://cloud-images.ubuntu.com/locator/ec2/
  3. #data "aws_ami" "ubuntu_pro" {
  4. # # Ubuntu Pro 18.04 Product ID: fc15dd7b-2aa0-47e5-bb05-94c75950b5de
  5. #
  6. # most_recent = true
  7. # owners = [ "513442679011" ] # This seems like it might change sometimes?
  8. #
  9. # # sadly, it looks like the image isn't named correctly in govcloud. I've given
  10. # # canonical feedback on this, but for now we'll use the ami id directly.
  11. # #filter {
  12. # # name = "name"
  13. # # values = ["ubuntu-pro/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*", ]
  14. # #}
  15. #
  16. # filter {
  17. # name = "image-id"
  18. # values = [ "ami-0fe6338c47e61cd5d" ]
  19. # }
  20. #
  21. # filter {
  22. # name = "root-device-type"
  23. # values = ["ebs"]
  24. # }
  25. #
  26. # filter {
  27. # name = "virtualization-type"
  28. # values = ["hvm"]
  29. # }
  30. #}
  31. data "aws_security_group" "typical-host" {
  32. name = "typical-host"
  33. vpc_id = var.vpc_id
  34. }
  35. # Use the default EBS key
  36. data "aws_kms_key" "ebs-key" {
  37. key_id = "alias/ebs_root_encrypt_decrypt"
  38. }
  39. resource "aws_network_interface" "vmray-server-interface" {
  40. subnet_id = var.private_subnets[0]
  41. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.vmray_server_sg.id]
  42. description = "vmray-server"
  43. tags = merge(local.standard_tags, var.tags, { Name = "vmray-server" })
  44. }
  45. ## Make /opt/vmray separate from the instance for greater margin of safety
  46. #resource "aws_ebs_volume" "server_opt_vmray" {
  47. # availability_zone = var.azs[0]
  48. # size = var.vmray_server_opt_vmray_size
  49. # type = "gp3"
  50. # encrypted = true
  51. # kms_key_id = data.aws_kms_key.ebs-key.arn
  52. #
  53. # tags = merge(local.standard_tags, var.tags, { Name = "vmray-server" })
  54. #}
  55. #
  56. # Has to be on the same partition as /tmp
  57. #resource "aws_volume_attachment" "server_opt_vmray" {
  58. # device_name = "/dev/xvdf"
  59. # volume_id = aws_ebs_volume.server_opt_vmray.id
  60. # instance_id = aws_instance.vmray-server-instance.id
  61. #}
  62. resource "aws_instance" "vmray-server-instance" {
  63. tenancy = "default"
  64. ebs_optimized = true
  65. disable_api_termination = var.instance_termination_protection
  66. instance_initiated_shutdown_behavior = "stop"
  67. instance_type = "m5a.xlarge"
  68. key_name = "msoc-build"
  69. monitoring = false
  70. iam_instance_profile = module.instance_profile.profile_id
  71. ami = data.aws_ami.ubuntu2004.image_id
  72. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  73. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  74. # that could be removed.
  75. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  76. metadata_options {
  77. http_endpoint = "enabled"
  78. # tfsec:ignore:aws-ec2-enforce-http-token-imds salt requirement?
  79. http_tokens = "optional"
  80. }
  81. root_block_device {
  82. volume_type = "gp3"
  83. volume_size = var.environment == "test" ? 60 : 500
  84. delete_on_termination = true
  85. encrypted = true
  86. kms_key_id = data.aws_kms_key.ebs-key.arn
  87. }
  88. ebs_block_device {
  89. # swap
  90. device_name = "/dev/xvdm"
  91. #volume_size = 48
  92. volume_type = "gp3"
  93. delete_on_termination = true
  94. encrypted = true
  95. kms_key_id = data.aws_kms_key.ebs-key.arn
  96. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  97. # This may prompt replacement when the AMI is updated.
  98. # See:
  99. # https://github.com/hashicorp/terraform/issues/19958
  100. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  101. snapshot_id = local.block_device_mappings["/dev/xvdm"].ebs.snapshot_id
  102. }
  103. ebs_block_device {
  104. # /home
  105. device_name = "/dev/xvdn"
  106. # volume_size = xx
  107. volume_type = "gp3"
  108. delete_on_termination = true
  109. encrypted = true
  110. kms_key_id = data.aws_kms_key.ebs-key.arn
  111. snapshot_id = local.block_device_mappings["/dev/xvdn"].ebs.snapshot_id
  112. }
  113. # ebs_block_device {
  114. # # /var
  115. # device_name = "/dev/xvdo"
  116. # # volume_size = xx
  117. # volume_type = "gp3"
  118. # delete_on_termination = true
  119. # encrypted = true
  120. # kms_key_id = data.aws_kms_key.ebs-key.arn
  121. # snapshot_id = local.block_device_mappings["/dev/xvdo"].ebs.snapshot_id
  122. # }
  123. # ebs_block_device {
  124. # # /var/tmp
  125. # device_name = "/dev/xvdp"
  126. # # volume_size = xx
  127. # volume_type = "gp3"
  128. # delete_on_termination = true
  129. # encrypted = true
  130. # kms_key_id = data.aws_kms_key.ebs-key.arn
  131. # snapshot_id = local.block_device_mappings["/dev/xvdp"].ebs.snapshot_id
  132. # }
  133. ebs_block_device {
  134. # /var/log
  135. device_name = "/dev/xvdq"
  136. # volume_size = xx
  137. volume_type = "gp3"
  138. delete_on_termination = true
  139. encrypted = true
  140. kms_key_id = data.aws_kms_key.ebs-key.arn
  141. snapshot_id = local.block_device_mappings["/dev/xvdq"].ebs.snapshot_id
  142. }
  143. ebs_block_device {
  144. # /var/log/audit
  145. device_name = "/dev/xvdr"
  146. # volume_size = xx
  147. volume_type = "gp3"
  148. delete_on_termination = true
  149. encrypted = true
  150. kms_key_id = data.aws_kms_key.ebs-key.arn
  151. snapshot_id = local.block_device_mappings["/dev/xvdr"].ebs.snapshot_id
  152. }
  153. # ebs_block_device {
  154. # # /tmp
  155. # device_name = "/dev/xvds"
  156. # volume_size = 100 # VMray extracts isos to /tmp
  157. # volume_type = "gp3"
  158. # delete_on_termination = true
  159. # encrypted = true
  160. # kms_key_id = data.aws_kms_key.ebs-key.arn
  161. # snapshot_id = local.block_device_mappings["/dev/xvds"].ebs.snapshot_id
  162. # }
  163. network_interface {
  164. device_index = 0
  165. network_interface_id = aws_network_interface.vmray-server-interface.id
  166. }
  167. user_data = data.template_cloudinit_config.cloud-init-vmray-server.rendered
  168. tags = merge(local.standard_tags, var.tags, { Name = "vmray-server" })
  169. volume_tags = merge(local.standard_tags, var.tags, { Name = "vmray-server" })
  170. }
  171. # Disabling this since we don't actually use it, but leaving this in here as
  172. # an example for future engineers, hopefully.
  173. #
  174. # Secrets example:
  175. # 1. Find the secret
  176. # 2. Get the secret
  177. # 3. Decode the json
  178. #data "aws_secretsmanager_secret" "ubuntu" {
  179. # name = "Ubuntu"
  180. # provider = aws.c2
  181. #}
  182. #
  183. #data "aws_secretsmanager_secret_version" "ubuntu" {
  184. # secret_id = data.aws_secretsmanager_secret.ubuntu.id
  185. # provider = aws.c2
  186. #}
  187. #
  188. #locals {
  189. # secret_ubuntu = jsondecode(data.aws_secretsmanager_secret_version.ubuntu.secret_string)
  190. #}
  191. # Render a multi-part cloud-init config making use of the part
  192. # above, and other source files
  193. data "template_cloudinit_config" "cloud-init-vmray-server" {
  194. gzip = true
  195. base64_encode = true
  196. # Main cloud-config configuration file.
  197. part {
  198. filename = "init.cfg"
  199. content_type = "text/cloud-config"
  200. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  201. {
  202. hostname = "vmray-server"
  203. fqdn = "vmray-server.${var.dns_info["private"]["zone"]}"
  204. environment = var.environment
  205. salt_master = local.salt_master
  206. proxy = local.proxy
  207. aws_partition = var.aws_partition
  208. aws_partition_alias = var.aws_partition_alias
  209. aws_region = var.aws_region
  210. }
  211. )
  212. }
  213. # mount /dev/xvdf at /opt/vmray
  214. #part {
  215. # content_type = "text/cloud-boothook"
  216. # content = file("${path.module}/cloud-init/opt_vmray.boothook")
  217. #}
  218. }
  219. module "private_dns_record_vmray_server" {
  220. source = "../../submodules/dns/private_A_record"
  221. name = "vmray-server"
  222. ip_addresses = [aws_instance.vmray-server-instance.private_ip]
  223. dns_info = var.dns_info
  224. reverse_enabled = true
  225. providers = {
  226. aws.c2 = aws.c2
  227. }
  228. }