From 21bcd4ab410fea1200cf4a1a64e269f2866ac632 Mon Sep 17 00:00:00 2001 From: alessandrovitali Date: Fri, 14 Aug 2026 14:14:34 +0200 Subject: [PATCH] refactor(terraform): move from legacy OVH to v2 API --- terraform/main.tf | 66 ++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/terraform/main.tf b/terraform/main.tf index 3d3f58f..46ca5d2 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -1,69 +1,53 @@ ## DATA SOURCES -# Fetch available images filtered by Linux -data "ovh_cloud_project_images" "images" { +data "ovh_cloud_instance_flavors" "catalog" { service_name = var.ovh_service_name region = var.ovh_region - os_type = "linux" } -# Fetch the ID for the d2-2 flavor -data "ovh_cloud_project_flavors" "flavors" { +data "ovh_cloud_instance_images" "catalog" { service_name = var.ovh_service_name region = var.ovh_region - name_filter = var.ovh_flavor_name -} - -locals { - # Find the exact image matching the image_name variable - image = one([ - for img in data.ovh_cloud_project_images.images.images : img if img.name == var.image_name - ]) - - # Retrieve the exact d2-2 flavor ID - flavor = one(data.ovh_cloud_project_flavors.flavors.flavors) } ## RESOURCES -# Create/Register SSH public key in the project -resource "ovh_cloud_project_ssh_key" "vm_ssh_key" { +resource "ovh_cloud_ssh_key" "deploy" { service_name = var.ovh_service_name - name = "ansible-ssh-key" + name = "ansible-key" public_key = var.ssh_public_key } # Provision the d2-2 Public Cloud Instance -resource "ovh_cloud_project_instance" "d2-2-vm" { +resource "ovh_cloud_instance" "from_catalog" { service_name = var.ovh_service_name region = var.ovh_region name = var.project_name - billing_period = "hourly" # Alternatives: "hourly" or "monthly" + ssh_key_name = ovh_cloud_ssh_key.deploy.name - boot_from { - image_id = local.image.id - } + flavor_id = one([ + for flavor in data.ovh_cloud_instance_flavors.catalog.flavors : + flavor.id if flavor.name == "d2-2" + ]) - flavor { - flavor_id = local.flavor.id - } + image_id = one([ + for image in data.ovh_cloud_instance_images.catalog.images : + image.id if image.name == "Debian 13" + ]) - ssh_key { - name = ovh_cloud_project_ssh_key.vm_ssh_key.name - } - - # Attaches a public IPv4 interface to the VM - network { - public = true - } + networks = [ + { auto_assign_public_ip = true }, + ] } ## OUTPUTS -# Outputs the public IPv4 address assigned to the VM once created -output "instance_public_ip" { - value = [ - for addr in ovh_cloud_project_instance.d2-2-vm.addresses : addr.ip if addr.version == 4 - ] - description = "The public IPv4 address of your Frankfurt VM" +output "public_ip" { + description = "The public IPv4 address of the provisioned OVHcloud VM" + value = one(flatten([ + for net in ovh_cloud_instance.from_catalog.current_state.networks : [ + for addr in net.addresses : addr.ip + if addr.type == "FIXED" && addr.version == 4 + ] + ])) }