Use variable in Terraform remote backend

[亡魂溺海] 提交于 2020-08-15 12:07:14

问题


# Using a single workspace:
terraform {
  backend "remote" {
    hostname = "app.terraform.io"
    organization = "company"

    workspaces {
      name = "my-app-prod"
    }
  }
}

For Terraform remote backend, would there be a way to use variable to specify the organization / workspace name instead of the hardcoded values there?

The Terraform documentation didn't seem to mention anything related either.


回答1:


The backend configuration documentation goes into this in some detail. The main point to note is this:

Only one backend may be specified and the configuration may not contain interpolations. Terraform will validate this.

If you want to make this easily configurable then you can use partial configuration for the static parts (eg the type of backend such as S3) and then provide config at run time interactively, via environment variables or via command line flags.

I personally wrap Terraform actions in a small shell script that runs terraform init with command line flags that uses an appropriate S3 bucket (eg a different one for each project and AWS account) and makes sure the state file location matches the path to the directory I am working on.




回答2:


I had the same problems and was very disappointed with the need of additional init/wrapper scripts. Some time ago I started to use Terragrunt.

It's worth taking a look at Terragrunt because it closes the gap between Terraform and the lack of using variables at some points, e.g. for the remote backend configuration: https://terragrunt.gruntwork.io/docs/getting-started/quick-start/#keep-your-backend-configuration-dry



来源:https://stackoverflow.com/questions/56829891/use-variable-in-terraform-remote-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!