worker.tf 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. ebs_block_device {
  47. # /opt/vmray
  48. # Note: Not in AMI
  49. device_name = "/dev/xvdf"
  50. volume_size = var.vmray_worker_opt_vmray_size
  51. delete_on_termination = true
  52. encrypted = true
  53. kms_key_id = data.aws_kms_key.ebs-key.arn
  54. }
  55. ebs_block_device {
  56. # swap
  57. device_name = "/dev/xvdm"
  58. #volume_size = 48
  59. volume_type = "gp3"
  60. delete_on_termination = true
  61. encrypted = true
  62. kms_key_id = data.aws_kms_key.ebs-key.arn
  63. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  64. # This may prompt replacement when the AMI is updated.
  65. # See:
  66. # https://github.com/hashicorp/terraform/issues/19958
  67. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  68. snapshot_id = local.block_device_mappings["/dev/xvdm"].ebs.snapshot_id
  69. }
  70. ebs_block_device {
  71. # /home
  72. device_name = "/dev/xvdn"
  73. # volume_size = xx
  74. volume_type = "gp3"
  75. delete_on_termination = true
  76. encrypted = true
  77. kms_key_id = data.aws_kms_key.ebs-key.arn
  78. snapshot_id = local.block_device_mappings["/dev/xvdn"].ebs.snapshot_id
  79. }
  80. ebs_block_device {
  81. # /var
  82. device_name = "/dev/xvdo"
  83. # volume_size = xx
  84. volume_type = "gp3"
  85. delete_on_termination = true
  86. encrypted = true
  87. kms_key_id = data.aws_kms_key.ebs-key.arn
  88. snapshot_id = local.block_device_mappings["/dev/xvdo"].ebs.snapshot_id
  89. }
  90. ebs_block_device {
  91. # /var/tmp
  92. device_name = "/dev/xvdp"
  93. # volume_size = xx
  94. volume_type = "gp3"
  95. delete_on_termination = true
  96. encrypted = true
  97. kms_key_id = data.aws_kms_key.ebs-key.arn
  98. snapshot_id = local.block_device_mappings["/dev/xvdp"].ebs.snapshot_id
  99. }
  100. ebs_block_device {
  101. # /var/log
  102. device_name = "/dev/xvdq"
  103. # volume_size = xx
  104. volume_type = "gp3"
  105. delete_on_termination = true
  106. encrypted = true
  107. kms_key_id = data.aws_kms_key.ebs-key.arn
  108. snapshot_id = local.block_device_mappings["/dev/xvdq"].ebs.snapshot_id
  109. }
  110. ebs_block_device {
  111. # /var/log/audit
  112. device_name = "/dev/xvdr"
  113. # volume_size = xx
  114. volume_type = "gp3"
  115. delete_on_termination = true
  116. encrypted = true
  117. kms_key_id = data.aws_kms_key.ebs-key.arn
  118. snapshot_id = local.block_device_mappings["/dev/xvdr"].ebs.snapshot_id
  119. }
  120. ebs_block_device {
  121. # /tmp
  122. device_name = "/dev/xvds"
  123. volume_size = 100
  124. volume_type = "gp3"
  125. delete_on_termination = true
  126. encrypted = true
  127. kms_key_id = data.aws_kms_key.ebs-key.arn
  128. snapshot_id = local.block_device_mappings["/dev/xvds"].ebs.snapshot_id
  129. }
  130. network_interface {
  131. device_index = 0
  132. network_interface_id = aws_network_interface.vmray-worker-interface[count.index].id
  133. }
  134. user_data = data.template_cloudinit_config.cloud-init-vmray-worker[count.index].rendered
  135. tags = merge( var.standard_tags, var.tags, { Name = "vmray-worker-${ count.index }" })
  136. volume_tags = merge( var.standard_tags, var.tags, { Name = "vmray-worker-${ count.index }" })
  137. }
  138. # Render a multi-part cloud-init config making use of the part
  139. # above, and other source files
  140. data "template_cloudinit_config" "cloud-init-vmray-worker" {
  141. count = var.vmray_worker_instance_count
  142. gzip = true
  143. base64_encode = true
  144. # Main cloud-config configuration file.
  145. part {
  146. filename = "init.cfg"
  147. content_type = "text/cloud-config"
  148. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  149. {
  150. hostname = "vmray-worker-${ count.index }"
  151. fqdn = "vmray-worker-${ 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. #ua_key = local.secret_ubuntu["ua_key"] # This is gathered in server.tf
  159. }
  160. )
  161. }
  162. # mount /dev/xvdf at /opt/vmray
  163. part {
  164. content_type = "text/cloud-boothook"
  165. content = file("${path.module}/cloud-init/opt_vmray.boothook")
  166. }
  167. }
  168. module "private_dns_record_vmray_worker" {
  169. count = var.vmray_worker_instance_count
  170. source = "../../submodules/dns/private_A_record"
  171. name = "vmray-worker-${ count.index }"
  172. ip_addresses = [ aws_instance.vmray-worker-instance[count.index].private_ip ]
  173. dns_info = var.dns_info
  174. reverse_enabled = true
  175. providers = {
  176. aws.c2 = aws.c2
  177. }
  178. }