fpco / terraform-aws-foundation

Establish a solid Foundation on AWS with these modules for Terraform
MIT License
203 stars 99 forks source link

using workspace_id #293

Closed JoseD92 closed 4 years ago

JoseD92 commented 4 years ago

Changes tf-cloud-credential so that it receives the workspace_id as an argument instead of searching it with a datasource, because the datasource brings problems when the workspace does not exists

JoseD92 commented 4 years ago

The error for this PR happens when using this module and the code for making the tf workspace to which the module will add the variables to are within the same tf project. This is because the module uses a datasource to get the id of the workspace, but datasources only work on existing resources so both can't be add to the project simultaneously. Resulting in errors like:

Error: Could not find workspace My-Project/project_workspace

  on modules/tf-cloud-credential/data.tf line 1, in data "tfe_workspace" "workspace":
   1: data "tfe_workspace" "workspace" {

with the additions of this PR the plan is successful as it uses the id of the new workspace directly, creating the workspace and variables:

  # module.workspace.tfe_workspace will be created
  + resource "tfe_workspace" "tfe_workspace" {
      + auto_apply            = false
      + external_id           = (known after apply)
      + file_triggers_enabled = true
      + id                    = (known after apply)
      + name                  = "myproject"
      + operations            = true
      + organization          = "myproject"
      + queue_all_runs        = true
      + ssh_key_id            = "***"
      + terraform_version     = "0.12.20"
      + working_directory     = "terraform/myproject"
    }

  # module.myproject.tfe_variable.workspace_aws_access_key_id will be created
  + resource "tfe_variable" "workspace_aws_access_key_id" {
      + category     = "env"
      + hcl          = false
      + id           = (known after apply)
      + key          = "AWS_ACCESS_KEY_ID"
      + sensitive    = true
      + value        = (sensitive value)
      + workspace_id = (known after apply)
    }

  # module.myproject.tfe_variable.workspace_aws_default_region will be created
  + resource "tfe_variable" "workspace_aws_default_region" {
      + category     = "env"
      + hcl          = false
      + id           = (known after apply)
      + key          = "AWS_DEFAULT_REGION"
      + sensitive    = false
      + value        = (sensitive value)
      + workspace_id = (known after apply)
    }

  # module.myproject.tfe_variable.workspace_aws_secret_access_key_id will be created
  + resource "tfe_variable" "workspace_aws_secret_access_key_id" {
      + category     = "env"
      + hcl          = false
      + id           = (known after apply)
      + key          = "AWS_SECRET_ACCESS_KEY"
      + sensitive    = true
      + value        = (sensitive value)
      + workspace_id = (known after apply)
    }
mcgirr commented 4 years ago

Thanks for this PR @JoseD92 I will review it and get it merged

mcgirr commented 4 years ago

This looks good to me. Thanks! Let's not remote the MR branch just yet until all the places that use it have been updated.