Blog Article:

Enhancing Cloud Automation Efficiencies with OpenNebula and Terraform

Ruben S. Montero

Chief Technology Advisor at OpenNebula

Nov 20, 2023

🔄 For this guide, we’re using Terraform v1.3.1 and OpenNebula 6.8, the latest versions available as of now.

Automation is the cornerstone of efficiency, scalability, and resource management in today’s ever-changing digital landscape. Since 2020, the Terraform provider from OpenNebula, which is actively maintained by Iguane Solutions, has been instrumental for enterprises looking to enhance their automation capabilities. OpenNebula emerges as a powerful tool for enterprises looking to boost their automation efforts. In this blog post, we will delve deep into the product, investigate its integration with the IAC (infrastructure-as-code) software tool Terraform, reveal its numerous benefits, offer troubleshooting tips, walk you through the provider setup process, and provide valuable insights into how to harness automation within this versatile cloud platform fully.

Initiating Your Terraform Journey with OpenNebula

First, it is critical to verify that Terraform is properly placed and ready to roll. The outstanding Infrastructure As Code (IAC) software platform enables you to elegantly and precisely define and provision your infrastructure.  

You must first install the IAC software application before continuing on this adventure. Navigate to HashiCorp’s official software tool documentation, given by the creative brains behind the IAC software tool.  

After successfully installing the software tool, the adventure of integration with OpenNebula can begin.

Setting Up Terraform for OpenNebula: A Step-by-Step Guide

To begin using Terraform with OpenNebula, you must first configure the Terraform provider. Achieving this is very quick and easy, here are the essential steps:   

1. Verify Terraform Installation: Make sure you have Terraform installed on your system as described above, you can find the detailed steps in the official Terraform documentation.

2. Add OpenNebula provider: Create the `provider.tf` file in which to declare the use of the OpenNebula provider. Terraform is able to automatically download any provider from its provider registry, so in order to use the OpenNebula provider it is enough to declare the required provider in Terraform as shown in the following example:

terraform {
  required_providers {
    opennebula = {
      source = "OpenNebula/opennebula"
      version = "~> 1.3"
    }
  }
}

Don’t forget to set the target version to the provider version you want to use. In this example, we will use the latest version of the provider available right now (v1.3.1).

3. Provider Configuration: Instance the OpenNebula provider in Terraform by specifying the required fields to allow Terraform to connect to your instance. This usually entails supplying the OpenNebula XML-RPC API endpoint, authentication credentials, and other environment-specific variables. To do this, we are going to create the `provisions.tf` file where we are going to instantiate the provider, and then, create our provisions:

provider "opennebula" {
  endpoint  = "https://example.com:2633/RPC2"
  username  = oneadmin
  password  = y0uRP@sW0Rd
}

4. Run Terraform: Once the provider is configured and instantiated, we can run the following command in Terraform to initialize the provider:

terraform init

Just like that, we would have our OpenNebula provider configured in Terraform! But we are not automating anything yet, let’s see in the next section how to automatically provision resources in OpenNebula thanks to this integration with Terraform.

Seamlessly deploying VM in OpenNebula using Terraform

In this example, we are going to dynamically add a new Host to OpenNebula using the Terraform provider. To achieve this, we just need to add the following example to our `provisions.tf` file created earlier:

resource "opennebula_host" "example-host" {
  name       = "my-kvm-host"
  type       = "kvm"
  cluster_id = 0

  overcommit {
    cpu = 3200        # 32 cores
    memory = 1048576  # 1 Gb
  }

  tags = {
    environment = "example"
  }
}

Here, we indicate we want to create a resource of type Host (`opennebula_host`) whose identifier is going to be `example-host`. We also add information such as the cluster to which it will be added, and some optional attributes such as overcommitment and tags. This case is just an example but you can find all the available attributes in the official OpenNebula Terraform provider documentation.

Once the Host is added, we are also going to deploy a VM dynamically using the same OpenNebula Terraform provider. Take a look at the following example, which we should also add to the `providers.tf` file:

resource "opennebula_virtual_machine" "example-vm" {

  name        = "my-vm"
  description = "My first VM deployed using OpenNebula Terraform provider"
  cpu         = 1
  vcpu        = 1
  memory      = 1024
  group       = "terraform"
  permissions = "660"

  graphics {
    type   = "VNC"
    listen = "0.0.0.0"
    keymap = "fr"
  }

  os {
    arch = "x86_64"
    boot = "disk0"
  }

  disk {
    image_id = opennebula_image.example.image.id
    size     = 10000
    target   = "vda"
    driver   = "qcow2"
  }

  tags = {
    environment = "example"
  }
}

In the same way as in the previous example we are creating a resource in OpenNebula, although in this case of Virtual Machine type (`opennebula_virtual_machine`). Next, we define the basic attributes such as the name, a description, and the CPU, vCPU, and memory resources to be used by the Virtual Machine. There are many different attributes that can be added, so please take a look at the official documentation of the provider to review them all. As an example, the VNC connection section, OS, disk, and tags have been added in this case.

With these resources added, it’s as simple as executing the following terraform commands to make the magic happen:

# Initialize the terraform execution environment
terraform init

# Allows you to preview the changes Terraform will make before you apply them
terraform plan

# Makes the changes defined by your plan to create, update, or destroy resources
terraform apply

And that’s it! Once the Terraform execution is finished we should be able to find a VM deployed in our OpenNebula in an automatic and easy way.

In Conclusion: Empowering your cloud computerization journey

OpenNebula, in collaboration with Terraform, paves the way for a new age of efficient, scalable, and automated cloud infrastructure management. Its capabilities in managing multi-cloud environments and superior virtual network management position it as the ideal solution for modernizing operations.

By integrating OpenNebula with cutting-edge software tools like Terraform, you’re not just enhancing your automation efforts but also future-proofing your cloud infrastructure against the dynamic needs of the digital era.

2 Comments

  1. Andrea Tran

    Currently, I am using OpenNebula and OpenNebula Terraform Provider to manage infrastructure non-production environments. However, The OpenNebula Terraform Provider is not enough, especially the “service” resource. I don’t understand why anytime update the state, the service will delete and create a new one ??

    Reply
    • Shivang Kapoor

      Hello Andrea,

      Happy to see you’re using the OpenNebula Terraform Provider. To help you better, could you share more details about the problem? Like which version you’re using and any relevant error logs you’ve come across. Also, I suggest trying the newest version, v1.4.0, because we’ve made some updates (improving the idempotency of the service resource) to make it work better.

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *