In this article, we will discover how to migrate an existing Terraform state file from a standard remote backend (Scaleway BLOB storage) to Terraform Cloud.
Prerequisites
- A Terraform Cloud account.
- An existing state file.
Procedure
First import the state file from the current location back to a local file. You should normally be able to do this with the terraform state pull > terraform.tfstate
command but for one reason I could not determine, I was not able to do it that way … So I did it the old fashioned way.
Log on to Scaleway BLOB bucket and download the content of the tfstate
file locally in Terraform repository (be sure that the file is included in your .gitignore
file).
Replace the backend configuration in main.tf
from
terraform {
backend "s3" {
}
}
to
terraform {
backend "local" {
}
}
Run terraform init -reconfigure
to get a local copy of the remote state file.
Check that the tfstate
file is correct by running terraform plan
and checking that nothing will be changed.
No changes. Infrastructure is up-to-date.
This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.
Now we’re ready to migrate the state file to Terraform Cloud. Reconfigure the backend section as below:
terraform {
backend "remote" {
hostname = "app.terraform.io"
organization = "<YOUR-ORG-NAME>"
workspaces {
name = "<YOUR-WORKSPACE>"
}
}
}
Create a token on Terraform Cloud using
terraform login
Terraform will request an API token for app.terraform.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:
C:\Users\user_name\AppData\Roaming\terraform.d\credentials.tfrc.json
Do you want to proceed?
Only 'yes' will be accepted to confirm.
Enter a value: yes
---------------------------------------------------------------------------------
Terraform must now open a web browser to the tokens page for app.terraform.io.
If a browser does not open this automatically, open the following URL to proceed:
https://app.terraform.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:
C:\Users\user_name\AppData\Roaming\terraform.d\credentials.tfrc.json
Token for app.terraform.io:
Enter a value:
Retrieved token for user sebpon
---------------------------------------------------------------------------------
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 app.terraform.io.
Now run again terraform init
.
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 "local" to "remote".
Acquiring state lock. This may take a few moments...
Do you want to copy existing state to the new backend?
Pre-existing state was found while migrating the previous "local" 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...
- Using previously-installed scaleway/scaleway v1.17.2
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, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.
* scaleway/scaleway: version = "~> 1.17.2"
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.
You can check the workspace of your Terraform Cloud account where you migrated the state file.
As my Terraform Cloud Workspace is configured to run plan
& apply
remotely on Terraform Cloud platform (it can be configured to run locally as well), only the output is streamed on my local console when I run terraform plan
.
terraform plan
Running plan in the remote backend. Output will stream here. Pressing Ctrl-C
will stop streaming the logs, but will not stop the plan running remotely.
Preparing the remote plan...
To view this run in a browser, visit:
https://app.terraform.io/app/seblab/scw_iac/runs/run-Tc1x2ZdtAn1Hg9rL
Waiting for the plan to start...
Terraform v0.13.5
Configuring remote state backend...
Initializing Terraform configuration...
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
scaleway_instance_security_group.def-sg: Refreshing state... [id=fr-par-1/1fbbfb1b-257e-4bab-bad9-7ef8f8915bd7]
scaleway_instance_ip.server_ip: Refreshing state... [id=fr-par-1/c24e3be3-484d-4b37-be8b-f2d451b46c78]
scaleway_object_bucket.blob: Refreshing state... [id=fr-par/blob]
scaleway_instance_server.seblab: Refreshing state... [id=fr-par-1/0cd67146-de29-420d-a652-20f021cc4a34]
------------------------------------------------------------------------
No changes. Infrastructure is up-to-date.
This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.
Furthermore, I linked the Terraform workspace to a Github repository, I cannot run apply from my local terminal. I need to publish a commit to my Github repository to start a run
.
As you can see, this run has detected that nothing would change and thus did not trigger the apply command.
This concludes the article.
See you soon.