Versions Compared

Key

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


Excerpt

Terraform is an infrastructure as code tool that lets you build, change, and version infrastructure. This includes low-level components like compute instances, storage, and networking; and high-level components.

OpenTofu is a fork of Terraform that is open-source, community-driven, and managed by the Linux Foundation.


Table of Contents

Installation of the CLI

In order to start using Terraform or OpenTofu  it is necessary to install its client. There are several ways to install it also depending on the local platform where it needs to be installed, these can be consulted in the Terraform documentation and OpenTofu documentation. 

One convenient way to install the client can be also via conda :

Section
bordertrue
Column

Terraform

Code Block
$ conda create -n terraform-env -c conda-forge terraform
Column

OpenTofu

Code Block
$ conda create -n opentofu-env -c conda-forge opentofu


Get Started

Pre-requisites

  • The Terraform or OpenTofu CLI installed. 
  • Application credentials (or username/password for internal users) to access the Openstack cloud project


Show If
groupecmwf
Info

Internal ECMWF staffs with direct access to the Openstack CCI can also use normal username/password instead of the applications credentials. 



Write configuration files

The set of files used to describe infrastructure in Terraform/OpenTofu is known as a Terraform/OpenTofu configuration.

Each Terraform/OpenTofu configuration must be in its own working directory.


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

Code Block
$ mkdir my-working-conf

$ cd my-working-conf


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" {
  # ...
}


where the explanation of the blocks is: 

Terraform Block

The terraform {}  block contains Terraform/OpenTofu settings, including the required providers Terraform/OpenTofu will use to provision your infrastructure. 

Providers

Theprovider block configures the specified provider, in this case openstack . A provider is a plugin that Terraform/OpenTofu uses to create and manage your resources.

For more information about the Terraform Openstack provider it is possible to consult the terraform-provider-openstack documentation . 

Resources

The resource blocks are used to define components of your infrastructure. A resource might be a physical or virtual component such as an instance, or it can be a logical resource such as an application.


More information can be found in the Terraform documentation or in the OpenTofu documentation.


Run Terraform or OpenTofu

Cloud Authentication

In order to run Terraform is required to have the Openstack application credentials . 

  • openrc file  :  that can be sourced for exporting the necessary environment variables
  • clouds.yaml file : that can be installed under : ~/.config/openstack/clouds.yaml 

Each file can be used in alternative to the other.

Section
bordertrue
Column

openrc file

If using the openrc file it is enough to source it, e.g.  :

Code Block
$ . ms-nmhs-project-openrc.sh

and this will export the required environment variables:

Code Block
 OS_AUTH_TYPE
 OS_AUTH_URL
 OS_IDENTITY_API_VERSION
 OS_INTERFACE
 OS_REGION_NAME
 OS_APPLICATION_CREDENTIAL_ID
 OS_APPLICATION_CREDENTIAL_SECRET

in this case the provider block in the terraform can be kept as:

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

since the required information are taken from the environment variables.


Column

clouds.yaml

If using the clouds.yaml file, this need to be be installed under : ~/.config/openstack/clouds.yaml 

In this case the provider block in the terraform need to be modified in this way to specify the cloud :

Code Block
 # Configure the OpenStack Provider
provider "openstack" {
  cloud   = "openstack"
}




Run the CLI

Initialize and apply the configuration

The first step once created the terraform configuration files is to initialize the directory :

Section
bordertrue
Column

Terraform

Code Block
$ terraform init



Column

OpenTofu

Code Block
$ tofu init



Once initialized it is possible to see any changes that are required for your infrastructure by running :

Section
bordertrue
Column

Terraform

Code Block
$ terraform plan


Column

OpenTofu

Code Block
$ tofu plan



Finally the actual infrastructure can be created by running:

Section
bordertrue
Column

Terraform

Code Block
$ terraform apply


Column

OpenTofu

Code Block
$ tofu apply



The status can be then seen via:

Section
bordertrue
Column

Terraform

Code Block
$ terraform show


Column

OpenTofu

Code Block
$ tofu show



Destroy the infrastructure

The created infrastructure can be easily destroyed by running:

Section
bordertrue
Column

Terraform

Code Block
$ terraform destroy


Column

OpenTofu

Code Block
$ tofu destroy




References