Amazon ec2 terraform内置函数中的变量替换

Amazon ec2 terraform内置函数中的变量替换,amazon-ec2,terraform,Amazon Ec2,Terraform,在Terraform中,我不会使用cidrhost内置函数将私有IP分配给我的实例。 我很确定我没有正确的语法,但无法找出错误所在 # Get the list of subnet ids of my VPC data "aws_subnet_ids" "org_subnet_ids" { vpc_id = "${data.aws_vpc.org_vpc.id}" } # Get the list of subnets from the ids data "aws_subnet" "o

在Terraform中,我不会使用cidrhost内置函数将私有IP分配给我的实例。 我很确定我没有正确的语法,但无法找出错误所在

# Get the list of subnet ids of my VPC
data "aws_subnet_ids" "org_subnet_ids" {
    vpc_id = "${data.aws_vpc.org_vpc.id}"
}

# Get the list of subnets from the ids
data "aws_subnet" "org_subnets" {
    count = "${length(data.aws_subnet_ids.org_subnet_ids.ids)}"
    id = "${data.aws_subnet_ids.org_subnet_ids.ids[count.index]}"
}

# Create instances and assign each of them to a different subnet
resource "aws_instance" "manager" {
  count                  = 3
  ami                    = "${data.aws_ami.ubuntu.id}"
  instance_type          = "t2.small"
  subnet_id              = "${data.aws_subnet_ids.org_subnet_ids.ids[count.index]}"
  private_ip             = "${cidrhost(${data.aws_subnet.org_subnets.cidr_block[count.index]}, count.index + 1)}"
  tags {
      Name = "manager-${count.index + 1}"
  }
}
“$terraform plan”失败,出现以下错误:

 Error: Failed to load root config module: Error loading ./provisionning/aws/ec2.tf
Error reading config for aws_instance[manager]: parse error at 1:12: expected expression but found invalid sequence "$"

你知道我这里缺少什么吗?

你不需要嵌套的
${}
。所以您应该可以使用
“${cidrhost(data.aws\u subnet.org\u subnets.cidr\u block[count.index],count.index+1)}”
谢谢。它似乎更进一步,但每个实例都有以下错误:“aws_instance.manager[1]:未找到变量“data.aws_subnet.org_subnets.cidr_block”的资源“data.aws_subnets.org_subnets”。奇怪的是,上面定义了数据。显然,我没有仔细查看,因为您的索引也位于错误的位置:
“${cidrhost(data.aws_subnet.org_subnets[count.ind‌​ex].cidr_block,count.index+1)}“
它似乎不喜欢它:读取aws_实例的配置时出错[manager]:在1:54处解析错误:预期”)“但找到了”。”。我还不太熟悉内置函数,我应该使用某种查找来获取cidr_块,还是直接使用点表示法?使用element()内置函数修复了问题:“${cidrhost(element(data.aws_subnet.org_subnets.*.cidr_block,count.index),count.index+1)}”。非常感谢你的帮助。当时我的方法有几个错误:)