For the complete documentation index, see llms.txt. This page is also available as Markdown.

CLI-driven Workflow

With the CLI integration, you can use Terrakube’s collaboration features in the Terraform CLI workflow that you are already familiar with. This gives you the best of both worlds as a developer who uses the Terraform CLI, and it also works well with your existing CI/CD pipelines.

You can initiate runs with the usual terraform plan and terraform apply commands and then monitor the run progress from your terminal. These runs run remotely in Terrakube.

Terrakube creates default templates for CLI-Driven workflow when you create a new organization, if you delete those templates you won't be able to run this workflow, check default templates for more information.

Configuration

To use the CLI-driven workflow the firts step will be to setup our project to use the terraform remote backend or the cloud block as the following example:

terraform {
  backend "remote" {
    hostname = "terrakube-api.example.com"
    organization = "simple"
    workspaces {
      name = "workspace1"
    }
  }
}

resource "random_string" "random" {
  length           = 16
  special          = true
  override_special = "/@£$"
}
terraform {
  cloud {
    organization = "simple"
    hostname = "terrakube-api.example.com"
    workspaces {
      name = "samplecloud"
    }
  }
}

resource "random_string" "random" {
  length           = 16
  special          = true
  override_special = "/@£$"
}
  • Hostname

    • This should be the Terrakube API

  • Organization

    • This should be the Terrakube organization where the workspace will be created.

  • Workspace

    • The name of the workspace to be created. Or you can use an existing workspace created in the UI using the API or CLI driven workflow.

    • Alternatively, the tags that select the workspaces to use. See Selecting Workspaces with Tags.

Selecting Workspaces with Tags

Instead of a single workspace name, the cloud block can select the workspaces to use by their tags. The tags attribute accepts either of two forms:

  • A list of tag keys, for example tags = ["development", "networking"]. A workspace matches when it has every key, whatever its values.

  • A map of key/value tags, for example tags = { env = "dev", app = "networking" }. A workspace matches when it has every key with exactly that value.

Key/value tags let you tell apart workspaces that share the same keys. For example, with myapp-dev (app = myapp, env = dev) and myapp-prod (app = myapp, env = prod) in the organization, the following configuration always selects myapp-dev:

When you run terraform init:

  • If no workspace matches the tags, Terraform offers to create one, and the new workspace gets the tags from the configuration.

  • If the selected workspace is missing some of the tags, Terraform adds them to the workspace. Tags that are not in the configuration, such as tags added in the UI, are kept.

  • Selecting a workspace that does not match the tags, for example with TF_WORKSPACE, fails with Invalid workspace selection.

When tags are set from the Terraform CLI, a workspace can have at most 10 tags.

Terraform login

Authenticate the CLI against Terrakube with the standard login command (use tofu login for OpenTofu):

Terraform opens your browser, you sign in with your identity provider, and the token is stored in the Terraform credentials file. Run it once for each hostname you use; if the registry has a different hostname from the API, log in to both (or put the same token in the credentials file for each).

Login through the Terrakube OAuth broker

Available from Terrakube 2.34.0. It is disabled by default.

When enabled, Terrakube brokers the sign-in through Dex and shows a consent page where you choose how long the token lives (1 to 365 days). The CLI receives a normal, revocable personal access token, which you can see and delete in your User Settings. After a successful login the browser may show a connection error to localhost; this is expected, because Terraform closes its temporary listener as soon as it has the token.

Administrators enable it with these environment variables. Set all of them on the API, and set TerraformLoginEnabled and TerraformLoginApiUrl on the registry too, so registry logins use the same broker:

Variable
Default
Description

TerraformLoginEnabled

false

Turns the broker on and publishes the login discovery document.

TerraformLoginApiUrl

Required when enabled. The public HTTPS URL of the API (for example https://terrakube-api.example.com), used for the OAuth callback and registry discovery. The API refuses to start with a non-HTTPS URL, except on localhost.

TerraformLoginDefaultDays

30

Token lifetime preselected on the consent page.

TerraformLoginMaxDays

365

Longest lifetime a user can pick.

TerraformLoginDexTokenUrl, TerraformLoginDexJwkSetUrl

Optional. Dex token and signing-key endpoints the API calls, for example an in-cluster Dex service URL, when the public Dex URL is not reachable from the API.

Also add <TerraformLoginApiUrl>/oauth/callback to the redirectURIs of your Dex static client; see User Authentication (Dex).

With the Helm chart, put them under env:

With Docker Compose, set TERRAFORM_LOGIN_ENABLED=true in docker-compose/.env.

The broker uses PKCE, only redirects to the local CLI (localhost, ports 10000-10010), and validates Dex tokens against its signing keys.

Terraform Init

After the authentication is completed you can run the following command to initialized the workspace

The output will look like this:

If the initialization is successfull you should be able to see something like this:

Implicit Workspace Creation

Terrakube will create a new workspace with the name you specify in the cloud block if it doesn’t already exist in your organization. You will see a message about this when you run terraform init.

Terraform Plan

Once the terraform initialization is completed we should be able to run a terraform plan remotely using the following:

You can include .tfvars files in the uploaded configuration. CLI run inputs apply only to the run; they do not change stored workspace variables.

If you go to the Terrakube UI, you will be able to see the workspace plan execution.

CLI variables in 2.34.0

Inputs supplied through -var, -var-file and TF_VAR_* are honored as encrypted variables for that run only. They do not update the workspace's stored variables. Supplying them requires Manage Workspace permission; a caller who can only plan receives 403 with the variable names.

CLI run variables use Terraform CLI argument/variable-file precedence above configuration terraform.tfvars and *.auto.tfvars. Typed HCL values retain their types with the updated executor. Invalid literals, expressions and interpolated values are rejected when the run is created. Both the plan and saved-plan apply use the run's values. See API-driven Workflow for encryption and size limits; its job variables field uses environment-variable placement instead.

For example:

Run permissions and restrictions

Auto-approving a non-speculative CLI run requires approve permission. Destroy workflows also enforce the separate destroy permission, and requireApproveToApply checks CLI templates that omit an approval step.

On a VCS/SSH workspace with branch restriction enabled, normal CLI plans still work, but apply/destroy runs using uploaded local configuration are refused. The built-in CLI terraform plan -destroy path is refused too. A CLI-driven workspace has no branch to restrict. See Run Restrictions.

Terraform Apply

You can also execute a terraform apply and it will run inside Terrakube remotely.

Terraform Destroy

Executing the terraform destroy is also possible.

Inside the UI the terraform destroy operation will look like this.

Last updated

Was this helpful?