hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.69k stars 9.55k forks source link

(Swift backend) documentation misleading for OS_CLOUD env variable #27709

Closed Phil1602 closed 2 years ago

Phil1602 commented 3 years ago

Terraform Version

Terraform v0.14.5

Terraform Configuration Files

This is only a snippet added to kubespray, see below.

backend.tf:

terraform {
  backend "swift" {
    cloud = "os-one" # this needs to match the OpenStack with swift enabled in the clouds.yaml and secure.yaml
    container  = "mycontainer"
    archive_container = "myarchivecontainer"
    state_name = "myterraform.state"
  }
}

Debug Output

Expected Behavior

According to the documentation:

cloud - (Optional; required if auth_url is not specified) An entry in a clouds.yaml file. See the OpenStack os-client-config documentation for more information about clouds.yaml files. If omitted, the OS_CLOUD environment variable is used.

The OS_CLOUD environment variable should (if the documentation states correctly) not have precedence over the attribute of the backend configuration block, because the variable is not omitted.

Actual Behavior

OS_CLOUD environment variable overrides the cloud attribute of the swift backend.

Workaround

Not using the OS_CLOUD env variable at all, but setting both attributes explicitly for provider and backend works fine.

terraform {
  backend "swift" {
    cloud = "os-one"
  }
}

provider "openstack" {
  cloud = "os-two"
}

terraform apply -var-file=cluster.tfvars ../../contrib/terraform/openstack/

Please note that the "workarkound" is fine, but it would be nice to have the documentation adjusted to make the priority OS_CLOUD > value in terraform (more) clear.

Steps to Reproduce

I encountered this issue while using kubespray. However kubespray needs many configuration parameters. I can provide a minimal and more detailed example if wanted.

Prerequisites:

  1. Have two different OpenStack environment within one clouds.yaml and secure.yaml file (one of them needs Container/Swift enabled)

Steps:

  1. Clone kubespray
  2. Prepare kubespray terraform, fill variables for flavor etc (However any standalone openstack ressource should be sufficient to test this)
  3. Setup external backend (placing this file into the contrib/terraform/openstack directory, see above)
  4. terraform init ../../contrib/terraform/openstack -> After the initialization you can check cat .terraform/terraform.tfstate | grep cloud that the correct cloud has been set.
  5. OS_CLOUD=os-two terraform apply -var-file=cluster.tfvars ../../contrib/terraform/openstack
    ....
    2021/02/08 15:29:17 [TRACE] Meta.Backend: using already-initialized, unchanged "swift" backend configuration
    2021/02/08 15:29:17 [DEBUG] OpenStack allowReauth: false
    2021/02/08 15:29:19 [DEBUG] Archive_container set, enabling object versioning
    2021/02/08 15:29:19 [DEBUG] OpenStack Region is: RegionOne
    Error: No suitable endpoint could be found in the service catalog.

    The error occurs, because OS_CLOUD seems to override the cloud value of the backend and tries to retrieve the data from os-two instead of os-one

Thanks in advance!

apparentlymart commented 2 years ago

Hello! Thanks for reporting this.

We removed the Swift backend in Terraform v1.3 because it hasn't had a dedicated maintainer for a few versions now and so it was becoming problematic to keep working in newer Terraform versions. Since that backend is no longer present in the Terraform codebase, I'm going to close this issue.

If you're currently using this backend with an older version of Terraform, see Removal of Deprecated State Storage Backends in the Terraform v1.3 upgrade guide for some information on the available options for migration.

Thanks again!

github-actions[bot] commented 2 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.