|
| 1 | +# Vultr Compute Instances Module |
| 2 | +# Provisions one or more identical Vultr cloud compute instances (count-scaled), |
| 3 | +# with optional SSH keys, startup script, VPC attach, and managed firewall. |
| 4 | + |
| 5 | +module "name" { |
| 6 | + source = "github.com/Facets-cloud/facets-utility-modules//name" |
| 7 | + environment = var.environment |
| 8 | + limit = 32 |
| 9 | + resource_name = var.instance_name |
| 10 | + resource_type = "compute" |
| 11 | +} |
| 12 | + |
| 13 | +locals { |
| 14 | + region = coalesce(try(var.instance.spec.region, null), var.inputs.vultr_cloud_account.attributes.region) |
| 15 | + vpc_ids = (var.instance.spec.networking.attach_vpc && var.inputs.network != null) ? [var.inputs.network.attributes.vpc_id] : null |
| 16 | + has_script = length(trimspace(var.instance.spec.access.startup_script)) > 0 |
| 17 | +} |
| 18 | + |
| 19 | +resource "vultr_ssh_key" "this" { |
| 20 | + for_each = { for idx, k in var.instance.spec.access.ssh_public_keys : tostring(idx) => k } |
| 21 | + name = "${module.name.name}-${each.key}" |
| 22 | + ssh_key = each.value |
| 23 | +} |
| 24 | + |
| 25 | +resource "vultr_startup_script" "this" { |
| 26 | + count = local.has_script ? 1 : 0 |
| 27 | + name = module.name.name |
| 28 | + script = base64encode(var.instance.spec.access.startup_script) |
| 29 | +} |
| 30 | + |
| 31 | +resource "vultr_firewall_group" "this" { |
| 32 | + count = var.instance.spec.firewall.manage ? 1 : 0 |
| 33 | + description = module.name.name |
| 34 | +} |
| 35 | + |
| 36 | +resource "vultr_firewall_rule" "this" { |
| 37 | + for_each = var.instance.spec.firewall.manage ? var.instance.spec.firewall.open_ports : {} |
| 38 | + firewall_group_id = vultr_firewall_group.this[0].id |
| 39 | + protocol = each.value.protocol |
| 40 | + ip_type = "v4" |
| 41 | + subnet = split("/", each.value.source)[0] |
| 42 | + subnet_size = tonumber(split("/", each.value.source)[1]) |
| 43 | + port = each.value.port |
| 44 | +} |
| 45 | + |
| 46 | +resource "vultr_instance" "this" { |
| 47 | + count = var.instance.spec.sizing.count |
| 48 | + region = local.region |
| 49 | + plan = var.instance.spec.sizing.plan |
| 50 | + os_id = var.instance.spec.image.os_id |
| 51 | + label = "${module.name.name}-${count.index}" |
| 52 | + hostname = "${module.name.name}-${count.index}" |
| 53 | + enable_ipv6 = var.instance.spec.networking.enable_ipv6 |
| 54 | + vpc_ids = local.vpc_ids |
| 55 | + ssh_key_ids = [for k in vultr_ssh_key.this : k.id] |
| 56 | + script_id = local.has_script ? vultr_startup_script.this[0].id : null |
| 57 | + firewall_group_id = var.instance.spec.firewall.manage ? vultr_firewall_group.this[0].id : null |
| 58 | + activation_email = false |
| 59 | +} |
0 commit comments