minamijoyo / tfmigrate

A Terraform / OpenTofu state migration tool for GitOps
MIT License
1.12k stars 57 forks source link

Breaks with Terraform Cloud #99

Closed dex4er closed 2 years ago

dex4er commented 2 years ago

Reproduction:

Initialization:

git clone https://github.com/hashicorp/tfc-getting-started
cd tfc-getting-started
./scripts/setup.sh
# ... enter / enter / enter / enter / ...

Token for fakewebservice API (it is set in TFC already but we need it for local run too):

cat ~/.terraform.d/credentials.tfrc.json | jq '.credentials["app.terraform.io"].token' | sed 's/^/provider_token = /' > terraform.tfvars 

Change to new cloud backed:

sed -i 's/backend "remote"/cloud/' backend.tf
terraform init
# Enter a value: yes

./.tfmigrate.hcl

tfmigrate {
  migration_dir = "./tfmigrate"
  is_backend_terraform_cloud = false
  history {
    storage "local" {
      path = "./history.json"
    }
  }
}

./tfmigrate/test.hcl

migration "state" "test" {
  dir       = "."
  workspace = "getting-started"

  actions = [
    "mv fakewebservices_database.prod_db fakewebservices_database.prod_db2",
  ]

  force = true
}

tfmigrate plan:

v0.3.4 with is_backend_terraform_cloud = false:

$  tfmigrate plan
2022/08/04 10:34:03 [INFO] [runner] unapplied migration files: [test.hcl]
2022/08/04 10:34:03 [INFO] [runner] load migration file: tfmigrate/test.hcl
2022/08/04 10:34:03 [INFO] [migrator] start state migrator plan
2022/08/04 10:34:03 [INFO] [migrator@.] terraform version: 1.2.5
2022/08/04 10:34:03 [INFO] [migrator@.] initialize work dir
2022/08/04 10:34:06 [INFO] [migrator@.] get the current remote state
2022/08/04 10:34:09 [INFO] [migrator@.] override backend to local
2022/08/04 10:34:09 [INFO] [executor@.] create an override file
2022/08/04 10:34:09 [INFO] [migrator@.] creating local workspace folder in: terraform.tfstate.d/getting-started
2022/08/04 10:34:09 [INFO] [executor@.] switch backend to local
2022/08/04 10:34:09 [INFO] [migrator@.] compute a new state
2022/08/04 10:34:09 [INFO] [migrator@.] check diffs
2022/08/04 10:34:11 [INFO] [migrator@.] unexpected diffs, ignoring as force option is true: failed to run command (exited 2): terraform plan -state=/tmp/tmp1593536331 -out=/tmp/tfplan771370973 -input=false -no-color -detailed-exitcode
stdout:
fakewebservices_database.prod_db2: Refreshing state... [id=fakedb-We792NeCUZBoGjCm]
fakewebservices_vpc.primary_vpc: Refreshing state... [id=fakevpc-NUohxvfnu36ayu4j]
fakewebservices_server.servers[0]: Refreshing state... [id=fakeserver-nsUSsCUumb1Z4itt]
fakewebservices_server.servers[1]: Refreshing state... [id=fakeserver-LXMvK4tbtoGA3cd3]
fakewebservices_load_balancer.primary_lb: Refreshing state... [id=fakelb-PMJfhAwA8BDrXNop]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create
  - destroy

Terraform will perform the following actions:

  # fakewebservices_database.prod_db will be created
  + resource "fakewebservices_database" "prod_db" {
      + id   = (known after apply)
      + name = "Production DB"
      + size = 256
    }

  # fakewebservices_database.prod_db2 will be destroyed
  # (because fakewebservices_database.prod_db2 is not in configuration)
  - resource "fakewebservices_database" "prod_db2" {
      - id   = "fakedb-We792NeCUZBoGjCm" -> null
      - name = "Production DB" -> null
      - size = 256 -> null
    }

Plan: 1 to add, 0 to change, 1 to destroy.

─────────────────────────────────────────────────────────────────────────────

Saved the plan to: /tmp/tfplan771370973

To perform exactly these actions, run the following command to apply:
    terraform apply "/tmp/tfplan771370973"

stderr:
2022/08/04 10:34:11 [INFO] [executor@.] remove the override file
2022/08/04 10:34:11 [INFO] [executor@.] remove the workspace state folder
2022/08/04 10:34:11 [INFO] [executor@.] switch back to remote
2022/08/04 10:34:11 [ERROR] [executor@.] failed to switch back to remote: failed to run command (exited 1): terraform init -input=false -no-color -reconfigure -reconfigure
stdout:

Initializing Terraform Cloud...

stderr:

Error: Invalid command-line option

The -reconfigure option is for in-place reconfiguration of state backends
only, and is not needed when changing Terraform Cloud settings.

When using Terraform Cloud, initialization automatically activates any new
Cloud configuration settings.

2022/08/04 10:34:11 [ERROR] [executor@.] please re-run terraform init -reconfigure
2022/08/04 10:34:11 [INFO] [migrator] state migrator plan success!
$  terraform init

Initializing Terraform Cloud...
Migrating from backend "local" to Terraform Cloud.

v0.3.4 with is_backend_terraform_cloud = true:

Exactly the same as above.

98 with is_backend_terraform_cloud = false:

Exactly the same as above.

98 with is_backend_terraform_cloud = true:

Migrating from backend "local" to Terraform Cloud.

Initializing provider plugins...
- Reusing previous version of hashicorp/fakewebservices from the dependency lock file
- Using previously-installed hashicorp/fakewebservices v0.2.3

Terraform Cloud has been successfully initialized!

You may now begin working with Terraform Cloud. Try running "terraform plan" to
see any changes that are required for your infrastructure.

If you ever set or change modules or Terraform Settings, run "terraform init"
again to reinitialize your working directory.
 dex4er  ~  Sources/terraform/tfc-getting-started  piotr.roszatycki  dev  main  1✎  4+  $  tfmigrate.dex4er plan
2022/08/04 10:37:14 [INFO] [runner] unapplied migration files: [test.hcl]
2022/08/04 10:37:14 [INFO] [runner] load migration file: tfmigrate/test.hcl
2022/08/04 10:37:14 [INFO] [migrator] start state migrator plan
2022/08/04 10:37:14 [INFO] [migrator@.] terraform version: 1.2.5
2022/08/04 10:37:14 [INFO] [migrator@.] initialize work dir
2022/08/04 10:37:17 [INFO] [migrator@.] get the current remote state
2022/08/04 10:37:19 [INFO] [migrator@.] override backend to local
2022/08/04 10:37:19 [INFO] [executor@.] create an override file
2022/08/04 10:37:19 [INFO] [migrator@.] creating local workspace folder in: terraform.tfstate.d/getting-started
2022/08/04 10:37:19 [INFO] [executor@.] switch backend to local
2022/08/04 10:37:19 [INFO] [migrator@.] compute a new state
2022/08/04 10:37:20 [INFO] [migrator@.] check diffs
2022/08/04 10:37:21 [INFO] [migrator@.] unexpected diffs, ignoring as force option is true: failed to run command (exited 2): terraform plan -state=/tmp/tmp1348777574 -out=/tmp/tfplan1588401082 -input=false -no-color -detailed-exitcode
stdout:
fakewebservices_vpc.primary_vpc: Refreshing state... [id=fakevpc-NUohxvfnu36ayu4j]
fakewebservices_database.prod_db2: Refreshing state... [id=fakedb-We792NeCUZBoGjCm]
fakewebservices_server.servers[0]: Refreshing state... [id=fakeserver-nsUSsCUumb1Z4itt]
fakewebservices_server.servers[1]: Refreshing state... [id=fakeserver-LXMvK4tbtoGA3cd3]
fakewebservices_load_balancer.primary_lb: Refreshing state... [id=fakelb-PMJfhAwA8BDrXNop]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create
  - destroy

Terraform will perform the following actions:

  # fakewebservices_database.prod_db will be created
  + resource "fakewebservices_database" "prod_db" {
      + id   = (known after apply)
      + name = "Production DB"
      + size = 256
    }

  # fakewebservices_database.prod_db2 will be destroyed
  # (because fakewebservices_database.prod_db2 is not in configuration)
  - resource "fakewebservices_database" "prod_db2" {
      - id   = "fakedb-We792NeCUZBoGjCm" -> null
      - name = "Production DB" -> null
      - size = 256 -> null
    }

Plan: 1 to add, 0 to change, 1 to destroy.

─────────────────────────────────────────────────────────────────────────────

Saved the plan to: /tmp/tfplan1588401082

To perform exactly these actions, run the following command to apply:
    terraform apply "/tmp/tfplan1588401082"

stderr:
2022/08/04 10:37:21 [INFO] [executor@.] remove the override file
2022/08/04 10:37:21 [INFO] [executor@.] remove the workspace state folder
2022/08/04 10:37:21 [INFO] [executor@.] switch back to remote
2022/08/04 10:37:25 [INFO] [migrator] state migrator plan success!
minamijoyo commented 2 years ago

@dex4er Thank you for reporting this! It looks like a bug introduced in v0.3.4 via #94.

minamijoyo commented 2 years ago

Fixed in v0.3.5. Thanks to @dex4er 🎉