The flavors in OpenStack specify the compute, memory, and storage resources available to virtual machines (instances). The configurations that specify such things as RAM, vCPUs, disk space, etc., are the flavors.
Creating flavors by hand with Horizon or CLI is time-consuming and error-prone — Terraform simplifies it by handling the process of creating flavors as code. In this tutorial, we’ll learn about how to automate creating OpenStack flavors with Terraform and our terraform-openstack/flavor module.
Prerequisites
Before you begin, ensure the following:
- Terraform is installed on your machine
- You have access to a working OpenStack cloud (auth URL, username, password, etc.)
- You’ve configured the OpenStack provider in Terraform
- You’re using an SSH key for VM login
Step 1: Install Terraform
If you don’t have terraform installed, run one of the following commands that match your working 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: Configure Terraform Provider
To authenticate Terraform with OpenStack, define the provider as follows in your main.tf
file:
# 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 statefile, you can configure it as follows:
terraform {
backend "local" {
path = "${path.module}/terraform.tfstate"
}
}
Step 3: The Flavor Module Overview
The flavor module has three main configuration files:
main.tf
– Defines the flavor resource.variables.tf
– Defines input variables for the flavor.outputs.tf
– Outputs relevant information about the flavor.
main.tf
resource "openstack_compute_flavor_v2" "flavor" {
for_each = { for f in var.flavors : f.name => f }
name = each.value.name
ram = each.value.ram
vcpus = each.value.vcpus
disk = each.value.disk
swap = each.value.swap
is_public = each.value.is_public
}
variables.tf
variable "flavors" {
description = "List of flavor configurations"
type = list(object({
name = string
ram = number
vcpus = number
disk = number
swap = number
is_public = bool
}))
}
outputs.tf
output "flavor_ids" {
value = { for flavor in openstack_compute_flavor_v2.flavor : flavor.name => flavor.id }
}
Step 4: Using the Flavor Module
Now, in your local main.tf
file, you need to reference the flavor module as follows:
module "flavors" {
source = "git::https://github.com/cloudspinx/terraform-openstack.git//modules/flavor?ref=main"
flavors = [
{
name = "small"
ram = 2048
vcpus = 1
disk = 20
swap = 0
is_public = true
},
{
name = "medium"
ram = 4096
vcpus = 2
disk = 40
swap = 0
is_public = true
},
{
name = "large"
ram = 8192
vcpus = 4
disk = 80
swap = 0
is_public = true
}
]
}
In this case, the module will create three different flavors with different specs, i.e small, medium, and large. You can edit the configuration to create only 1 flavor, with your custom values for the variables.
Apply the Configuration
To run the module and deploy the resources on OpenStack, run the following commands to initialize terraform and apply:
terraform init
terraform apply
Terraform will show the plan and prompt you for approval. Once approved, your flavor will be created in OpenStack.
Confirm that the flavor(s) is created using openstack CLI or web dashboard:
openstack flavor list
Optional: Running the module locally
If you want to customize the module to match your needs, it’s best to clone the repo and run the module locally on your working environment:
git clone https://github.com/cloudspinx/terraform-openstack.git
Create a main.tf
file in the root folder for module usage configs.
terraform-openstack/
├── main.tf # Provider and module usage configs
├── modules # Modules, including kepair
│ ├── flavor/ # The module you're using
│ │ ├── main.tf # Define resources
│ │ ├── variables.tf # Define any variables
│ │ ├── outputs.tf # Optional outputs
│ └── other_modules/
Be sure to reference the module path in your source. See below:
module "flavors" {
source = "./modules/flavor"
flavors = [
{
name = "small"
ram = 2048
vcpus = 1
disk = 20
swap = 0
is_public = true
},
{
name = "medium"
ram = 4096
vcpus = 2
disk = 40
swap = 0
is_public = true
},
{
name = "large"
ram = 8192
vcpus = 4
disk = 80
swap = 0
is_public = true
}
]
}
Conclusion
Terraform OpenStack flavor provisioning ensures consistency, simplifies automation, and replicates infrastructure. The terraform-openstack flavor module offers an elegant, modular way of defining compute templates that scale with your cloud infrastructure.