如何在 `main.tf` 中引用变量?
How can I reference a variable in `main.tf`?
我有两个地形文件 main.tf
和 var.tfvars
:
main.tf:
provider "google" {
project = var.project_id
region = var.gcp_region
}
provider "aws" {
region = var.aws_region
alias = "aws"
}
vars.tfvars:
variable "gcp_region" {
type = string
default = "asia-southeast1"
}
variable "aws_region" {
type = string
default = "ap-southeast-2"
}
variable "project_id" {
type = string
default = "test-oidc-arosha"
}
当我 运行 terraform apply
时,出现以下错误:
Error: Reference to undeclared input variable
│
│ on local.tf line 6, in locals:
│ 6: state_backet = "${local.component_name}-${local.part_name}-deployment-${var.aws_region}-${data.aws_caller_identity.current.account_id}"
│
│ An input variable with the name "aws_region" has not been declared. This variable can be declared
│ with a variable "aws_region" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on main.tf line 2, in provider "google":
│ 2: project = var.project_id
│
│ An input variable with the name "project_id" has not been declared. This variable can be declared
│ with a variable "project_id" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on main.tf line 3, in provider "google":
│ 3: region = var.gcp_region
│
│ An input variable with the name "gcp_region" has not been declared. This variable can be declared
│ with a variable "gcp_region" {} block.
即使我已经为每个变量指定了默认值,我还是不明白为什么会出现此错误。
我的 terraform 版本是:
$ terraform --version
Terraform v1.0.0
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v3.45.0
+ provider registry.terraform.io/hashicorp/google v3.72.0
您需要在 main.tf 中添加变量定义,然后在 var.tf 中适当地使用您的变量
例子
variable "aws_region" {
type = string
default = "ap-southeast-2"
}
var.tf变量
例子
aws_region = {
Name = "created by jatin/terraform",
instance_type="t2.micro"
}
将 vars.tfvars
文件的名称更改为 variables.tf
,它应该采用默认值。 .tfvars
文件用于输入变量,这些变量又定义在 variables.tf
文件(或任何 .tf
文件,实际上,不管你怎么称呼它。)
我有两个地形文件 main.tf
和 var.tfvars
:
main.tf:
provider "google" {
project = var.project_id
region = var.gcp_region
}
provider "aws" {
region = var.aws_region
alias = "aws"
}
vars.tfvars:
variable "gcp_region" {
type = string
default = "asia-southeast1"
}
variable "aws_region" {
type = string
default = "ap-southeast-2"
}
variable "project_id" {
type = string
default = "test-oidc-arosha"
}
当我 运行 terraform apply
时,出现以下错误:
Error: Reference to undeclared input variable
│
│ on local.tf line 6, in locals:
│ 6: state_backet = "${local.component_name}-${local.part_name}-deployment-${var.aws_region}-${data.aws_caller_identity.current.account_id}"
│
│ An input variable with the name "aws_region" has not been declared. This variable can be declared
│ with a variable "aws_region" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on main.tf line 2, in provider "google":
│ 2: project = var.project_id
│
│ An input variable with the name "project_id" has not been declared. This variable can be declared
│ with a variable "project_id" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on main.tf line 3, in provider "google":
│ 3: region = var.gcp_region
│
│ An input variable with the name "gcp_region" has not been declared. This variable can be declared
│ with a variable "gcp_region" {} block.
即使我已经为每个变量指定了默认值,我还是不明白为什么会出现此错误。
我的 terraform 版本是:
$ terraform --version
Terraform v1.0.0
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v3.45.0
+ provider registry.terraform.io/hashicorp/google v3.72.0
您需要在 main.tf 中添加变量定义,然后在 var.tf 中适当地使用您的变量
例子
variable "aws_region" {
type = string
default = "ap-southeast-2"
}
var.tf变量
例子
aws_region = {
Name = "created by jatin/terraform",
instance_type="t2.micro"
}
将 vars.tfvars
文件的名称更改为 variables.tf
,它应该采用默认值。 .tfvars
文件用于输入变量,这些变量又定义在 variables.tf
文件(或任何 .tf
文件,实际上,不管你怎么称呼它。)