Terraform - 从aws实例资源输出实例ID - 一次一个

时间:2018-05-27 06:03:04

标签: amazon-ec2 interpolation terraform terraform-provider-aws

我有以下资源来创建2个实例:

`resource "aws_instance" "prod_server" {
  count                  = "${var.instance_count}"
  instance_type          = "${var.instance_type}"
  ami                    = "${data.aws_ami.server_ami.id}"
  key_name               = "${aws_key_pair.prod_auth.id}"
  vpc_security_group_ids = ["${var.vpc_security_group}"]
  subnet_id              = "${element(var.subnets, count.index)}"
  user_data              = "${data.template_file.user-init.*.rendered[count.index]}"

  tags {
    Name = "prod_server-${count.index+1}"
  }
}`

其中count的值为2。

我想使用单独的输出资源为每个实例输出实例ID,如下所示:

`output "server_id1" {
  value = "${aws_instance.prod_server.0.id}"
}

output "server_id2" {
  value = "${aws_instance.prod_server.1.id}"
}`

但是我一直收到以下错误: Resource 'aws_instance.prod_server' not found for variable 'aws_instance.prod_server.1.id'

我意识到我可以使用以下方法立即获取ID: "${aws_instance.prod_server.*.id}"

但我确实有一个特定的理由想要逐一获得它们。

有人可以建议/帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

您可以像这样使用element

<div id="left-pane">
  <div id="top-image" class="img-wrapper">
    <div class="img visible" data-imageset="1"
        style="background-image: url('http://placeimg.com/640/480/people');">
    </div>
    <div class="img" data-imageset="2"
        style="background-image: url('http://placeimg.com/640/480/animals');">
    </div>
    <div class="img" data-imageset="3"
        style="background-image: url('http://placeimg.com/640/480/any');">
    </div>
  </div>
  <div id="bottom-image" class="img-wrapper">
    <div class="img visible" data-imageset="1"
        style="background-image: url('http://placeimg.com/640/480/nature');">
    </div>
    <div class="img" data-imageset="2"
        style="background-image: url('http://placeimg.com/640/480/tech');">
    </div>
    <div class="img" data-imageset="3"
        style="background-image: url('http://placeimg.com/640/480/arch');">
    </div>
  </div>
</div>
<div id="right-pane">
  <div class="scrollable-content">Scrollable content!</div>
</div>