Getting Started with Terraform
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:
- Install HashiCorp Terraform or OpenTofu
- Download your Openstack configuration from the Infomaniak Manager or Openstack Dashboard
- An existing SSH keypair (Preferred)
Initialize the project
Create a folder named like you want to contain all data generated by Terraform for your deployment :
mkdir my-infrastructure
In this folder create a new file named main.tf
:
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 |
|
- 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 |
|
- 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.