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.


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 :

Terraform

$ conda create -n terraform-env -c conda-forge terraform

OpenTofu

$ 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


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:

$ mkdir my-working-conf

$ cd my-working-conf


Create the main configuration file to define the infrastructure:

$ touch main.tf


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

# 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

The provider 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.

openrc file

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

$ . ms-nmhs-project-openrc.sh

and this will export the required environment variables:

 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:

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

since the required information are taken from the environment variables.


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 :

 # 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 :

Terraform

$ terraform init



OpenTofu

$ tofu init



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

Terraform

$ terraform plan


OpenTofu

$ tofu plan



Finally the actual infrastructure can be created by running:

Terraform

$ terraform apply


OpenTofu

$ tofu apply



The status can be then seen via:

Terraform

$ terraform show


OpenTofu

$ tofu show



Destroy the infrastructure

The created infrastructure can be easily destroyed by running:

Terraform

$ terraform destroy


OpenTofu

$ tofu destroy




References




  • No labels