Apply for invite to IaCP

Migrating to Scalr

Overview

A lot of Terraform users will already be using Terraform locally and/or storing state in a remote backend such as S3 or TFE/C. As your usage of Terraform scales up you will see the need to take advantage of the features Scalr as a remote backend for operations and state.

To take advantage of these benefits you will need to migrate the Terraform state from your local machine or from a other remote backends. Both can easily be done with Scalr using the Terraform CLI.

The exact steps you need to follow vary depending on whether you are migrating from local usage (Terraform OSS) or from another remote backend.

Preparation

You must start by configuring the CLI with an API token so it can connect to Scalr. Also ensure you have the right Terraform version and active pipelines are stopped.

  1. In the Scalr UI Create an API Token

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

    OS

    File name and location

    Windows

    file must be named named terraform.rc and placed in the relevant user’s %APPDATA% directory. The physical location of this directory depends on your Windows version and system configuration; use $env:APPDATA in PowerShell to find its location on your system.

    All other

    ~/.terraformrc

    credentials "my.scalr.com" {
      token = "<user-token>"
    }
    
  3. Ensure that you are using Terraform 0.12.0 or higher.

  4. Stop all runs and existing pipelines to ensure data integrity.


Migrating State from Remote Backends

Examples of a remote backends are:

  • S3

  • Consul

  • TFE

  • Other enhanced remote operations backends

This example will migrate the state stored in an S3 remote backend, into Scalr. This method can be used for migrating from any remote state backend.

  1. Start point is a template configured with an S3 remote backend

terraform {
  backend "s3" {
    bucket = "scalr-demo-state"
    key    = "demo/state"
    region = "us-east-1"
  }
}
  1. This template has an active state file stored in S3 as a result of running terrafrom apply.

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_instance.scalr_test will be created
  + resource "aws_instance" "scalr_test" {

:
:

        }
    }

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.

  Enter a value: yes

aws_instance.scalr_test: Creating...
aws_instance.scalr_test: Still creating... [10s elapsed]
aws_instance.scalr_test: Still creating... [20s elapsed]
aws_instance.scalr_test: Creation complete after 22s [id=i-0228278100efd9fb6]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

scalr_test_private_ip = [
  "10.0.0.xxx",
]
scalr_test_public_ip = [
  "54.197.41.xxx",
]
  1. To migrate to Scalr change the existing remote backed terraform { } code in your template to point to Scalr. You can get the organization id from the dashboard or environment switcher on the UI. The workspace can be pre-created in the Scalr UI or you can let terraform init do it in the next step.

    ../_images/org_id_2.png
terraform {
  backend "remote" {
    hostname = "my.scalr.com"
    organization = "org-ssccu6d5ch64lqg"
    workspaces {
      name = "migrate-demo"
    }
  }
}
  1. Run terraform init and the workspace will be created with the latest state file. When you are asked if you want to copy the existing state, say “Yes”. The Terraform CLI will automatically migrate the state from the old remote backend to Scalr.

# terraform init

Initializing the backend...
Backend configuration changed!

Terraform has detected that the configuration specified for the backend
has changed. Terraform will now check for existing state in the backends.


Terraform detected that the backend type changed from "s3" to "remote".
Do you want to copy existing state to the new backend?
  Pre-existing state was found while migrating the previous "s3" backend to the
  newly configured "remote" backend. No existing state was found in the newly
  configured "remote" backend. Do you want to copy this state to the new "remote"
  backend? Enter "yes" to copy and "no" to start with an empty state.

  Enter a value: yes


Successfully configured the backend "remote"! Terraform will automatically
use this backend unless the backend configuration changes.

Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 2.70"

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.
  1. Workspace has been created in Scalr and state imported as shown in this screen shot.

    ../_images/migrate_ws.png

Migrating from TF OSS

You can migrate to Scalr using your current working directory, or you can clone the directory first to allow you to verify the process without impacting your existing locally stored state files. Follow this if the either of these scenarios applies use case:

  • Locally stored state

  • Standard remote backend state storage, like S3. (Go to the next section if you are using an enhanced remote backend)

In either case, ensure that the terraform.tfstate exists in your working directory, if not, copy it into your working directory.

Important

If you clone your working directory, you MUST copy the terraform.tfstate files.

  1. Add the remote backed terraform { } code to your template to point to Scalr. You can get the organization id from the dashboard or environment switcher on the UI. The workspace can be pre-created in the Scalr UI or you can let terraform init do it in the next step.

    ../_images/org_id.png
terraform {
backend "remote" {
  hostname = "your.scalr.com"
  organization = "<organization-id>"
  workspaces {
    name = "<workspace-name>"
    }
   }
  }
  1. Run terraform init and the workspace will be created with the latest state file. When you are asked if you want to copy the existing state, say “Yes”.