在 Terraform 远程后端使用变量 [英] Use variable in Terraform remote backend

查看:32
本文介绍了在 Terraform 远程后端使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

对于 Terraform 远程后端,是否可以使用变量来指定组织/工作区名称而不是那里的硬编码值?

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

Terraform 文档似乎也没有提到任何相关的东西.

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

推荐答案

后端配置文档 对此进行了详细介绍.需要注意的要点是:

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

只能指定一个后端并且配置不得包含插值.Terraform 将对此进行验证.

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

如果您想使其易于配置,则可以使用 partial配置静态部分(例如后端的类型,如 S3),然后在运行时以交互方式提供配置,通过环境变量或通过命令行标志.

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.

我个人将 Terraform 操作包装在一个运行 terraform init 的小型 shell 脚本中,该脚本带有使用适当 S3 存储桶(例如,每个项目和 AWS 帐户的不同存储桶)的命令行标志,并确保状态文件位置与我正在处理的目录的路径相匹配.

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.

这篇关于在 Terraform 远程后端使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆