worker.tf 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. network_interface {
  31. device_index = 0
  32. network_interface_id = aws_network_interface.vmray-worker-interface[count.index].id
  33. }
  34. user_data = data.template_cloudinit_config.cloud-init-vmray-worker[count.index].rendered
  35. tags = merge( var.standard_tags, var.tags, { Name = "vmray-worker-${ count.index }" })
  36. volume_tags = merge( var.standard_tags, var.tags, { Name = "vmray-worker-${ count.index }" })
  37. }
  38. data "template_file" "cloud-init-vmray-worker" {
  39. count = var.vmray_worker_instance_count
  40. template = file("${path.module}/cloud-init/cloud-init.tpl")
  41. vars = {
  42. hostname = "vmray-worker-${ count.index }"
  43. fqdn = "vmray-worker-${ count.index }.${var.dns_info["private"]["zone"]}"
  44. environment = var.environment
  45. salt_master = var.salt_master
  46. proxy = var.proxy
  47. aws_partition = var.aws_partition
  48. aws_partition_alias = var.aws_partition_alias
  49. aws_region = var.aws_region
  50. ua_key = local.secret_ubuntu["ua_key"] # This is gathered in server.tf
  51. }
  52. }
  53. # Render a multi-part cloud-init config making use of the part
  54. # above, and other source files
  55. data "template_cloudinit_config" "cloud-init-vmray-worker" {
  56. count = var.vmray_worker_instance_count
  57. gzip = true
  58. base64_encode = true
  59. # Main cloud-config configuration file.
  60. part {
  61. filename = "init.cfg"
  62. content_type = "text/cloud-config"
  63. content = data.template_file.cloud-init-vmray-worker[count.index].rendered
  64. }
  65. # Additional parts as needed
  66. #part {
  67. # content_type = "text/x-shellscript"
  68. # content = "ffbaz"
  69. #}
  70. }
  71. module "private_dns_record_vmray_worker" {
  72. count = var.vmray_worker_instance_count
  73. source = "../../submodules/dns/private_A_record"
  74. name = "vmray-worker-${ count.index }"
  75. ip_addresses = [ aws_instance.vmray-worker-instance[count.index].private_ip ]
  76. dns_info = var.dns_info
  77. reverse_enabled = true
  78. providers = {
  79. aws.c2 = aws.c2
  80. }
  81. }