Skip to content

Getting Started with Terraform

OPENSTACK CLOUD

In this guide we will see how to use Terraform to manage an OpenStack infrastructure on the Infomaniak Public Cloud.

Prerequisites

To get started you need some prerequisites:

Initialize the project

Create a folder named like you want to contain all data generated by Terraform for your deployment :

> shell
mkdir my-infrastructure

In this folder create a new file named main.tf :

> shell
cd my-infrastructure
touch main.tf

Configure Terraform for the Public Cloud

Traditionally, the OpenStack tools were configured using a set of environment variables (OS_AUTH_URL, OS_USERNAME, etc.), usually delivered in the format of a simple shell script that can be sourced into your current environment. While this works, it becomes hard to manage if you are working with multiple OpenStack environments.

The clouds.yaml configuration file was developed as an alternative mechanism for storing your OpenStack credentials.

Follow the procedure according to your actual setup :

Retrieve the OpenRC configuration from the Manager or Openstack Dashboard.

Edit main.tf and copy the content bellow :

main.tf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Define required providers
terraform {
  # Ensure the use of a compatible Terraform version
  required_version = ">= 0.14.0"
  required_providers {
    # Define OpenStack terraform provider
    openstack = {
      source  = "terraform-provider-openstack/openstack"
      version = "~> 2.0.0"# (1)!
    }
  }
}

# Configure the OpenStack Provider
provider "openstack" {
  auth_url    = "https://api.pub1.infomaniak.cloud/identity"
  region      = "dc3-a"
  user_name   = "PCU-XXXXXXX" # TODO: changeme
  tenant_name = "PCP-XXXXXXX" # TODO: changeme
  password    = "your_password" # TODO: changeme
}
  1. This syntax make sure to only allow the rightmost version component to increment (only minor update).

The first section define all the requirements used by Terraform for this deployment, you may not want to do any change in this section.

The second section is the configuration of the provider. You must perform changes to use your Infomaniak Public Cloud project and user.

You will find the mapping table between the Openstack configuration file and and the Openstack provider fields on the matrix bellow :

Terraform provider field Openstack configuration Field
auth_url OS_AUTH_URL
region OS_REGION_NAME
user_name OS_USERNAME
password OS_PASSWORD
user_domain_name OS_USER_DOMAIN_NAME
project_domain_id OS_PROJECT_DOMAIN_ID
tenant_id OS_PROJECT_ID
tenant_name OS_PROJECT_NAME

Retrieve the cloud.yaml configuration from the Manager or Openstack Dashboard and copy its content to ~/.config/openstack.

Edit main.tf and copy the content bellow :

main.tf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Define required providers
terraform {
  # Ensure the use of a compatible Terraform version
  required_version = ">= 0.14.0"
  required_providers {
    # Define OpenStack terraform provider
    openstack = {
      source  = "terraform-provider-openstack/openstack"
      version = "~> 2.0.0"# (1)!
    }
  }
}

# Configure the OpenStack Provider
provider "openstack" {
  cloud = "PCP-XXXXXXX" # TODO: changeme
}
  1. This syntax make sure to only allow the rightmost version component to increment (only minor update).

Do not forget to update the configuration with your project id.

Success

That's it you can begin to deploy resources with Terraform.