_images/login_button.png _images/signup_button.png



Terraform CLI with Scalr

Reading time: 2-3 minutes

Scalr is an enhanced remote backend for the Terraform CLI. This provides the following capabilities:

  • State files stored centrally in Scalr

  • Terraform operations (plan, apply, destroy) run in workspaces in Scalr

  • Automatic policy evaluation (if configured)

To utilize Scalr as a remote backed there are 3 steps:

  1. Obtain an API token from Scalr

  2. Add a backed configuration to your Terraform configuration

  3. Set input variables in the workspace


API Token

  1. At environment scope create a token

    _images/api_token.png
  2. Add the token to your CLI Configuration file.

  • Windows : terraform.rc in %APPDATA% directory.

  • Unix/Linux/Mac : ~/.terraformrc

    credentials "my-account.scalr.io" {
      token = "<user-token>"
    }
    

Backend Terraform Configuration

  1. Get the organization id from the environment switcher on the UI by hovering over, or get it from the Dashboard.

    _images/org_id.png
  2. Add a terraform block to your template. The hostname will be your local installation if IaCP is self-hosted. You can choose the workspace name at this point.

terraform {
  backend "remote" {
    hostname = "my-account.scalr.io"
    organization = "<id of the environment>"  // e.g. org-t4737mmm538mabc
    workspaces {
      name = "<workspace-name>"
    }
  }
}

Note

The ID of the environment can be either env-xxxxxxxxxxxxxxx or org-xxxxxxxxxxxxxxx

  1. Now run terraform init to create the workspace

Tip

If you have an existing state file on your local system or you previously had state store in another remote backed, then the terraform init command will automatically migrate the state to Scalr. See Migrating to Scalr for more details

Set Input Variables

If the Terraform configuration contains input variables that do not have assigned values, then these must be assigned values in then Scalr workspace via the UI. Scalr will automatically create the variables for you.

_images/intro_ws_vars_1.png _images/ws_vars_values.png

If the local workspace contains any *.auto.tfvars or terraform.tfvars files these will provide default variable values that Terraform will automatically use.

If variables in the above tfvars files have the same names as variables specified in the workspace, the workspace’s values will be used. For map variables the values in tfvars are merged with values in the same named variable in the workspace.



Supported CLI Commands

Scalr remote backend provides the following support for commands of Terraform CLI. Scalr only supports the CLI for versions >= 0.12.0:

CLI command

IaCP

apply

console

destroy

fmt

get

graph

import

init

output

plan

providers

show

state

taint

untaint

validate

version

workspace