worker.tf 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. resource "aws_instance" "vmray-worker-instance" {
  9. count = var.vmray_worker_instance_count
  10. tenancy = "default"
  11. ebs_optimized = true
  12. disable_api_termination = var.instance_termination_protection
  13. instance_initiated_shutdown_behavior = "stop"
  14. instance_type = var.instance_types["vmray-worker"]
  15. key_name = "msoc-build"
  16. monitoring = false
  17. iam_instance_profile = "msoc-default-instance-profile"
  18. ami = data.aws_ami.ubuntu2004.image_id
  19. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  20. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  21. # that could be removed.
  22. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  23. root_block_device {
  24. volume_type = "gp3"
  25. volume_size = "60"
  26. delete_on_termination = true
  27. encrypted = true
  28. kms_key_id = data.aws_kms_key.ebs-key.arn
  29. }
  30. ebs_block_device {
  31. # swap
  32. device_name = "/dev/xvdm"
  33. #volume_size = 48
  34. volume_type = "gp3"
  35. delete_on_termination = true
  36. encrypted = true
  37. kms_key_id = data.aws_kms_key.ebs-key.arn
  38. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  39. # This may prompt replacement when the AMI is updated.
  40. # See:
  41. # https://github.com/hashicorp/terraform/issues/19958
  42. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  43. snapshot_id = local.block_device_mappings["/dev/xvdm"].ebs.snapshot_id
  44. }
  45. ebs_block_device {
  46. # /home
  47. device_name = "/dev/xvdn"
  48. # volume_size = xx
  49. volume_type = "gp3"
  50. delete_on_termination = true
  51. encrypted = true
  52. kms_key_id = data.aws_kms_key.ebs-key.arn
  53. snapshot_id = local.block_device_mappings["/dev/xvdn"].ebs.snapshot_id
  54. }
  55. ebs_block_device {
  56. # /var
  57. device_name = "/dev/xvdo"
  58. # volume_size = xx
  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_id = local.block_device_mappings["/dev/xvdo"].ebs.snapshot_id
  64. }
  65. ebs_block_device {
  66. # /var/tmp
  67. device_name = "/dev/xvdp"
  68. # volume_size = xx
  69. volume_type = "gp3"
  70. delete_on_termination = true
  71. encrypted = true
  72. kms_key_id = data.aws_kms_key.ebs-key.arn
  73. snapshot_id = local.block_device_mappings["/dev/xvdp"].ebs.snapshot_id
  74. }
  75. ebs_block_device {
  76. # /var/log
  77. device_name = "/dev/xvdq"
  78. # volume_size = xx
  79. volume_type = "gp3"
  80. delete_on_termination = true
  81. encrypted = true
  82. kms_key_id = data.aws_kms_key.ebs-key.arn
  83. snapshot_id = local.block_device_mappings["/dev/xvdq"].ebs.snapshot_id
  84. }
  85. ebs_block_device {
  86. # /var/log/audit
  87. device_name = "/dev/xvdr"
  88. # volume_size = xx
  89. volume_type = "gp3"
  90. delete_on_termination = true
  91. encrypted = true
  92. kms_key_id = data.aws_kms_key.ebs-key.arn
  93. snapshot_id = local.block_device_mappings["/dev/xvdr"].ebs.snapshot_id
  94. }
  95. ebs_block_device {
  96. # /tmp
  97. device_name = "/dev/xvds"
  98. volume_size = 8 # vmray requires >= 5
  99. volume_type = "gp3"
  100. delete_on_termination = true
  101. encrypted = true
  102. kms_key_id = data.aws_kms_key.ebs-key.arn
  103. snapshot_id = local.block_device_mappings["/dev/xvds"].ebs.snapshot_id
  104. }
  105. network_interface {
  106. device_index = 0
  107. network_interface_id = aws_network_interface.vmray-worker-interface[count.index].id
  108. }
  109. user_data = data.template_cloudinit_config.cloud-init-vmray-worker[count.index].rendered
  110. tags = merge( var.standard_tags, var.tags, { Name = "vmray-worker-${ count.index }" })
  111. volume_tags = merge( var.standard_tags, var.tags, { Name = "vmray-worker-${ count.index }" })
  112. }
  113. # Render a multi-part cloud-init config making use of the part
  114. # above, and other source files
  115. data "template_cloudinit_config" "cloud-init-vmray-worker" {
  116. count = var.vmray_worker_instance_count
  117. gzip = true
  118. base64_encode = true
  119. # Main cloud-config configuration file.
  120. part {
  121. filename = "init.cfg"
  122. content_type = "text/cloud-config"
  123. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  124. {
  125. hostname = "vmray-worker-${ count.index }"
  126. fqdn = "vmray-worker-${ count.index }.${var.dns_info["private"]["zone"]}"
  127. environment = var.environment
  128. salt_master = var.salt_master
  129. proxy = var.proxy
  130. aws_partition = var.aws_partition
  131. aws_partition_alias = var.aws_partition_alias
  132. aws_region = var.aws_region
  133. #ua_key = local.secret_ubuntu["ua_key"] # This is gathered in server.tf
  134. }
  135. )
  136. }
  137. # Additional parts as needed
  138. #part {
  139. # content_type = "text/x-shellscript"
  140. # content = "ffbaz"
  141. #}
  142. }
  143. module "private_dns_record_vmray_worker" {
  144. count = var.vmray_worker_instance_count
  145. source = "../../submodules/dns/private_A_record"
  146. name = "vmray-worker-${ count.index }"
  147. ip_addresses = [ aws_instance.vmray-worker-instance[count.index].private_ip ]
  148. dns_info = var.dns_info
  149. reverse_enabled = true
  150. providers = {
  151. aws.c2 = aws.c2
  152. }
  153. }