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: Create Terraform Working Directory
We need to create a directory where all our Terraform files will be stored. Here’s how you might want to setup your terraform project folder:
terraform-openstack/
├── main.tf # Provider and resource definitions
├── 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/
└── terraform.tfvars # (Optional) Declare variable values
Create the project directory:
mkdir terraform-openstack && cd terraform-openstack
mkdir modules && cd modules
mkdir glance_image
Step 3: Glance Module Overview
For this task, we opted for a module wince 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.
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 4: 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 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: