worker.tf 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. }
  51. }
  52. # Render a multi-part cloud-init config making use of the part
  53. # above, and other source files
  54. data "template_cloudinit_config" "cloud-init-vmray-worker" {
  55. count = var.vmray_worker_instance_count
  56. gzip = true
  57. base64_encode = true
  58. # Main cloud-config configuration file.
  59. part {
  60. filename = "init.cfg"
  61. content_type = "text/cloud-config"
  62. content = data.template_file.cloud-init-vmray-worker[count.index].rendered
  63. }
  64. # Additional parts as needed
  65. #part {
  66. # content_type = "text/x-shellscript"
  67. # content = "ffbaz"
  68. #}
  69. }
  70. module "private_dns_record_vmray_worker" {
  71. count = var.vmray_worker_instance_count
  72. source = "../../submodules/dns/private_A_record"
  73. name = "vmray-worker-${ count.index }"
  74. ip_addresses = [ aws_instance.vmray-worker-instance[count.index].private_ip ]
  75. dns_info = var.dns_info
  76. reverse_enabled = true
  77. providers = {
  78. aws.c2 = aws.c2
  79. }
  80. }