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.71k stars 9.55k forks source link

ability to use interpolation in backend config for remote state storage #15131

Open leighmhart opened 7 years ago

leighmhart commented 7 years ago

Hi there,

I'd like to use variables to configure backend for remote state storage and locking (s3 in this case) so that I can pop my remote-state.tf file into our GCP project template repository and not worry about anyone (including me) forgetting to set the key properly (so that tfstates are unique per project in the bucket).

Example:

terraform {
  backend "s3" {
    region = "us-east-1"
    bucket = "my-terraform-remote-state-bucket-name"
    key = "${var.gcp-project}/terraform.tfstate"
    lock_table = "my-terraform-remote-state-lock-table-name"
    encrypt = "true"
    acl = "private"
    shared_credentials_file = "/path/to/creds.txt"
    profile = "my-chicken-egg-profile"
    role_arn = "arn:aws:iam::NNNNNNNNNNNN:role/specialAccessForMeRoleKKThxBai"
  }
}

The error output for 0.9.6 is as follows:

Failed to load backend: Error loading backend config: 1 error(s) occurred:

* terraform.backend: configuration cannot contain interpolations

The backend configuration is loaded by Terraform extremely early, before
the core of Terraform can be initialized. This is necessary because the backend
dictates the behavior of that core. The core is what handles interpolation
processing. Because of this, interpolations cannot be used in backend
configuration.

If you'd like to parameterize backend configuration, we recommend using
partial configuration with the "-backend-config" flag to "terraform init".

I get the explanation here - makes perfect sense that I can't use ${module.project.id} as part of my key-name (because the remote state contains that value and - well - chicken/egg) - however, I should be able to access variables defined in variables.tf / terraform.tfvars as those are still local resources?

Thanks for any consideration!

Regards

Leigh

veqryn commented 7 years ago

I would like the same feature for different reasons. We have several identical environments, prod, stage, test/dev, and the only difference between them is the size of the instances and clusters. We use the same terraform directory for them, and switch off between them by changing which tfvar file is used. We also expect that the backend (the tfstate file) will get saved to different locations.

For example, we have:

dev.tfvars
stage.tfvars
prod.tfvars

The tfvars files looks like this:

env_name = "dev"
<other stuff...>

And we would like to do this with our backend:

terraform {
  required_version = "~> 0.9.8"
  backend "s3" {
    bucket  = "xxxxxx-terraform-backend"
    key     = "infrastructure/terraform/xxxxx/${var.env_name}/terraform.tfstate"
    region  = "us-east-1"
    profile = "yyyyyy"
  }
}
kamoljan commented 7 years ago

+1

CyborgShadow commented 7 years ago

+1

BijeshSamson11 commented 7 years ago

Can anyone tell me... In Terraform can we have custom names for state files and copy the state files to different location, while these values are being passed via variables? I have several clusters and environments with same configuration, I like to have all of them in single statck/folder.

Thanks in Advance, Bijesh

gangadhar01a commented 7 years ago

+1

dahui commented 7 years ago

+1

szpuni commented 7 years ago

I'm having exact same issue. It would be really great to have variable interpolations in backend. This way we could build modules with more flexibility.

linuxbsdfreak commented 7 years ago

+1. Would be gr8 to have interpolation for backend config.

veqryn commented 7 years ago

Or at the least, allow very "basic" interpolation. Like, it needs to be a variable supplied on the command line, env var, or through the vars file.

anshulshrivastava commented 7 years ago

+1

ghost commented 7 years ago

As an alternative, for consul backend. It appears that we can use environment variables for everything except path. Path is the one thing we need either variable interpolation for or to have exposed via env.

https://www.terraform.io/docs/backends/types/consul.html

why is CONSUL_PATH not a supported env variable?

perplexa commented 7 years ago

+1

rbramwell commented 7 years ago

+1

agnnn commented 7 years ago

+1

msloes commented 7 years ago

+1

nfollett-slalom commented 7 years ago

+1

cjcdoomed commented 7 years ago

+1