我可以在 TerraForm main.tf 文件中使用变量吗?
Can I use variables in the TerraForm main.tf file?
好的,所以我有三个 .tf
文件:main.tf
我将 azure 声明为提供者,resources.tf
我的所有资源都已声明,variables.tf
.
我使用 variables.tf
来存储 resources.tf
使用的密钥。
但是,我想使用存储在我的变量文件中的变量来填充后端范围中的字段,如下所示:
main.tf
:
provider "azurerm" {
version = "=1.5.0"
}
terraform {
backend "azurerm" {
storage_account_name = "${var.sa_name}"
container_name = "${var.c_name}"
key = "${var.key}"
access_key = "${var.access_key}"
}
}
存储在 variables.tf
中的变量是这样的:
variable "sa_name" {
default = "myStorageAccount"
}
variable "c_name" {
default = "tfstate"
}
variable "key" {
default = "codelab.microsoft.tfstate"
}
variable "access_key" {
default = "weoghwoep489ug40gu ... "
}
我在 运行 terraform init
:
时得到了这个
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".
有办法解决吗?我真的希望我所有的 keys/secrets 都在同一个文件中......而不是主文件中的一个键,我最好将其推送到 git.
Terraform 不太关心文件名:它只是加载当前目录中的所有 .tf
文件并处理它们。 main.tf
、variables.tf
和 outputs.tf
等名称是有用的约定,可让开发人员更轻松地浏览代码,但它们不会对 Terraform 的行为产生太大影响。
您看到错误的原因是您试图在 backend
配置中使用变量。不幸的是,Terraform 不允许在后端进行任何插值(任何 ${...}
)。引用自 documentation:
Only one backend may be specified and the configuration may not contain interpolations. Terraform will validate this.
因此,您必须对 backend
中的所有值进行硬编码,或者提供 partial configuration and fill in the rest of the configuration via CLI params using an outside tool (e.g., Terragrunt)。
后端配置有一些重要的限制:
- 一个配置只能提供一个后端块。
- 后端块不能引用命名值(如输入变量、局部变量或数据源属性)。
可以在下面看到 Terraform 后端配置 link:
https://www.terraform.io/docs/configuration/backend.html
好的,所以我有三个 .tf
文件:main.tf
我将 azure 声明为提供者,resources.tf
我的所有资源都已声明,variables.tf
.
我使用 variables.tf
来存储 resources.tf
使用的密钥。
但是,我想使用存储在我的变量文件中的变量来填充后端范围中的字段,如下所示:
main.tf
:
provider "azurerm" {
version = "=1.5.0"
}
terraform {
backend "azurerm" {
storage_account_name = "${var.sa_name}"
container_name = "${var.c_name}"
key = "${var.key}"
access_key = "${var.access_key}"
}
}
存储在 variables.tf
中的变量是这样的:
variable "sa_name" {
default = "myStorageAccount"
}
variable "c_name" {
default = "tfstate"
}
variable "key" {
default = "codelab.microsoft.tfstate"
}
variable "access_key" {
default = "weoghwoep489ug40gu ... "
}
我在 运行 terraform init
:
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".
有办法解决吗?我真的希望我所有的 keys/secrets 都在同一个文件中......而不是主文件中的一个键,我最好将其推送到 git.
Terraform 不太关心文件名:它只是加载当前目录中的所有 .tf
文件并处理它们。 main.tf
、variables.tf
和 outputs.tf
等名称是有用的约定,可让开发人员更轻松地浏览代码,但它们不会对 Terraform 的行为产生太大影响。
您看到错误的原因是您试图在 backend
配置中使用变量。不幸的是,Terraform 不允许在后端进行任何插值(任何 ${...}
)。引用自 documentation:
Only one backend may be specified and the configuration may not contain interpolations. Terraform will validate this.
因此,您必须对 backend
中的所有值进行硬编码,或者提供 partial configuration and fill in the rest of the configuration via CLI params using an outside tool (e.g., Terragrunt)。
后端配置有一些重要的限制:
- 一个配置只能提供一个后端块。
- 后端块不能引用命名值(如输入变量、局部变量或数据源属性)。
可以在下面看到 Terraform 后端配置 link: https://www.terraform.io/docs/configuration/backend.html