How to Create a GandiCloud VPS with Terraform

This page will show you how to quickly create a GandiCloud VPS with a Terraform manifest. You can visit Terraform solution page if you want to learn more about what can be done with GandiCloud and Terraform.

Prerequisites

  • Set the environment variables of your OpenStack project/Gandi Organization for OpenStack CLI

  • Have a valid SSH Keypair in your GandiCloud Keyring

  • Install Terraform locally on your PC

Create The Terraform manifest

First, create an empty directory, such as “Terraform_gandi/”.

In this directoy create a new file named “main.tf”, then copy and paste the lines below into the file.

This Terraform manifest will create an Ubuntu test server that you can access with your SSH key.

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

provider "openstack" {
  auth_url = "https://keystone.sd6.api.gandi.net:5000/v3"
  region = "FR-SD6"
}

variable "SSH_PUBKEY" {
  type = string
}

resource "openstack_compute_instance_v2" "tf_server" {
  name = "tf-test-server"
  key_pair = "${var.SSH_PUBKEY}"
  flavor_name = "V-R1"
  security_groups = ["default"]
  power_state = "active"
  network {
    name = "public"
}

  block_device {
    uuid = "47edd0a0-23ce-4ce5-9168-36de68990d1b"
    source_type           = "image"
    volume_size           = 25
    boot_index            = 0
    destination_type      = "volume"
    delete_on_termination = true
  }
}

output "IP_v4" {
  value = openstack_compute_instance_v2.tf_server.access_ip_v4
}

This Terraform manifest will create:

  • A GandiCloud V-R1 Server

  • Using Ubuntu 20.04

  • Accessible through SSH (with a key-pair already defined)

Note

For this Terraform manifest to work properly the following environment variables must be exported: TF_VAR_SSH_PUBKEY : the name of your OpenStack SSH KEYPAIR that will be associated to the server

If you did not export the variable it will ask you a value before the execution.

Run the Terraform manifest

The first time you run a Terraform manifest, first run the following command to initialize the Terraform:

terraform init

Then:

terraform plan

Check there is no error then launch the Terraform plan with:

terraform apply

Finally the output will return you the public v4 IP of the created server. You can then connect use SSH to connect to this server using the standard ‘ubuntu’ user.

ssh ubuntu@<Your-server-IPv4>

Go further with Terraform and GandiCloud.