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

Error with local tfstate while using Terraform cloud as backend #115

Open
arvind-kalyan opened this issue Jan 8, 2022 · 0 comments
Open

Comments

@arvind-kalyan
Copy link

Hello, I need help to resolve this error which I'm getting while working to build a flow on top of python_terraform APIs:

Running apply in the remote backend. Output will stream here. Pressing Ctrl-C will cancel the remote apply if it's still pending. If the apply started it, will stop streaming the logs, but will not stop the apply running remotely.\n\nPreparing the remote apply...\n\nTo view this run in a browser, visit:\nhttps://app.terraform.io/app/xxxxx/xxxxx/runs/run-xxxxxxx\n\nWaiting for the plan to start...\n\nTerraform v1.0.11\non linux_amd64\n\n------------ Terraform Cloud System Message ------------\n\nTerraform Cloud detected a terraform.tfstate file in your working\ndirectory: ./terraform.tfstate\n\nThe presence of this file causes a state migration error which prevents\nTerraform from running successfully. To fix this error please migrate\nyour local terraform.tfstate to Terraform Cloud and make sure the\nthe file is deleted.\n\nFor step by step instructions on how to migrate your terraform.tfstate\nfile from Terraform Open Source to Terraform Cloud, please see:\n\n https://www.terraform.io/docs/enterprise/migrate/index.html\n\n--------------------------------------------------------\n\nSetup failed: Terraform Cloud detected a terraform.tfstate file

I'm not able to understand how to migrate the state using python terraform APIs. I'm trying to setup an automated workflow to deploy TF based infra. Here's my code flow at a high level with relevant steps:

Setup:

  1. working directory with relevant TF config files
  2. terraform 1.0.11 binary installed
  3. Ubuntu 20.04.2 server

globals.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.64.2"
    }
  }
  required_version = "~> 1.0"

  backend "remote" {
    hostname = "app.terraform.io"
    organization = "xxxxxxx"
    workspaces {
      prefix = "xxx-"
    }
  }
}

Code flow:

# base_dir = directory where the config files are present 
createAutoTfVarsFile() # create a new terraform.auto.tfvars.json based on what is required
tf = Terraform(working_dir=base_dir, terraform_bin_path=TERRAFORM_PATH)
tf.init(backend_config={'token': tf_token}) --- This step creates the terraform.tfstate file locally
tf.create_workspace(workspace='newworkspace')
tf.set_workspace(workspace='newworkspace')
return_code, stdout, stderr = tf.apply(skip_plan=True, parallelism=10) --- I'm getting the above error at this step

I see that there is some migrate-state option that we can provide, but I'm not able to understand how to supply that during terraform.init (in python_terraform). Please let me know what I'm missing. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant