hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.87k stars 455 forks source link

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

Open Ranguna opened 2 months ago

Ranguna commented 2 months ago

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:

Workaround for a possibly separate issue of migrating the state (see https://github.com/hashicorp/terraform-cdk/issues/3704#issuecomment-2295293154):

No response

References

A possible reason for this is that the migrateState flag is not being passed when the diff handler initialises terraform here: https://github.com/hashicorp/terraform-cdk/blob/4f8c0c9984b130ffa3ce0537977dc09a08281782/packages/%40cdktf/cli-core/src/lib/cdktf-project.ts#L400

The method getStackExecutor accepts a second argument with the cli options, which are spread here: https://github.com/hashicorp/terraform-cdk/blob/4f8c0c9984b130ffa3ce0537977dc09a08281782/packages/%40cdktf/cli-core/src/lib/cdktf-project.ts#L333 and later used here: https://github.com/hashicorp/terraform-cdk/blob/4f8c0c9984b130ffa3ce0537977dc09a08281782/packages/%40cdktf/cli-core/src/lib/cdktf-stack.ts#L261

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: https://github.com/hashicorp/terraform-cdk/blob/4f8c0c9984b130ffa3ce0537977dc09a08281782/packages/%40cdktf/cli-core/src/lib/models/terraform-cli.ts#L123

Relevant docs: https://github.com/hashicorp/terraform-cdk/blob/4f8c0c9984b130ffa3ce0537977dc09a08281782/website/docs/cdktf/concepts/remote-backends.mdx#L348

Help Wanted

Community Note

Ranguna commented 2 months ago

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: https://github.com/hashicorp/terraform-cdk/blob/4f8c0c9984b130ffa3ce0537977dc09a08281782/packages/%40cdktf/cli-core/src/lib/models/terraform-cli.ts#L148

I can open a separate issue for this if needed.