我需要使用 Terraform 在同一区域内的 3 个可用区内部署 3 个 EC2 实例。我试图使用以下结构:
variable "AVZ" {
type = "map"
default = {
eu-central-1 = ["eu-central-1a","eu-central-1b","eu-central-1c"]
eu-west-1 = ["eu-west-1a","eu-west-1b","eu-west-1c"]
}
}
和
resource "aws_instance" "web-server0" {
count = "${var.AWS_COUNT_WEB}"
availability_zone = "${element(split(",", var.AVZ), count.index)}"
ami = "${lookup(var.AWS_AMI,var.AWS_REGION)}"
key_name = "${var.AWS_KEY_NAME}"
instance_type = "${var.AWS_INSTANCE_TYPE}"
associate_public_ip_address = "true"
vpc_security_group_ids = [
"${aws_security_group.allow_ssh.id}",
"${aws_security_group.allow_web.id}"
]
tags {
Name = "web-server-b${count.index}"
Tier = "blue"
Role = "Web_Server"
}
然而,它并没有太大帮助。它根本不起作用我收到以下错误:
At column 11, line 1: split: argument 2 should be type string, got type
map in:
有谁知道我如何使用变量类型映射,而不是字符串,以便我可以将 AZ 映射到区域并根据定义的区域查找可用的 AZ,并根据该结果部署 EC2 实例?
谢谢你,亚历克斯
您将 AVZ 定义为地图,但您不使用密钥来访问它。A
split(",", ...)
通常用于带有字符串值而不是列表的旧代码中。所以试试这个插值: