server.tf 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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(var.standard_tags, var.tags, { Name = "vmray-server" })
  44. }
  45. resource "aws_instance" "vmray-server-instance" {
  46. tenancy = "default"
  47. ebs_optimized = true
  48. disable_api_termination = var.instance_termination_protection
  49. instance_initiated_shutdown_behavior = "stop"
  50. instance_type = var.instance_types["vmray-server"]
  51. key_name = "msoc-build"
  52. monitoring = false
  53. iam_instance_profile = "msoc-default-instance-profile"
  54. ami = data.aws_ami.ubuntu2004.image_id
  55. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  56. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  57. # that could be removed.
  58. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  59. root_block_device {
  60. volume_type = "gp3"
  61. volume_size = "60"
  62. delete_on_termination = true
  63. encrypted = true
  64. kms_key_id = data.aws_kms_key.ebs-key.arn
  65. }
  66. ebs_block_device {
  67. # swap
  68. device_name = "/dev/xvdm"
  69. #volume_size = 48
  70. volume_type = "gp3"
  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["/dev/xvdm"].ebs.snapshot_id
  80. }
  81. ebs_block_device {
  82. # /home
  83. device_name = "/dev/xvdn"
  84. # volume_size = xx
  85. volume_type = "gp3"
  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["/dev/xvdn"].ebs.snapshot_id
  90. }
  91. ebs_block_device {
  92. # /var
  93. device_name = "/dev/xvdo"
  94. # volume_size = xx
  95. volume_type = "gp3"
  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["/dev/xvdo"].ebs.snapshot_id
  100. }
  101. ebs_block_device {
  102. # /var/tmp
  103. device_name = "/dev/xvdp"
  104. # volume_size = xx
  105. volume_type = "gp3"
  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["/dev/xvdp"].ebs.snapshot_id
  110. }
  111. ebs_block_device {
  112. # /var/log
  113. device_name = "/dev/xvdq"
  114. # volume_size = xx
  115. volume_type = "gp3"
  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["/dev/xvdq"].ebs.snapshot_id
  120. }
  121. ebs_block_device {
  122. # /var/log/audit
  123. device_name = "/dev/xvdr"
  124. # volume_size = xx
  125. volume_type = "gp3"
  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["/dev/xvdr"].ebs.snapshot_id
  130. }
  131. ebs_block_device {
  132. # /tmp
  133. device_name = "/dev/xvds"
  134. volume_size = 8 # vmray requires >= 5
  135. volume_type = "gp3"
  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["/dev/xvds"].ebs.snapshot_id
  140. }
  141. network_interface {
  142. device_index = 0
  143. network_interface_id = aws_network_interface.vmray-server-interface.id
  144. }
  145. user_data = data.template_cloudinit_config.cloud-init-vmray-server.rendered
  146. tags = merge( var.standard_tags, var.tags, { Name = "vmray-server" })
  147. volume_tags = merge( var.standard_tags, var.tags, { Name = "vmray-server" })
  148. }
  149. # Disabling this since we don't actually use it, but leaving this in here as
  150. # an example for future engineers, hopefully.
  151. #
  152. # Secrets example:
  153. # 1. Find the secret
  154. # 2. Get the secret
  155. # 3. Decode the json
  156. #data "aws_secretsmanager_secret" "ubuntu" {
  157. # name = "Ubuntu"
  158. # provider = aws.c2
  159. #}
  160. #
  161. #data "aws_secretsmanager_secret_version" "ubuntu" {
  162. # secret_id = data.aws_secretsmanager_secret.ubuntu.id
  163. # provider = aws.c2
  164. #}
  165. #
  166. #locals {
  167. # secret_ubuntu = jsondecode(data.aws_secretsmanager_secret_version.ubuntu.secret_string)
  168. #}
  169. # Render a multi-part cloud-init config making use of the part
  170. # above, and other source files
  171. data "template_cloudinit_config" "cloud-init-vmray-server" {
  172. gzip = true
  173. base64_encode = true
  174. # Main cloud-config configuration file.
  175. part {
  176. filename = "init.cfg"
  177. content_type = "text/cloud-config"
  178. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  179. {
  180. hostname = "vmray-server"
  181. fqdn = "vmray-server.${var.dns_info["private"]["zone"]}"
  182. environment = var.environment
  183. salt_master = var.salt_master
  184. proxy = var.proxy
  185. aws_partition = var.aws_partition
  186. aws_partition_alias = var.aws_partition_alias
  187. aws_region = var.aws_region
  188. }
  189. )
  190. }
  191. # Additional parts as needed
  192. #part {
  193. # content_type = "text/x-shellscript"
  194. # content = "ffbaz"
  195. #}
  196. }
  197. module "private_dns_record_vmray_server" {
  198. source = "../../submodules/dns/private_A_record"
  199. name = "vmray-server"
  200. ip_addresses = [ aws_instance.vmray-server-instance.private_ip ]
  201. dns_info = var.dns_info
  202. reverse_enabled = true
  203. providers = {
  204. aws.c2 = aws.c2
  205. }
  206. }