worker.tf 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. resource "aws_network_interface" "vmray-worker-interface" {
  2. count = var.vmray_worker_instance_count
  3. subnet_id = var.private_subnets[count.index % 3]
  4. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.vmray_worker_sg.id ]
  5. description = "vmray-worker"
  6. tags = merge(var.standard_tags, var.tags, { Name = "vmray-worker" })
  7. }
  8. # Make /opt/vmray separate from the instance for greater margin of safety
  9. resource "aws_ebs_volume" "worker_opt_vmray" {
  10. count = var.vmray_worker_instance_count
  11. availability_zone = var.azs[count.index % 3]
  12. size = var.vmray_worker_opt_vmray_size
  13. type = "gp3"
  14. encrypted = true
  15. kms_key_id = data.aws_kms_key.ebs-key.arn
  16. tags = merge(var.standard_tags, var.tags, { Name = "vmray-worker-${count.index}" })
  17. }
  18. resource "aws_volume_attachment" "worker_opt_vmray" {
  19. count = var.vmray_worker_instance_count
  20. device_name = "/dev/xvdf"
  21. volume_id = aws_ebs_volume.worker_opt_vmray[count.index].id
  22. instance_id = aws_instance.vmray-worker-instance[count.index].id
  23. }
  24. resource "aws_instance" "vmray-worker-instance" {
  25. count = var.vmray_worker_instance_count
  26. tenancy = "default"
  27. ebs_optimized = true
  28. disable_api_termination = var.instance_termination_protection
  29. instance_initiated_shutdown_behavior = "stop"
  30. instance_type = var.instance_types["vmray-worker"]
  31. key_name = "msoc-build"
  32. monitoring = false
  33. iam_instance_profile = "msoc-default-instance-profile"
  34. ami = data.aws_ami.ubuntu2004.image_id
  35. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  36. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  37. # that could be removed.
  38. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  39. root_block_device {
  40. volume_type = "gp3"
  41. volume_size = "60"
  42. delete_on_termination = true
  43. encrypted = true
  44. kms_key_id = data.aws_kms_key.ebs-key.arn
  45. }
  46. # Attached separately
  47. #ebs_block_device {
  48. # # /opt/vmray
  49. # # Note: Not in AMI
  50. # device_name = "/dev/xvdf"
  51. # volume_size = var.vmray_worker_opt_vmray_size
  52. # delete_on_termination = true
  53. # encrypted = true
  54. # kms_key_id = data.aws_kms_key.ebs-key.arn
  55. #}
  56. ebs_block_device {
  57. # swap
  58. device_name = "/dev/xvdm"
  59. #volume_size = 48
  60. volume_type = "gp3"
  61. delete_on_termination = true
  62. encrypted = true
  63. kms_key_id = data.aws_kms_key.ebs-key.arn
  64. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  65. # This may prompt replacement when the AMI is updated.
  66. # See:
  67. # https://github.com/hashicorp/terraform/issues/19958
  68. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  69. snapshot_id = local.block_device_mappings["/dev/xvdm"].ebs.snapshot_id
  70. }
  71. ebs_block_device {
  72. # /home
  73. device_name = "/dev/xvdn"
  74. # volume_size = xx
  75. volume_type = "gp3"
  76. delete_on_termination = true
  77. encrypted = true
  78. kms_key_id = data.aws_kms_key.ebs-key.arn
  79. snapshot_id = local.block_device_mappings["/dev/xvdn"].ebs.snapshot_id
  80. }
  81. ebs_block_device {
  82. # /var
  83. device_name = "/dev/xvdo"
  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/xvdo"].ebs.snapshot_id
  90. }
  91. ebs_block_device {
  92. # /var/tmp
  93. device_name = "/dev/xvdp"
  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/xvdp"].ebs.snapshot_id
  100. }
  101. ebs_block_device {
  102. # /var/log
  103. device_name = "/dev/xvdq"
  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/xvdq"].ebs.snapshot_id
  110. }
  111. ebs_block_device {
  112. # /var/log/audit
  113. device_name = "/dev/xvdr"
  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/xvdr"].ebs.snapshot_id
  120. }
  121. ebs_block_device {
  122. # /tmp
  123. device_name = "/dev/xvds"
  124. volume_size = 100
  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/xvds"].ebs.snapshot_id
  130. }
  131. network_interface {
  132. device_index = 0
  133. network_interface_id = aws_network_interface.vmray-worker-interface[count.index].id
  134. }
  135. user_data = data.template_cloudinit_config.cloud-init-vmray-worker[count.index].rendered
  136. tags = merge( var.standard_tags, var.tags, { Name = "vmray-worker-${ count.index }" })
  137. volume_tags = merge( var.standard_tags, var.tags, { Name = "vmray-worker-${ count.index }" })
  138. }
  139. # Render a multi-part cloud-init config making use of the part
  140. # above, and other source files
  141. data "template_cloudinit_config" "cloud-init-vmray-worker" {
  142. count = var.vmray_worker_instance_count
  143. gzip = true
  144. base64_encode = true
  145. # Main cloud-config configuration file.
  146. part {
  147. filename = "init.cfg"
  148. content_type = "text/cloud-config"
  149. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  150. {
  151. hostname = "vmray-worker-${ count.index }"
  152. fqdn = "vmray-worker-${ count.index }.${var.dns_info["private"]["zone"]}"
  153. environment = var.environment
  154. salt_master = var.salt_master
  155. proxy = var.proxy
  156. aws_partition = var.aws_partition
  157. aws_partition_alias = var.aws_partition_alias
  158. aws_region = var.aws_region
  159. #ua_key = local.secret_ubuntu["ua_key"] # This is gathered in server.tf
  160. }
  161. )
  162. }
  163. # mount /dev/xvdf at /opt/vmray
  164. part {
  165. content_type = "text/cloud-boothook"
  166. content = file("${path.module}/cloud-init/opt_vmray.boothook")
  167. }
  168. }
  169. module "private_dns_record_vmray_worker" {
  170. count = var.vmray_worker_instance_count
  171. source = "../../submodules/dns/private_A_record"
  172. name = "vmray-worker-${ count.index }"
  173. ip_addresses = [ aws_instance.vmray-worker-instance[count.index].private_ip ]
  174. dns_info = var.dns_info
  175. reverse_enabled = true
  176. providers = {
  177. aws.c2 = aws.c2
  178. }
  179. }