OpenTofu is a fork of Terraform that is open-source, community-driven, and managed by the Linux Foundation. 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 OpenTofu 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: Create the main configuration file to define the infrastructure: Open the where the explanation of the blocks is: The The For more information about the Terraform Openstack provider it is possible to consult the terraform-provider-openstack documentation . The More information can be found in the Terraform documentation or in the OpenTofu documentation. In order to run Terraform is required to have the Openstack application credentials . Each file can be used in alternative to the other. If using the openrc file it is enough to source it, e.g. : and this will export the required environment variables: in this case the provider block in the terraform can be kept as: since the required information are taken from the environment variables. If using the clouds.yaml file, this need to be be installed under : In this case the provider block in the terraform need to be modified in this way to specify the cloud : The first step once created the terraform configuration files is to initialize the directory : Terraform OpenTofu Once initialized it is possible to see any changes that are required for your infrastructure by running : Terraform OpenTofu Finally the actual infrastructure can be created by running: Terraform OpenTofu The status can be then seen via: Terraform OpenTofu The created infrastructure can be easily destroyed by running: Terraform OpenTofu
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.Installation of the CLI
$ conda create -n terraform-env -c conda-forge terraform
$ conda create -n opentofu-env -c conda-forge opentofu
Get Started
Pre-requisites
Write configuration files
$ mkdir my-working-conf
$ cd my-working-conf
$ touch main.tf
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" {
# ...
}
Terraform Block
terraform {}
block contains Terraform/OpenTofu settings, including the required providers Terraform/OpenTofu will use to provision your infrastructure. Providers
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.Resources
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.Run Terraform or OpenTofu
Cloud Authentication
~/.config/openstack/clouds.yaml
openrc file
$ . ms-nmhs-project-openrc.sh
OS_AUTH_TYPE
OS_AUTH_URL
OS_IDENTITY_API_VERSION
OS_INTERFACE
OS_REGION_NAME
OS_APPLICATION_CREDENTIAL_ID
OS_APPLICATION_CREDENTIAL_SECRET
# Configure the OpenStack Provider
provider "openstack" {
# ...
}
clouds.yaml
~/.config/openstack/clouds.yaml
# Configure the OpenStack Provider
provider "openstack" {
cloud = "openstack"
}
Run the CLI
Initialize and apply the configuration
$ terraform init
$ tofu init
$ terraform plan
$ tofu plan
$ terraform apply
$ tofu apply
$ terraform show
$ tofu show
Destroy the infrastructure
$ terraform destroy
$ tofu destroy
References