main.tf 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # The part of a Twingate connector that is the same on every cloud: the
  2. # Twingate connector and its tokens, and the cloud-init that hardens the machine
  3. # and installs the connector. Each provider's connector module calls this one,
  4. # so a hardening fix reaches every cloud at once. It creates no cloud resource.
  5. resource "twingate_connector" "this" {
  6. remote_network_id = var.remote_network_id
  7. name = "${var.layer}-${local.name_key}-connector-${var.instance}"
  8. # Provider-side, not in user_data. Unset it is null, which the provider treats
  9. # as "not configured": the resource config is then exactly what it was before
  10. # this setting existed, so the default plan cannot differ. Set to true or
  11. # false it is sent.
  12. status_updates_enabled = var.connector_telemetry.status_updates_enabled
  13. }
  14. resource "twingate_connector_tokens" "this" {
  15. connector_id = twingate_connector.this.id
  16. }
  17. locals {
  18. # Names use resource_key when it is set and region otherwise.
  19. name_key = var.resource_key != "" ? var.resource_key : var.region
  20. # What the cloud-init gets for the admin key: "<algorithm> <key>" only. A
  21. # trailing comment is accepted by the variable's validation and dropped here,
  22. # so user_data is the same whether or not the key carries one.
  23. admin_key_parts = split(" ", var.admin_ssh_public_key)
  24. admin_key = "${local.admin_key_parts[0]} ${local.admin_key_parts[1]}"
  25. # The weekly connector update runs half an hour after the nightly reboot
  26. # window opens, so each connector updates only while its sibling is idle.
  27. reboot_hour = tonumber(substr(var.reboot_time, 0, 2))
  28. reboot_minute = tonumber(substr(var.reboot_time, 3, 2))
  29. update_minute = (local.reboot_minute + 30) % 60
  30. update_hour = (local.reboot_hour + floor((local.reboot_minute + 30) / 60)) % 24
  31. # Extra lines for /etc/twingate/connector.conf, already indented for the
  32. # cloud-init block scalar and each ending in a newline. With the default
  33. # connector_telemetry every list is empty, so this is "" and user_data is
  34. # byte-identical to before this setting existed (golden hashes in the tests).
  35. telemetry_conf_lines = concat(
  36. var.connector_telemetry.log_level == null ? [] : ["TWINGATE_LOG_LEVEL=${var.connector_telemetry.log_level}"],
  37. var.connector_telemetry.connection_logs ? ["TWINGATE_LOG_ANALYTICS=v3"] : [],
  38. var.connector_telemetry.metrics_port == null ? [] : ["TWINGATE_METRICS_PORT=${var.connector_telemetry.metrics_port}"],
  39. [for k in sort(keys(var.connector_telemetry.labels)) : "TWINGATE_LABEL_${k}=${var.connector_telemetry.labels[k]}"],
  40. )
  41. telemetry_conf = join("", [for l in local.telemetry_conf_lines : " ${l}\n"])
  42. }