diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 0000000..3d3f58f --- /dev/null +++ b/terraform/main.tf @@ -0,0 +1,69 @@ +## DATA SOURCES + +# Fetch available images filtered by Linux +data "ovh_cloud_project_images" "images" { + 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" { + 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" { + service_name = var.ovh_service_name + name = "ansible-ssh-key" + public_key = var.ssh_public_key +} + +# Provision the d2-2 Public Cloud Instance +resource "ovh_cloud_project_instance" "d2-2-vm" { + service_name = var.ovh_service_name + region = var.ovh_region + name = var.project_name + billing_period = "hourly" # Alternatives: "hourly" or "monthly" + + boot_from { + image_id = local.image.id + } + + flavor { + flavor_id = local.flavor.id + } + + ssh_key { + name = ovh_cloud_project_ssh_key.vm_ssh_key.name + } + + # Attaches a public IPv4 interface to the VM + network { + public = 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" +} diff --git a/terraform/providers.tf b/terraform/providers.tf new file mode 100644 index 0000000..89daf4f --- /dev/null +++ b/terraform/providers.tf @@ -0,0 +1,16 @@ +terraform { + required_version = ">= 1.0.0" + required_providers { + ovh = { + source = "ovh/ovh" + version = "~> 2.19.0" + } + } +} + +provider "ovh" { + endpoint = var.ovh_endpoint + application_key = var.ovh_application_key + application_secret = var.ovh_application_secret + consumer_key = var.ovh_consumer_key +} diff --git a/terraform/variables.tf b/terraform/variables.tf new file mode 100644 index 0000000..4103785 --- /dev/null +++ b/terraform/variables.tf @@ -0,0 +1,53 @@ +variable "project_name" { + type = string + description = "VM name" +} + +variable "ovh_endpoint" { + type = string + description = "OVH API endpoint" + default = "ovh-eu" +} + +variable "ovh_application_key" { + type = string + sensitive = true +} + +variable "ovh_application_secret" { + type = string + sensitive = true +} + +variable "ovh_consumer_key" { + type = string + sensitive = true +} + +variable "ovh_service_name" { + type = string + description = "OVHcloud Public Cloud Project ID" +} + +variable "ovh_region" { + type = string + description = "OVHcloud region code" + default = "DE1" # Frankfurt, Germany +} + +variable "ovh_flavor_name" { + type = string + description = "VM flavor name" + default = "d2-2" +} + +variable "image_name" { + type = string + description = "OS distribution image name to boot the VM from" + default = "Debian 13" +} + +variable "ssh_public_key" { + type = string + description = "Public SSH key to install on the VM" +}