How to configure Terraform for managing a cloud over OpenStack Print

  • 2

Step 1: Prepare Terraform Directory


Terraform should be installed to a machine that has Internet access to your Private Cloud. This could be your own machine or one of your cloud's hardware nodes, we suggest creating a folder to manage your Terraform plans and execution files. For example:

mkdir ~/terraform

 

Step 2: Specify Terraform Provider

When working with Terraform, you must specify a provider. There are a number of providers to choose from located in Terraform's Providers website. For our case, we need to use Terraform's OpenStack Provider because our clouds are powered by Virtuozzo/OpenStack.

To specify the OpenStack provider, create a file called providers.tf in your Terraform directory containing:

terraform {
required_version = ">= 0.14.0"
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
version = "~> 1.53.0"
}
}
}

Step 3: Initialize Terraform


With a provider defined, Terraform must be initialized. To initialize Terraform, execute:

terraform init

When Terraform has been successfully initialized, the following message is returned:

Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

 

Step 4: Get Virtuozzo/OpenStack Application Credentials

To point Terraform to the appropriate cloud and authenticate, you can generate a set of OpenStack Application Credentials. This data should be taken from the Virtuozzo/Openstack admin pannel.

The contents sholuld look similar to:

user_name = "mxdAdm"
tenant_id = "6bsdeld46a804998a4135ed011538cc5"
password = "hMCYGMBbddp"
auth_url = "https://panel.servercloud.com:5000/v3"
region = "RegionOne"
user_domain_id = "b5ddscbc5d4404b9b35d733625dcad8"   ---*
project_domain_id = "b5ddscbc5d4404b9b35d733625dcad8" ---*

---* these ID are the same for both parameters.

 

Step 5: Create Main Terraform File

To do so, we must create a template, define the openstack provider, and define the compute resources. 

Within your Terraform directory, create a file called main.tf. Then, for the OpenStack provider, configure the cloud to point to, like so:

provider "openstack" {
user_name = "mxdAdm"
tenant_id = "6bsdeld46a804998a4135ed011538cc5"
password = "hMCYGMBbddp"
auth_url = "https://panel.servercloud.com:5000/v3"
region = "RegionOne"
user_domain_id = "b5ddscbc5d4404b9b35d733625dcad8"   ---*
project_domain_id = "b5ddscbc5d4404b9b35d733625dcad8" ---*
}

---* these ID are the same for both parameters.

Configure Compute Resource​

With the provider section configured, you can now define a compute resource. This compute resource is defined in the same main.tf file. You will need to collect a number of details from your cloud, such as an image UUID, a list of flavors, a key pair, and any other items needed when spawning an instance.

To define a compute resource, we use the following template:

resource "openstack_compute_instance_v2" "<resource-name>" {
name = ""
image_id = ""
flavor_id = ""
key_pair = ""
security_groups = []

network {
name = ""
}
}

With the OpenStack Terraform provider, openstack_compute_instance_v2 is a resource we can use to create an instance. For more details about this resource and its configuration options, see Terraform's Registry and search for openstack_compute_instance_v2.

A description of variables used for the resource block:

name: Defines the instance's name
image_id: UUID for an operating system image hosted in your cloud
flavor_id: The value for an instance flavor
key_pair: The value for a key pair hosted in your cloud
security_groups: The value in list format for security groups to set

Then within the resource block, we also configure a network with which the instance will be associated by creating a network block.

A description of the variable used for the network block:

name: This is the name of a network available to your project

 

Configure Block storage for the compute resource

In case the VM requires a volume to be assigned for storage, its creation and subsequent assignment will proceed as an separate resource block:

resource "openstack_blockstorage_volume_v3" "my_tf_vol_worker" {
count = #
name = "my_tf_vol_worker${count.index+1}"
size = 100
description = "Bootable volume for my VM"
image_id = var.K8S_IMAGE_ID
}

 

Step 6: Create Terraform Plan

Terraform needs to create a plan based on the current configuration. This plan provides the changes Terraform will make to your cloud prior to making them, giving an operator a chance for review. The command to create a Terraform plan is:

terraform plan

 

By default terraform plan does not save the plan to disk. To have the plan written to disk, use:

terraform plan -out <path>

 

Replacing <path> with where you want to store the Terraform plan.

 

Step 7: Deploy Terraform Plan

After reviewing the plan and ensuring the changes to be made meet your expectations, use Terraform to deploy the plan. The command to deploy a Terraform plan is:

terraform apply [PLAN]

Where [PLAN] is an optional variable. For this example, since we saved the plan to disk, we will use that plan when applying Terraform.


Was this answer helpful?

« Back