【发布时间】:2020-04-05 16:45:48
【问题描述】:
我是 terraform 的新手,我的要求是将多个实例启动到具有不同 user_data 的通用 ec2 模块。
我的 maif.tf 文件:
provider "aws" {
region = "${var.region}"
profile = "${var.aws_profile}" #.aws/credentials
}
variable "count" {
default = 3
}
variable "hostnames" {
default = {
"0" = "testcase_1.sh"
"1" = "testcase_2.sh"
"2" = "testcase_3.sh"
}
}
data "template_file" "user_data" {
// here we expand multiple template_files - the same number as we have instances
count = "${var.count}"
template = "wrf_testcase.sh"
vars {
// that gives us access to use count.index to do the lookup
hostname = "${lookup(var.hostnames, count.index)}"
}
}
module "ec2" {
source = "/././modules/ec2"
count = 3
user_data = "${element(data.template_file.user-data.rendered, count.index)}"
}
我的 ec2 模块:
resource "aws_instance" "application" {
count = "${var.count}"
ami = "${var.ami}"
availability_zone = "${var.availability_zone}"
ebs_optimized = "${var.ebs_optimized}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
monitoring = "${var.monitoring}"
vpc_security_group_ids = ["${var.security_group_ids}"]
subnet_id = "${var.subnet_id}"
associate_public_ip_address = "${var.associate_public_ip_address}"
iam_instance_profile = "${var.iam_instance_profile}"
user_data = "${var.user_data}"
tags = "${merge(var.tags, map("Name",
format("%s", var.instance_name)))}"
}
错误:模块“ec2”:计数变量仅在资源内有效
【问题讨论】:
-
错误:模块“ec2”:计数变量仅在资源内有效
-
您能否编辑您的问题以包括您尝试过的内容以及为什么这对您不起作用。如果它出错,则显示您的确切错误。如果它没有出错但没有给你你想要的东西,你能清楚地解释为什么它没有做你想要的吗?
-
这个我试过了。
-
它告诉你不能在模块中使用关键字
count。有一大堆重复的问题,包括前几天的这个:stackoverflow.com/questions/56241761/…