Provision an OpenStack instance with Terraform


A quick way to create an instance on OpenStack is to use Terraform, an open-source Infrastructure-as-Code (IaC) tool developed by HashiCorp.
Here is my environment, quickly drawn 😉 :

This instance will be provisioned with Linux CirrOS.
The Terraform plan that i used :

# Creating an SSH key pair resource
resource "openstack_compute_keypair_v2" "MaCle2" {
  provider   = openstack.hello # Provider name declared in provider.tf
  name       = "MaCle2" # Name of the SSH key to use for creation
  public_key = file("~/.ssh/id_rsa.pub") # Path to your previously generated SSH key
}

# Creating the instance
resource "openstack_compute_instance_v2" "create_an_instance" {
  name        = "MonInstance2" #Instance name
  provider    = openstack.hello  # Provider name
  image_name  = "cirros-0.5.2-x86_64-disk" # Image name
  flavor_name = "m1.nano" # Instance type name
  key_pair    = openstack_compute_keypair_v2.MaCle2.name
  security_groups = ["default"]
  network {
    name      = "Shared" # Adds the network component to reach your instance
  }
}

The provider to use to interact with OpenStack is terraform-provider-openstack :

# Define providers and set versions
terraform {
required_version    = ">= 0.14.0" 
  required_providers {
    openstack = {
      source  = "terraform-provider-openstack/openstack"
      version = "~> 1.42.0"
    }
  }
}

# Configure the OpenStack provider 
provider "openstack" {
  auth_url    = "http://10.0.2.15/identity" # Authentication URL
  domain_name = "default" 
  alias       = "hello" 
}

The documentation for terraform-provider-openstack can be found here :
https://registry.terraform.io/providers/terraform-provider-openstack/openstack/latest/docs

Commands to run :
1) terraform validate : to validate/verify the configuration

2) terraform plan : to preview the changes

3) terraform apply : to actually apply/run the actions

4) terraform destroy : to destroy everything and return to the initial state. In this case, that means both the instance and the key will be deleted.

This is part of the output after running the “terraform apply” instruction :

Of course, the OpenStack environment variables need to be set first :

source admin-openrc.sh

OpenStack compute service : error when trying to access the instances section

In the Horizon dashboard, I tried to access the instances section, which is part of the compute service. I got this error :

JSONDecodeError at /project/instances/
Expecting value: line 1 column 1 (char 0)
Request Method: 	GET
Request URL: 	http://localhost/dashboard/project/instances/
Django Version: 	3.2.10
Exception Type: 	JSONDecodeError
Exception Value: 	
Expecting value: line 1 column 1 (char 0)
Exception Location: 	/usr/lib/python3.8/json/decoder.py, line 355, in raw_decode

I checked all the running devstack services, they were all running but one :

I tried to start this service only but it was not enough. I had to restart all services :

sudo systemctl restart devstack@*

Installing OpenStack on Ubuntu


Here are the steps to install OpenStack with DevStack on Ubuntu 20.04.3 LTS.
DevStack really simplifies the installation of OpenStack.
I followed the steps described here : https://docs.openstack.org/devstack/latest/
1) First you need to create the user “stack” :
sudo useradd -s /bin/bash -d /opt/stack -m stack
2) That user should have no password and have sudo privileges :
echo “stack ALL=(ALL) NOPASSWD: ALL” | sudo tee /etc/sudoers.d/stack
3) Login as the user stack :
sudo -u stack -i
4) Then clone the DevStack files :
git clone https://opendev.org/openstack/devstack
5) Go to the devstack folder and create a file called local.conf :
cd devstack
/opt/stack/devstack/touch local.conf
Add the following content :
[[local|localrc]]
ADMIN_PASSWORD=secret
DATABASE_PASSWORD=$ADMIN_PASSWORD
RABBIT_PASSWORD=$ADMIN_PASSWORD
SERVICE_PASSWORD=$ADMIN_PASSWORD

And finally launch the script that will install OpenStack :
./stack.sh

2021-12-15 20:03:55.748 | stack.sh completed in 1444 seconds. (it took a bit more than 20mn)

You can then login to the Horizon dashboard. Just go to the URL http://localhost. User/password : admin/secret

You can check the version installed :

And also launch OpenStack CLI :