Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cdktf cli diff handler does not migrate state to different backend #3704

Open
1 task done
Ranguna opened this issue Aug 18, 2024 · 1 comment
Open
1 task done

cdktf cli diff handler does not migrate state to different backend #3704

Ranguna opened this issue Aug 18, 2024 · 1 comment
Labels
bug Something isn't working new Un-triaged issue

Comments

@Ranguna
Copy link

Ranguna commented Aug 18, 2024

Expected Behavior

cdktf cli should trigger a state migration to the terraform process

Actual Behavior

Terraform fails to initialise and shows the following error:

Error: Backend configuration changed
            │ 
            │ A change in the backend configuration has been detected, which may require
            │ migrating existing state.
            │ 
            │ If you wish to attempt automatic migration of the state, use "terraform init
            │ -migrate-state".
            │ If you wish to store the current configuration with no changes to the state,
            │ use "terraform init -reconfigure".

Steps to Reproduce

  1. Create a stack with a local state;
  2. Change stack to use a remote state backend;
  3. run cdktf diff --migrate-state

Versions

language: ts
"cdktf": "^0.20.
"cdktf-cli": "^0.20.8",
"constructs": "^10.3.0"
Terraform/1.5.7

Providers

hashicorp/google-beta@~> 5.41

Gist

Can't migrate state to a different backend.

Possible Solutions

No response

Workarounds

Manually editing node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:

  • changing from migrateState:this.options.migrateState to migrateState:true;
  • and adding ,e.migrateState&&r.push("-migrate-state") next to e.noColor&&r.push("-no-color") and before the ;;

Workaround for a possibly separate issue of migrating the state (see #3704 (comment)):

  • changing from Should Terraform migrate your existing state to Do you want to copy existing state to the new backend in node_modules/cdktf-cli/bundle/bin/cmds/handlers.js.

Anything Else?

No response

References

A possible reason for this is that the migrateState flag is not being passed when the diff handler initialises terraform here:

const stack = this.getStackExecutor(

The method getStackExecutor accepts a second argument with the cli options, which are spread here:

and later used here:
migrateState: this.options.migrateState ?? false,

The value of this.options.migrateState is undefined at that time.

Not only that, but it seems the terraform cli package is also not passing the -migrate-state to the terraform process:

Relevant docs:

3. Run `cdktf diff <stack name> --migrate-state` to migrate the state into HCP Terraform or Terraform Enterprise.

Help Wanted

  • I'm interested in contributing a fix myself

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment
@Ranguna Ranguna added bug Something isn't working new Un-triaged issue labels Aug 18, 2024
@Ranguna
Copy link
Author

Ranguna commented Aug 18, 2024

Additionally, Terraform/1.9.4 has the following prompt for state migration:

Do you want to copy existing state to the new backend

But the cdktf cli seems to be looking for this instead:

if (data.includes("Should Terraform migrate your existing state?")) {

I can open a separate issue for this if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working new Un-triaged issue
Projects
None yet
Development

No branches or pull requests

1 participant