refactor(terraform): move from legacy OVH to v2 API

This commit is contained in:
2026-08-14 14:14:34 +02:00
parent 614a529af5
commit 21bcd4ab41
+24 -40
View File
@@ -1,69 +1,53 @@
## DATA SOURCES ## DATA SOURCES
# Fetch available images filtered by Linux data "ovh_cloud_instance_flavors" "catalog" {
data "ovh_cloud_project_images" "images" {
service_name = var.ovh_service_name service_name = var.ovh_service_name
region = var.ovh_region region = var.ovh_region
os_type = "linux"
} }
# Fetch the ID for the d2-2 flavor data "ovh_cloud_instance_images" "catalog" {
data "ovh_cloud_project_flavors" "flavors" {
service_name = var.ovh_service_name service_name = var.ovh_service_name
region = var.ovh_region 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 ## RESOURCES
# Create/Register SSH public key in the project resource "ovh_cloud_ssh_key" "deploy" {
resource "ovh_cloud_project_ssh_key" "vm_ssh_key" {
service_name = var.ovh_service_name service_name = var.ovh_service_name
name = "ansible-ssh-key" name = "ansible-key"
public_key = var.ssh_public_key public_key = var.ssh_public_key
} }
# Provision the d2-2 Public Cloud Instance # 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 service_name = var.ovh_service_name
region = var.ovh_region region = var.ovh_region
name = var.project_name name = var.project_name
billing_period = "hourly" # Alternatives: "hourly" or "monthly" ssh_key_name = ovh_cloud_ssh_key.deploy.name
boot_from { flavor_id = one([
image_id = local.image.id for flavor in data.ovh_cloud_instance_flavors.catalog.flavors :
} flavor.id if flavor.name == "d2-2"
])
flavor { image_id = one([
flavor_id = local.flavor.id for image in data.ovh_cloud_instance_images.catalog.images :
} image.id if image.name == "Debian 13"
])
ssh_key { networks = [
name = ovh_cloud_project_ssh_key.vm_ssh_key.name { auto_assign_public_ip = true },
} ]
# Attaches a public IPv4 interface to the VM
network {
public = true
}
} }
## OUTPUTS ## OUTPUTS
# Outputs the public IPv4 address assigned to the VM once created output "public_ip" {
output "instance_public_ip" { description = "The public IPv4 address of the provisioned OVHcloud VM"
value = [ value = one(flatten([
for addr in ovh_cloud_project_instance.d2-2-vm.addresses : addr.ip if addr.version == 4 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
] ]
description = "The public IPv4 address of your Frankfurt VM" ]))
} }