worker.tf 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. resource "aws_network_interface" "vmray-worker-interface" {
  2. count = local.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(local.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 = local.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. #
  17. # tags = merge(local.standard_tags, var.tags, { Name = "vmray-worker-${count.index}" })
  18. #}
  19. #resource "aws_volume_attachment" "worker_opt_vmray" {
  20. # count = local.instance_count
  21. # device_name = "/dev/xvdf"
  22. # volume_id = aws_ebs_volume.worker_opt_vmray[count.index].id
  23. # instance_id = aws_instance.vmray-worker-instance[count.index].id
  24. #}
  25. resource "aws_instance" "vmray-worker-instance" {
  26. count = local.instance_count
  27. tenancy = "default"
  28. ebs_optimized = true
  29. disable_api_termination = var.instance_termination_protection
  30. instance_initiated_shutdown_behavior = "stop"
  31. instance_type = "c5n.metal"
  32. key_name = "msoc-build"
  33. monitoring = false
  34. iam_instance_profile = module.instance_profile.profile_id
  35. ami = data.aws_ami.ubuntu2004.image_id
  36. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  37. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  38. # that could be removed.
  39. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  40. metadata_options {
  41. http_endpoint = "enabled"
  42. # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately
  43. http_tokens = "optional"
  44. }
  45. root_block_device {
  46. volume_type = "gp3"
  47. volume_size = var.environment == "prod" ? 300 : 100
  48. delete_on_termination = true
  49. encrypted = true
  50. kms_key_id = data.aws_kms_key.ebs-key.arn
  51. }
  52. # Attached separately
  53. #ebs_block_device {
  54. # # /opt/vmray
  55. # # Note: Not in AMI
  56. # device_name = "/dev/xvdf"
  57. # volume_size = var.vmray_worker_opt_vmray_size
  58. # delete_on_termination = true
  59. # encrypted = true
  60. # kms_key_id = data.aws_kms_key.ebs-key.arn
  61. #}
  62. ebs_block_device {
  63. # swap
  64. device_name = "/dev/xvdm"
  65. #volume_size = 48
  66. volume_type = "gp3"
  67. delete_on_termination = true
  68. encrypted = true
  69. kms_key_id = data.aws_kms_key.ebs-key.arn
  70. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  71. # This may prompt replacement when the AMI is updated.
  72. # See:
  73. # https://github.com/hashicorp/terraform/issues/19958
  74. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  75. snapshot_id = local.block_device_mappings["/dev/xvdm"].ebs.snapshot_id
  76. }
  77. ebs_block_device {
  78. # /home
  79. device_name = "/dev/xvdn"
  80. # volume_size = xx
  81. volume_type = "gp3"
  82. delete_on_termination = true
  83. encrypted = true
  84. kms_key_id = data.aws_kms_key.ebs-key.arn
  85. snapshot_id = local.block_device_mappings["/dev/xvdn"].ebs.snapshot_id
  86. }
  87. # ebs_block_device {
  88. # # /var
  89. # device_name = "/dev/xvdo"
  90. # # volume_size = xx
  91. # volume_type = "gp3"
  92. # delete_on_termination = true
  93. # encrypted = true
  94. # kms_key_id = data.aws_kms_key.ebs-key.arn
  95. # snapshot_id = local.block_device_mappings["/dev/xvdo"].ebs.snapshot_id
  96. # }
  97. # ebs_block_device {
  98. # # /var/tmp
  99. # device_name = "/dev/xvdp"
  100. # # volume_size = xx
  101. # volume_type = "gp3"
  102. # delete_on_termination = true
  103. # encrypted = true
  104. # kms_key_id = data.aws_kms_key.ebs-key.arn
  105. # snapshot_id = local.block_device_mappings["/dev/xvdp"].ebs.snapshot_id
  106. # }
  107. ebs_block_device {
  108. # /var/log
  109. device_name = "/dev/xvdq"
  110. # volume_size = xx
  111. volume_type = "gp3"
  112. delete_on_termination = true
  113. encrypted = true
  114. kms_key_id = data.aws_kms_key.ebs-key.arn
  115. snapshot_id = local.block_device_mappings["/dev/xvdq"].ebs.snapshot_id
  116. }
  117. ebs_block_device {
  118. # /var/log/audit
  119. device_name = "/dev/xvdr"
  120. # volume_size = xx
  121. volume_type = "gp3"
  122. delete_on_termination = true
  123. encrypted = true
  124. kms_key_id = data.aws_kms_key.ebs-key.arn
  125. snapshot_id = local.block_device_mappings["/dev/xvdr"].ebs.snapshot_id
  126. }
  127. # ebs_block_device {
  128. # # /tmp
  129. # device_name = "/dev/xvds"
  130. # volume_size = 100
  131. # volume_type = "gp3"
  132. # delete_on_termination = true
  133. # encrypted = true
  134. # kms_key_id = data.aws_kms_key.ebs-key.arn
  135. # snapshot_id = local.block_device_mappings["/dev/xvds"].ebs.snapshot_id
  136. # }
  137. network_interface {
  138. device_index = 0
  139. network_interface_id = aws_network_interface.vmray-worker-interface[count.index].id
  140. }
  141. user_data = data.template_cloudinit_config.cloud-init-vmray-worker[count.index].rendered
  142. tags = merge(local.standard_tags, var.tags, { Name = "vmray-worker-${count.index}" })
  143. volume_tags = merge(local.standard_tags, var.tags, { Name = "vmray-worker-${count.index}" })
  144. }
  145. # Render a multi-part cloud-init config making use of the part
  146. # above, and other source files
  147. data "template_cloudinit_config" "cloud-init-vmray-worker" {
  148. count = local.instance_count
  149. gzip = true
  150. base64_encode = true
  151. # Main cloud-config configuration file.
  152. part {
  153. filename = "init.cfg"
  154. content_type = "text/cloud-config"
  155. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  156. {
  157. hostname = "vmray-worker-${count.index}"
  158. fqdn = "vmray-worker-${count.index}.${var.dns_info["private"]["zone"]}"
  159. environment = var.environment
  160. salt_master = local.salt_master
  161. proxy = local.proxy
  162. aws_partition = var.aws_partition
  163. aws_partition_alias = var.aws_partition_alias
  164. aws_region = var.aws_region
  165. #ua_key = local.secret_ubuntu["ua_key"] # This is gathered in server.tf
  166. }
  167. )
  168. }
  169. # mount /dev/xvdf at /opt/vmray
  170. part {
  171. content_type = "text/cloud-boothook"
  172. content = file("${path.module}/cloud-init/opt_vmray.boothook")
  173. }
  174. }
  175. module "private_dns_record_vmray_worker" {
  176. count = local.instance_count
  177. source = "../../submodules/dns/private_A_record"
  178. name = "vmray-worker-${count.index}"
  179. ip_addresses = [aws_instance.vmray-worker-instance[count.index].private_ip]
  180. dns_info = var.dns_info
  181. reverse_enabled = true
  182. providers = {
  183. aws.c2 = aws.c2
  184. }
  185. }