Civo has a certified Terraform provider that can be used to provision infrastructure resources on the Civo platform. In this short tutorial we will see how to create a Kubernetes cluster using the Civo Terraform provider.

Before we get begin, if you prefer a video tutorial I have also recorded a short walkthrough to get your started, which you can see below.

Getting started

You will need to have Terraform downloaded and set up for your operating system. It is available for free from the official Terraform website.

You will also need a Civo account. If you do not yet have an account, you can sign up here - it takes just a few minutes.

Using the Terraform provider

In order to tell Terraform to use Civo, we need to create a file with those instructions. You can do that by creating a provider.tf file with the below contents

terraform {
  required_providers {
    civo = {
      source = "civo/civo"
      version = "0.10.3"
    }
  }
}

provider "civo" {
  token = "token"
}

The token is the API key that you can find under your account security settings.

At the time of writing the latest version of the Civo provider is 0.10.3. Please refer to the official repository to make sure you use the version best version available.

terraform init

Once you have saved the provider.tf file, we need to initialize the provider. Run terrafrom init to initialize the latest Terraform provider. You should see something like the following:

$ terraform init
Initializing the backend...

Initializing provider plugins...
- Finding civo/civo versions matching "0.10.3"...
- Installing civo/civo v0.10.3...
- Installed civo/civo v0.10.3 (signed by a HashiCorp partner, key ID CA1DE390990EBE66)

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.

main.tf

It's time to write some infrastructure as code! For this, we will create a file main.tf with the cluster information that you specify, in the Terraform format:

resource "civo_kubernetes_cluster" "my-cluster" {
    name = "sammy"
    region = "LON1"
    applications = "Rancher"
    num_target_nodes = 3
    target_nodes_size = "g3.k3s.xsmall"
}

The above should create a 3 node cluster named sammy in LON1 region with the Rancher application. You can change the values as you see fit for your needs.

terraform plan

Run terraform plan to see the execution plan

$ terraform plan
terraform plan
civo_kubernetes_cluster.my-cluster: Refreshing state... [id=964190a8-a49a-4ff0-b9e4-082d283d4d2c]

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:

  # civo_kubernetes_cluster.my-cluster will be created
  + resource "civo_kubernetes_cluster" "my-cluster" {
      + api_endpoint           = (known after apply)
      + applications           = "Rancher"
      + built_at               = (known after apply)
      + created_at             = (known after apply)
      + dns_entry              = (known after apply)
      + id                     = (known after apply)
      + installed_applications = (known after apply)
      + instances              = (known after apply)
      + kubeconfig             = (known after apply)
      + kubernetes_version     = (known after apply)
      + master_ip              = (known after apply)
      + name                   = "sammy"
      + network_id             = (known after apply)
      + num_target_nodes       = 3
      + pools                  = (known after apply)
      + ready                  = (known after apply)
      + region                 = "LON1"
      + status                 = (known after apply)
      + target_nodes_size      = "g3.k3s.xsmall"
    }

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

terraform apply

The above looks good. Let's run terraform apply to actually create the cluster:

Terraform Apply command requesting confirmation to complete the execution

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

  Enter a value: yes

civo_kubernetes_cluster.my-cluster: Creating...
civo_kubernetes_cluster.my-cluster: Still creating... [10s elapsed]
civo_kubernetes_cluster.my-cluster: Still creating... [20s elapsed]
civo_kubernetes_cluster.my-cluster: Still creating... [30s elapsed]
civo_kubernetes_cluster.my-cluster: Still creating... [40s elapsed]
civo_kubernetes_cluster.my-cluster: Still creating... [50s elapsed]
civo_kubernetes_cluster.my-cluster: Still creating... [1m0s elapsed]
civo_kubernetes_cluster.my-cluster: Still creating... [1m10s elapsed]
civo_kubernetes_cluster.my-cluster: Still creating... [1m20s elapsed]
civo_kubernetes_cluster.my-cluster: Still creating... [1m30s elapsed]
civo_kubernetes_cluster.my-cluster: Still creating... [1m40s elapsed]
civo_kubernetes_cluster.my-cluster: Still creating... [1m50s elapsed]
civo_kubernetes_cluster.my-cluster: Still creating... [2m0s elapsed]
civo_kubernetes_cluster.my-cluster: Creation complete after 2m1s [id=b6e4821a-d899-4c42-8c5b-9728938310ad]

As you can see, the cluster has been created using Terraform. You can check that it is running either on the Civo website under your account, or using the Civo CLI by running civo k3s ls.

Civo CLI output showing our running cluster

Conclusion

You can use the Terraform provider to create other resources like virtual machine instances on Civo as well. Plus, the provider is open source so you can contribute to the repository or create issues for enhancements you would like to see.