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 CLIIn 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 StartedPre-requisites- The Terraform or OpenTofu CLI installed.
- Application credentials (or username/password for internal users) to access the Openstack cloud project
Internal ECMWF staffs with direct access to the Openstack CCI can also use normal username/password instead of the applications credentials. |
|
Write configuration filesThe 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:
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 BlockThe terraform {} block contains Terraform/OpenTofu settings, including the required providers Terraform/OpenTofu will use to provision your infrastructure. ProvidersThe 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 . ResourcesThe 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 OpenTofuCloud AuthenticationIn 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 fileIf 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.yamlIf 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 CLIInitialize and apply the configurationThe first step once created the terraform configuration files is to initialize the directory :
Once initialized it is possible to see any changes that are required for your infrastructure by running :
Finally the actual infrastructure can be created by running:
The status can be then seen via:
Destroy the infrastructureThe created infrastructure can be easily destroyed by running:
References
|