# Getting started with Terraform in Glesys Cloud

***

The Glesys Provider for Terraform lets you manage resources in Glesys Cloud. Currently, it supports, among other things, **virtual machines** (KVM and VMware), **networking**, **load balancers**, and **object storage**.

On [GitHub](https://github.com/glesys), you can find our repository [`terraform-provider-glesys`](https://github.com/glesys/terraform-provider-glesys) along with the accompanying documentation.

## Prerequisites

You need permission to a Glesys Cloud account:

* Open a free account [here](https://cloud.glesys.com/#/signup)&#x20;
* A project in the control panel (this is where your services reside)&#x20;
* An API key. For this example, we use the following permissions for the **Server** module in the Glesys API:    
  * `create`
  * `destroy`
  * `details`
  * `edit`
  * `list`

## Example

Here is an example where we create a virtual server and then increase its disk size.

You can use this configuration by creating a file named `example.tf` in a directory and then running the following commands from that directory:

* Initialize a working directory for Terraform: `terraform init`
* Generate and display an execution plan: `terraform plan`
* Build your infrastructure: `terraform apply`&#x20;

### Step 1: Install and configure Terraform

For Terraform to communicate with the Glesys API, you need to provide a project ID (clXXXXX) from Glesys Cloud as the username and an API key that can authenticate against that project.

In this example, we’ll export environment variables in a shell so you don’t have to specify them each time a command interacts with the Glesys API.

{% code title="Multiple commands" %}

```
export GLESYS_USERID=CL12345
export GLESYS_TOKEN=abc12345XYZ
```

{% endcode %}

Let's start by creating a directory for this example:

{% code title="Multiple commands" %}

```
mkdir glesys-terraform
cd glesys-terraform
```

{% endcode %}

In the directory, create a file called `example.tf`, which is used when you download the GleSYS Provider. Give the file the following content.

{% code title="example.tf" %}

```
terraform {
  required_providers {
    glesys = {
      source = "glesys/glesys"
      version = "~> 0.4.6" # Här kan du ändra om du vill köra en särskild version av providern.
    }
  }
}
```

{% endcode %}

By running the `terraform init` command, you initiate the download of the GleSYS Provider:

{% code title="Command" %}

```
terraform init
```

{% endcode %}

The output from the command will look similar to this:

{% code title="Output" %}

```
Initializing the backend...

Initializing provider plugins...
- Finding latest version of glesys/glesys...
- Installing glesys/glesys v0.4.6...
- Installed glesys/glesys v0.4.6 (self-signed, key ID 4B5E1D585D113D4D)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html

Terraform has made some changes to the provider dependency selections recorded
in the .terraform.lock.hcl file. Review those changes and commit them to your
version control system if they represent changes you intended to make.

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.
```

{% endcode %}

You are now ready to create resources in Glesys Cloud!

### Step 2: Create a virtual server

To create a virtual server, you need to add several parameters to the `example.tf` file. We recommend reading the documentation for [`glesys_server`](https://registry.terraform.io/providers/glesys/glesys/latest/docs/resources/server), where you can also see which parameters are required and which are optional.

If you want to spin up a virtual machine (KVM) running Debian 12 in the Stockholm data center and also create two users (*alice* and *bob*), the parameters would look like the example below. You can also clearly see how much resources have been allocated to the server. Add the following content to the previously created `example.tf` file:

{% code title="Additions to example.tf" %}

```
resource "glesys_server" "kvm" {
  count = 1
  datacenter = "Stockholm"
  memory = 1024
  storage = 20
  cpu = 1
  bandwidth = 100

  hostname = "www1"

  platform = "KVM"
  template = "debian-12"

  user {
        username = "alice"
        publickeys = [
          "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINOCh8br7CwZDMGmINyJgBip943QXgkf7XdXrDMJf5Dl alice@example.com",
          "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOfN4dBsS2p1UX+DP6RicdxAYCCeRK8mzCldCS0W9A+5 alice@ws.example.com"
        ]
        password = "hunter3!"
  }
  user {
        username = "bob"
        publickeys = ["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINOCh8br7CwZDMGmINyJgBip943QXgkf7XdXrDMJf5Dl bob@example.com"]
        password = "hunter333!"
  }
}
```

{% endcode %}

To create the virtual machine, you first let Terraform read the configuration file and show which changes need to be made to your infrastructure. You do this by running `terraform plan`.

Then we run the `terraform apply` command to build the infrastructure:

<pre data-title="Commands and output (commands and interactive inputs are highlighted)"><code><strong>terraform apply
</strong>
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # glesys_server.kvm[0] will be created
  + resource "glesys_server" "kvm" {
      + bandwidth    = 100
      + cpu          = 1
      + datacenter   = "Stockholm"
      + hostname     = "www1"
      + id           = (known after apply)
      + ipv4_address = (known after apply)
      + ipv6_address = (known after apply)
      + memory       = 1024
      + platform     = "KVM"
      + storage      = 20
      + template     = "debian-12"

      + user {
          + password   = "hunter3!"
          + publickeys = [
              + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINOCh8br7CwZDMGmINyJgBip943QXgkf7XdXrDMJf5Dl alice@example.com",
              + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOfN4dBsS2p1UX+DP6RicdxAYCCeRK8mzCldCS0W9A+5 alice@ws.example.com",
            ]
          + username   = "alice"
        }
      + user {
          + password   = "hunter333!"
          + publickeys = [
              + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINOCh8br7CwZDMGmINyJgBip943QXgkf7XdXrDMJf5Dl bob@example.com",
            ]
          + username   = "bob"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

<strong>  Enter a value: yes
</strong>
glesys_server.kvm[0]: Creating...
glesys_server.kvm[0]: Creation complete after 4s [id=kvm123456]
</code></pre>

After a successful run, you can move on to the next step.

#### Show the current state

With the `terraform show` command you can view the final result of the run. Details such as IPv4/IPv6 addresses have now been returned from the API.

{% code title="Command" %}

```
terraform show
```

{% endcode %}

{% code title="Output" %}

```
resource "glesys_server" "kvm" {
    bandwidth    = 100
    cpu          = 1
    datacenter   = "Stockholm"
    hostname     = "www1"
    id           = "kvm123456"
    ipv4_address = "46.246.39.1"
    ipv6_address = "2a00:1a28:1410:5::1"
    memory       = 1024
    platform     = "KVM"
    storage      = 20
    template     = "debian-12"

    user {
        password   = "hunter3!"
        publickeys = [
            "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINOCh8br7CwZDMGmINyJgBip943QXgkf7XdXrDMJf5Dl alice@example.com",
            "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOfN4dBsS2p1UX+DP6RicdxAYCCeRK8mzCldCS0W9A+5 alice@ws.example.com",
        ]
        username   = "alice"
    }
    user {
        password   = "hunter333!"
        publickeys = [
            "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINOCh8br7CwZDMGmINyJgBip943QXgkf7XdXrDMJf5Dl bob@example.com",
        ]
        username   = "bob"
    }
}
```

{% endcode %}

### Step 3: Increase the disk size

To increase the virtual machine’s disk size, simply modify the value in `example.tf` and run `terraform apply` again. In this example, we have changed it from 20 to 30 GiB.

<pre data-title="Commands and output (commands and interactive inputs are highlighted)"><code><strong>terraform apply
</strong>glesys_server.kvm[0]: Refreshing state... [id=kvm123456]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # glesys_server.kvm[0] will be updated in-place
  ~ resource "glesys_server" "kvm" {
        id           = "kvm123456"
      ~ storage      = 20 -> 30
        # (9 unchanged attributes hidden)

        # (2 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

<strong>  Enter a value: yes
</strong>
glesys_server.kvm[0]: Modifying... [id=kvm123456]
glesys_server.kvm[0]: Modifications complete after 1s [id=kvm123456]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
</code></pre>

## Contribute open-source code

Everyone is welcome to contribute to the project with new features, improvements, and bug fixes. Read more about how to get started [here](https://github.com/glesys/terraform-provider-glesys/blob/main/README.md).

## Further reading

* [An introduction to Glesys API](https://github.com/glesys/api-docs/wiki/Api-Introduction).
* [The complete API documentation](https://github.com/glesys/api-docs/wiki/API-Documentation).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.glesys.com/platform/control-panel/api/real-world-use-cases/getting-started-with-terraform-in-glesys-cloud.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
