Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This page describes how to create a new VM by using Terraform.


Table of Contents

Pre-requisites

The following pre-requisites must be satisfied.


There is a set of mandatory inputs required to create a new VM which are:

  • image_name  :  the Virtual Machine image (see EWC - VM images and default users )
  • flavor_name  :  the resources (CPU, RAM, Disk) configuration for the VM (see as reference EWC VM plans )
  • key_pair  :  configured SSH key which is needed to connect to the VM (see EWC - OpenStack Command-Line client for how to import it ) 
  • security_groups : security groups are sets of applied IP filter rules which define networking access to the instance 
  • network  : the network to be attached to the VM : external-internet: access to the Internet with public IP ; private-<tenant>: local private network within the tenant.


Excerpt

The available selectable options could also be checked using the Opnstack CLI commands :

Code Block
$ openstack keypair list
 
$ openstack image list
 
$ openstack flavor list 
 
$ openstack network list


Write configuration files

Create a directory for your configuration and change directory into it:

Code Block
$ mkdir example-create-vm

$ cd example-create-vm


Create the main configuration file to define the infrastructure:

Code Block
$ touch main.tf


Open the main.tf file in a text editor and fill it as needed like in the following example :

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

# Configure the OpenStack Provider
provider "openstack" {
  # ...
}


resource "openstack_compute_instance_v2" "server-name" {
  name            = "server-name"
  image_name      = "image-name"
  flavor_name     = "flavor-name"
  key_pair        = "key-name"
  security_groups = ["secgroup-name-1", "secgroup-name-2", ...]

  network {
    name = "network-name"
  }
}

Replace the following fields as desired:

  • server-name
  • image-name
  • flavor-name
  • key-name
  • secgroup-name-1 , secgroup-name-2
  • network-name


For instance for ECMWF can be :

Code Block
resource "openstack_compute_instance_v2" "test-server" {
  name            = "test-server"
  image_name      = "Rocky-9.4-20240717094419"
  flavor_name     = "2cpu-2gbmem-30gbdisk"
  key_pair        = "mykey"
  security_groups = ["default", "ssh"]

  network {
    name = "private-cci1-ewcloud-ms-nmhs-project"
  }
}


Run Terraform or OpenTofu to create a VM

Initialize the directory :

Section
bordertrue


Column

Terraform

Code Block
$ terraform init


Column

OpenTofu

Code Block
$ tofu init




Review the required changes: 

Code Block
$ terraform plan


Apply the changes to create the VM :

Section
bordertrue


Column

Terraform

Code Block
$ terraform apply


Column

OpenTofu

Code Block
$ tofu apply




Status can be then seen via:

Section
bordertrue


Column

Terraform

Code Block
$ terraform show


Column

OpenTofu

Code Block
$ tofu show





Destroy the VM

The created VM can be then destroyed by simply running:

Section
bordertrue


Column

Terraform

Code Block
$ terraform destroy


Column

OpenTofu

Code Block
$ tofu destroy