CLI Driven Workspace

CLI Driven Workspace

If an existing workflow already exists that utilizes the native Terraform CLI or you just want the flexibility to use it, you can continue to use the Terraform CLI as it is fully supported in Scalr. Scalr will execute the runs in a container in the Scalr backend, but the logs and output will still be sent back to your console.

To utilize Scalr as a remote backend there are a few simple steps:

  1. Obtain an API token by running terraform login <account-name>.scalr.io
  2. Create a workspace
  3. Execute terraform init

Create API Token

A personal access token can be created by clicking on your username on the bottom left of the Scalr UI and going to "Personal Access Tokens". A token can also be created by running terraform login <account-name>.scalr.io. Ensure you update to your actual account name:

terraform login <account-name>.scalr.io
Terraform will request an API token for <account-name>.scalr.io using your browser.

If login is successful, Terraform will store the token in plain text in
the following file for use by subsequent commands:
    /Users/name/.terraform.d/credentials.tfrc.json

Do you want to proceed?
  Only 'yes' will be accepted to confirm.

 Enter a value: yes

This will redirect you to the Scalr UI to create the API token. Copy the token and paste it into the command prompt:

---------------------------------------------------------------------------------

Terraform must now open a web browser to the tokens page for docs.scalr.io.

If a browser does not open this automatically, open the following URL to proceed:
    https://<account-name>.scalr.io/app/settings/tokens?source=terraform-login


---------------------------------------------------------------------------------

Generate a token using your browser, and copy-paste it into this prompt.

Terraform will store the token in plain text in the following file
for use by subsequent commands:
    /Users/name/.terraform.d/credentials.tfrc.json

Token for <account-name>.scalr.io:
  Enter a value:


Retrieved token for user [email protected]


---------------------------------------------------------------------------------

Success! Terraform has obtained and saved an API token.

The new API token will be used for any future Terraform command that must make
authenticated requests to <account-name>.scalr.io.

Create Workspace

  1. Click on “New Workspace”, give the workspace a name, select “CLI” as the workspace type, and save.
  2. On the workspace dashboard, click on “base backend configuration”, and copy the boilerplate code into your Terraform configuration. This code will automatically generate the hostname, organization id, and workspace name:
313
terraform {
  backend "remote" {
    hostname = "<account>.scalr.io"
    organization = "<org-id>"

    workspaces {
      name = "<workspace-name>"
    }
  }
}

Note: Workspace creation can be done from the CLI instead of the UI by using the same code snippet as above. If the workspace name is not reserved, Scalr will automatically create the new workspace after running terraform init.

  1. Lastly, from the command line, run terraform init. From this point forward, you can use the Terraform CLI as you normally would and commands will be executed remotely in Scalr.
terraform init

Initializing the backend...

Initializing provider plugins...
- Using previously-installed registry.scalr.io/scalr/scalr v1.0.0-rc26

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.

If there is an existing state file in the local system or state that was previously stored in another remote backend, then the terraform init command will automatically migrate the state to Scalr. See Migrating to Scalr for more details.

🚧

Warning

Version Mismatch. If the workspace is pre-created manually in Scalr and the Terraform version of the workspace does not match the version of the CLI then then following error will be displayed:
Error reading local state: state snapshot was created by Terraform vx.x.x, which is newer than current vx.x.x;.
If you see this error, please ensure the Terraform version of the CLI matches the Terraform version of the workspace.

Set Terraform Variables

Terraform variables can be set in the Terraform code or within the Scalr UI. If the configuration contains variables that do not have assigned values, then these must be assigned values in the Scalr workspace via the UI. Scalr will automatically create the variables.

2190

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

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

Set Shell Variables

If the Terraform configuration utilizes shell variables (export var=value), e.g. for credentials or to set values for Terraform input variables with TF_VAR_{variable_name}={value} (-parallelism, -var-file, etc).

Shell variables can be set at all levels in Scalr and are inherited by lower levels. Use environment or account level for shell variables that are needed in multiple workspaces or environments. Use workspace level for shell variables that are specific to individual Terraform configurations.

It is also possible to use the Scalr provider to pull output from one workspace and post it as an environment or account shell variable to make it easily available to all other workspaces.

2152

See Variables for full details.

Terraform CLI Support Commands

CLI commandScalr
apply
console
destroy
fmt
get
graph
import
init
output
plan
providers
show
state
taint
untaint
validate
version
workspace

Video

More of a visual learner? Check out this feature on YouTube.