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

fix: Setting working_directory shouldn't depend on a VCS connection #49

Merged
merged 1 commit into from
Apr 30, 2024
Merged

fix: Setting working_directory shouldn't depend on a VCS connection #49

merged 1 commit into from
Apr 30, 2024

Conversation

borisroman
Copy link
Contributor

Hi!

When not using the VCS connection (ie. when doing local development, but with remote run/state) you still need to specify the working_directory. Setting it to null when not having a VCS connection prevents a successful TF run.

Cheers,
Boris

@shoekstra
Copy link
Member

Hi Boris! Nice to see you again...

The code looks to be written as intended: only set a working directory if using a VCS connection; we made it like this to support API driven workspaces which should operate the same as CLI driven workspaces (which seems to be what you refer to?).

If you can please help us out by providing the error from the plan or an example showing how you set var.tfe_workspace and/or var.additional_tfe_workspaces, we can look further.

Thanks!

@borisroman
Copy link
Contributor Author

borisroman commented Sep 13, 2023

Hi Stephen, nice to see you again too!

I'm indeed using it with CLI-driven workspaces. Please take a look at the following example.

data "tfe_oauth_client" "default" {
  name         = "GitHub.com"
  organization = "example"
}

module "aws" {
  source = "github.com/schubergphilis/terraform-aws-mcaf-avm?ref=v2.10.0"

  create_default_workspace = false
  name                     = "aws"
  tags                     = { Terraform = true }

  account = {
    alias_prefix        = ""
    email               = "[email protected]"
    organizational_unit = "example"
    sso_email           = "[email protected]"
  }

  tfe_workspace = {
    default_region        = "eu-west-1"
    organization          = "example"
    repository_identifier = "example/infrastructure"
    vcs_oauth_token_id    = data.tfe_oauth_client.default.oauth_token_id
  }

  additional_tfe_workspaces = {
    development = {
      connect_vcs_repo  = false
      execution_mode    = "remote"
      name              = "development"
      working_directory = "terraform/development"

      trigger_prefixes = [
        "/modules",
        "/terraform/development",
      ]
    }

    production = {
      connect_vcs_repo  = false
      execution_mode    = "remote"
      name              = "production"
      working_directory = "terraform/production"

      trigger_prefixes = [
        "/modules",
        "/terraform/production",
      ]
    }
  }
}

When you then go to one of the workspaces (ie. terraform/development) on your laptop and run a terraform apply it will do a remote execution which then runs in the root directory, which doesn't have any terraform files, and hence failed. Setting the working directory in the Terraform Cloud console to terraform/development, and running terraform apply again will result in a successful run.

$ terraform apply
Running apply in Terraform Cloud. 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.

Preparing the remote apply...

To view this run in a browser, visit:
xxx

Waiting for the plan to start...

Terraform v1.5.7
on linux_amd64
Initializing plugins and modules...

Initializing Terraform Cloud...
Initializing modules...
xxx

╷
│ Error: Unreadable module directory
│
│ Unable to evaluate directory symlink: lstat ../../modules: no such file or
│ directory

I hope this clarifies enough!

Cheers,
Boris

@svanharmelen
Copy link

To my understanding you always need to set a working_directory regardless of using a VCS connected workspace. It just means the run will use a specific directory to find it's config files and has the same effect in both execution modes (assuming you have a single git repo with a file tree for different TF workspaces).

@shoekstra
Copy link
Member

I'm sorry, I completely missed the reply from @borisroman 😬

Thanks for clarifying, and yes, reading the CLI docs again it seems this is needed if using a monorepo.

It looks fine to me, if you could please rebase and amend the commit to have a fix: prefix then this should be good to go 👍

@borisroman
Copy link
Contributor Author

Done @shoekstra !

@shoekstra shoekstra changed the title Setting the working_directory shouldn't depend on a VCS connection fix: Setting working_directory shouldn't depend on a VCS connection Apr 30, 2024
@shoekstra shoekstra added the fix Something isn't working label Apr 30, 2024
@shoekstra shoekstra merged commit 00c2508 into schubergphilis:master Apr 30, 2024
4 of 7 checks passed
@borisroman borisroman deleted the fix/working_directory branch April 30, 2024 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants