Uploading VM Images to OpenStack Glance using Terraform

Hi there folks, today, we are going to go through how to upload a VM image to OpenStack Glance using Terraform. Terraform is the go-to, Infrastructure as Code(IaC) tool in the current regime, and is written using HCL. With Terraform, you can specify on-premises and cloud resources in human-readable configuration files that can be shared, reused, and versioned all together. After that, all of your infrastructure can be provisioned and managed using a standardized approach.

Prerequisites

  • Active OpenStack environment with Glance service enabled.
  • Terraform installed on your local machine.
  • OpenStack credentials (username, password, tenant, auth URL).
  • The OS image that you want to upload to Glance.

Step 1: Install Terraform

Install Hashicorp terraform on your local environment:

# Ubuntu/Debian
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform

# CentOS/RHEL
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform

# Fedora
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager addrepo --from-repofile=https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
sudo dnf -y install terraform

# Amazon Linux
sudo yum install -y yum-utils shadow-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform

# macOS Homebrew
brew tap hashicorp/tap
brew install hashicorp/tap/terraform

Step 2: Glance Module Overview

For this task, we opted for a module since we are putting together different modules to create different resources. This module is designed to upload and manage images from a local path to the OpenStack Glance service. This ensures that the images are available for use when provisioning instances as we’ll see in the coming articles.

If you’d prefer to have the files locally, let’s go through how you’d setup:

Module Structure

This how the module is stuctured:

terraform-openstack/                               
├── modules               # Modules, including glance_image
│   ├── glance_image/     # The module you're using
│   │   ├── main.tf       
│   │   ├── variables.tf  # Define any variables
│   │   ├── outputs.tf    # Optional outputs 
│   └── other_modules/

The module has three terraform files, main.tf, variables.tf, and outputs.tf.

Main.tf
# Define the OpenStack image resource
resource "openstack_images_image_v2" "image" {
  name              = var.image_name
  disk_format       = var.disk_format
  container_format  = var.container_format
  visibility        = var.visibility
  local_file_path   = var.local_file_path

  # Optionally, add other properties as needed
  min_disk_gb       = var.min_disk_gb
  min_ram_mb        = var.min_ram_mb
  tags              = var.tags
}
Variables.tf
variable "image_name" {
  description = "The name of the image"
  type        = string
}

variable "disk_format" {
  description = "The disk format of the image (e.g., qcow2, raw)"
  type        = string
}

variable "container_format" {
  description = "The container format of the image (e.g., bare, ovf)"
  type        = string
}

variable "visibility" {
  description = "The visibility of the image (e.g., public, private)"
  type        = string
}

variable "image_source_url" {
  description = "The URL to download the image from (optional, overrides image_path)"
  type        = string
  default     = ""
}

variable "local_file_path" {
  description = "The local file path of the image to upload (optional)"
  type        = string
  default     = ""
}

variable "min_disk_gb" {
  description = "The minimum disk size required to boot the image"
  type        = number
  default     = 0
}

variable "min_ram_mb" {
  description = "The minimum RAM size required to boot the image"
  type        = number
  default     = 0
}

variable "tags" {
  description = "Tags for the image"
  type        = list(string)
  default     = []
}
Outputs.tf
output "image_id" {
  description = "The ID of the created OpenStack image"
  value       = openstack_images_image_v2.image.id
}

output "image_name" {
  description = "The name of the created OpenStack image"
  value       = openstack_images_image_v2.image.name
}
Step 3: Defining OpenStack Provider

In your main.tf or providers.tf file, define and then customize the following configurations to configure OpenStack provider:

# Define required providers
terraform {
  required_version = ">= 0.14.0"
  required_providers {
    openstack = {
      source = "terraform-provider-openstack/openstack"
      version = "~> 2.1.0"
    }
  }
}

# Configure the OpenStack Provider
provider "openstack" {
  user_name   = "admin"
  tenant_name = "admin"
  password    = "pwd"
  auth_url    = "http://myauthurl:5000/v3"
  region      = "RegionOne"
}

For a local state fil, use the following configuration:

#Local state file storage
terraform {
  backend "local" {
    path = "${path.module}/terraform.tfstate"
  }
}

Step 5: Using the Glance Module

With all the module configurations done, here’s how you can populate your main.tf file to use the glance module to upload your VM image to OpenStack Glance. The main.tf file should be in the root directory:

module "glance_image" {
  source = "git::https://github.com/cloudspinx/terraform-openstack.git//modules/glance_image?ref=main"
  # To use your own module, use; source = "./modules/glance_image"

  image_name        = "my-image"
  disk_format       = "qcow2"
  container_format  = "bare"
  visibility        = "public"
  local_file_path   = "path/to/local/image.qcow2"
}

For the source, reference the module accordingly to use your local glance_image module. Once you’ve written the configs, run the init, plan, and apply commands:

terraform init
terraform plan
terraform apply

Terraform will ask for your approval to provision the OpensStack resources. If you’d like terraform to proceed without asking for your approval, you can use the –auto-approve flag.

terraform apply --auto-approve

Conclusion

Using Terraform to upload images to OpenStack Glance simplifies and automates the image management process. By using the glance_image module, you ensure that your image upload process is repeatable, consistent, and easy to manage.

More articles on the same:

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Unlock the Right Solutions with Confidence

At CloudSpinx, we don’t just offer services - we deliver clarity, direction, and results. Whether you're navigating cloud adoption, scaling infrastructure, or solving DevOps challenges, our seasoned experts help you make smart, strategic decisions with total confidence. Let us turn complexity into opportunity and bring your vision to life.

Leave a Comment

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

Related Post

Autofs also referred to as automount is a feature in Linux like systems that automatically mount filesystems on demand. In […]

Every Linux user must be familiar with the command ‘man’. The command is used to display instructions on the usage […]

Engineering is a science-math problem solving field in our society. Engineers make use of engineering skills to convert ideas in […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.