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

Workspace is not selected during init #661

Closed
calvinbui opened this issue Jun 4, 2019 · 10 comments · Fixed by #1363
Closed

Workspace is not selected during init #661

calvinbui opened this issue Jun 4, 2019 · 10 comments · Fixed by #1363
Labels
question Further information is requested

Comments

@calvinbui
Copy link

When running atlantis plan -w dev -d terraform/environments/gcp/datascience/skepsi, Atlantis will fail as it cannot select a default workspace.

The output:

Ran Plan for dir: terraform/environments/gcp/datascience/skepsi workspace: dev

Plan Error


running "/usr/local/bin/terraform0.11.14 init -input=false -no-color -upgrade" in "/home/atlantis/.atlantis/repos/autopilothq/infrastructure/148/dev/terraform/environments/gcp/datascience/skepsi": exit status 1
Upgrading modules...
- module.skepsi-container
  Updating source "github.com/terraform-google-modules/terraform-google-container-vm"

Initializing the backend...

Successfully configured the backend "remote"! Terraform will automatically
use this backend unless the backend configuration changes.
The currently selected workspace (default) does not exist.
  This is expected behavior when the selected workspace did not have an
  existing non-empty state. Please enter a number to select a workspace:
  
  1. dev
  2. stg

  Enter a value: 
Error selecting workspace: input not a valid number

The same happens output occurs on my workstation but I am able to get past it by selecting 1 or 2 as my default workspace.

atlantis.yaml

version: 3
projects:
- dir: terraform/environments/gcp/datascience/skepsi
  terraform_version: v0.11.14
  autoplan:
    enabled: false
@lkysow
Copy link
Member

lkysow commented Jun 4, 2019

Hmm, looks like a bug with tf. Can you figure out a way for Atlantis to run this in automation? Will we have to detect the input?

You can probably fix for now with a custom workflow

@lkysow lkysow added the question Further information is requested label Jun 4, 2019
@calvinbui
Copy link
Author

calvinbui commented Jun 4, 2019

there's a few tickets about this actually so it's not Atlantis related: hashicorp/terraform#21393

I was able to workaround it using a custom workflow

workflows:
  dev:
    plan:
      steps:
        - run: "terraform$ATLANTIS_TERRAFORM_VERSION init -upgrade -input=false -no-color || :" # can error out if there is no default workspace
        - run: "terraform$ATLANTIS_TERRAFORM_VERSION workspace select -no-color $WORKSPACE"
        - run: "terraform$ATLANTIS_TERRAFORM_VERSION init -upgrade -input=false -no-color"
        - plan
  stg:
    plan:
      steps:
        - run: "terraform$ATLANTIS_TERRAFORM_VERSION init -upgrade -input=false -no-color || :" # can error out if there is no default workspace
        - run: "terraform$ATLANTIS_TERRAFORM_VERSION workspace select -no-color $WORKSPACE"
        - run: "terraform$ATLANTIS_TERRAFORM_VERSION init -upgrade -input=false -no-color"
        - plan

@lkysow
Copy link
Member

lkysow commented Jun 5, 2019

Looks like it wasn't backported to 0.11 so users will have to either use your workaround or upgrade to 0.12. Thanks for doing the research!

@lkysow lkysow closed this as completed Jun 5, 2019
@andyshinn
Copy link

I'm running into this even on 0.12. Does Atlantis set TF_WORKSPACE for init? It works locally for me but Atlantis is failing saying The currently selected workspace (default) does not exist. But I have specified the workspaces names for each project in atlantis.yaml.

@lkysow
Copy link
Member

lkysow commented Jan 28, 2020

Are you using custom workflows? The default plan step handles changing workspaces and creating one if it doesn't exist but if you're using custom workflows you need to write that yourself.

@andyshinn
Copy link

andyshinn commented Jan 28, 2020

No. But I solved this by creating one that looks like:

workflows:
  staging:
    plan:
      steps:
      - env:
          name: TF_WORKSPACE
          command: 'echo "$WORKSPACE"'
      - init
      - plan:
          extra_args: ["-var-file", "vars/staging.tfvars"]
    apply:
      steps:
      - env:
          name: TF_WORKSPACE
          command: 'echo "$WORKSPACE"'
      - apply

I added extra args after I realized I needed a custom workflow anyways. Should this be necessary?

@lkysow
Copy link
Member

lkysow commented Jan 28, 2020

What does your full atlantis.yaml look like? I re-read your original comment and you said that you have the workspace name set and it's not using it?

@m1pl
Copy link
Contributor

m1pl commented Jan 26, 2021

I'm currently running into this issue:

running "/usr/local/bin/terraform init -input=false -no-color -upgrade" in "/atlantis-data/repos/<redacted>/staging/tf": exit status 1

Initializing the backend...

The currently selected workspace (default) does not exist.
  This is expected behavior when the selected workspace did not have an
  existing non-empty state. Please enter a number to select a workspace:
  
  1. staging

  Enter a value: 

Error: Failed to select workspace: input not a valid number

Additional info:

/ $ terraform --version
Terraform v0.14.3

Your version of Terraform is out of date! The latest version
is 0.14.5. You can update by downloading from https://www.terraform.io/downloads.html

atlantis.yaml

version: 3
projects:
  - dir: tf
    workspace: staging

I am not using custom workflows. Any help debugging it would be appreciated.

@andyshinn
Copy link

You need to add the workspace as env before init and apply:

workflows:
  staging:
    plan:
      steps:
      - env:
          name: TF_WORKSPACE
          command: 'echo "$WORKSPACE"'
      - init
      - plan
    apply:
      steps:
      - env:
          name: TF_WORKSPACE
          command: 'echo "$WORKSPACE"'
      - apply

@m1pl
Copy link
Contributor

m1pl commented Jan 26, 2021

@andyshinn I'd rather not customize the workflows just for the workspace to be set correctly. I'll open a PR to correctly set the TF_WORKSPACE env var during prepareCmd.

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

Successfully merging a pull request may close this issue.

4 participants