[英]terraform S3 module, can't call the KMS key arn calling module
[英]Not able to fetch the variable's value defined in a module in Terraform
我是 terraform 的新手...我在模塊內創建了一個 dev.tfvars 文件並在其中聲明了一些變量。 我正在嘗試獲取此值並將其分配給不同模塊中的另一個變量。 這是行不通的。
下面是代碼片段..
module "mydemomodule" {
source = "../modules/dev_env" // module call
}
resource "aws_instance" "demo" {
ami = "${module.mydemomodule.AMIS.LINUX_MUMBAI}" // trying to fetch the value
region = "${module.mydemomodule.AWS_MUMBAI_REGION}" // trying to fetch the value
instance_type = "${module.mydemomodule.INSTANCE_TYPE}" // trying to fetch the value
key_name = "${aws_key_pair.ssshkey.key_name}"
tags = {
Name = "${terraform.workspace}_server"
}
}
dev.tfvars // this file is inside other module
-----------------------------------------------
variable "INSTANCE_TYPE"{
default = "t2.micro"
}
//AWS_REGION = "us-east-1"
variable "AWS_MUMBAI_REGION"{
default = "ap-south-1"
}
variable "AWS_SINGAPORE_REGION"{
default = "us-southeast-1"
}
variable "AMIS" {
type = map
default = {
LINUX_MUMBAI = "ami-0cca134ec43cf708f"
UBUNTU_MUMBAI = "ami-07ffb2f4d65357b42"
LINUX_SGP = "ami-005835d578c62050d"
UBUNTU_SGP = "ami-02045ebddb047018b"
}
}
運行 terraform 計划時出現如下錯誤
Error: Unsupported attribute
│
│ on main.tf line 16, in resource "aws_instance" "demo":
│ 16: ami = "${module.mydemomodule.AMIS.LINUX_MUMBAI}"
│ ├────────────────
│ │ module.mydemomodule is a object
│
│ This object does not have an attribute named "AMIS".
╵
╷
│ Error: Unsupported argument
│
│ on main.tf line 17, in resource "aws_instance" "demo":
│ ├────────────────
│ │ module.mydemomodule is a object
│
│ This object does not have an attribute named "INSTANCE_TYPE".
╵
模塊中的變量僅限於該模塊,不能在該模塊外訪問。 如果您在多個模塊中需要該值,為什么不在頂層定義它,並將它傳遞給所有需要它的模塊呢?
如果您絕對需要在模塊內定義值,那么您需要向該模塊添加一個output
以輸出該值,以便您可以在頂層訪問它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.