You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »


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

Terraform

In order to start using Terraform 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. 

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

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



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

OpenTofu

In order to start using 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 OpenTofu documentation. 

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

$ 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 is known as a Terraform configuration.

Each Terraform 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

Terraform

Initialize and apply terraform configuration

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

$ terraform init


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

$ terraform plan


Finally the actual infrastructure can be created by running:

$ terraform apply


The status can be then seen via:

$ terraform show


Destroy the infrastructure

The created infrastructure can be easily destroyed by running:

$ terraform destroy



OpenTofu

Initialize and apply OpenTofu configuration

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

$ tofu init


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

$ tofu plan


Finally the actual infrastructure can be created by running:

$ tofu apply


The status can be then seen via:

$ tofu show


Destroy the infrastructure

The created infrastructure can be easily destroyed by running:

$ tofu destroy





References




  • No labels