【问题标题】:Set network_interface for aws_instance if count.index=0 using terraform如果 count.index=0 使用 terraform 为 aws_instance 设置 network_interface
【发布时间】:2018-09-09 22:04:35
【问题描述】:

我正在使用 terraform 创建 2 个 ec2 实例,我想为 terraform 创建的第一个实例提供辅助 IP 地址。

我正在使用下面的代码块

resource "aws_network_interface" "floating_private" {
  subnet_id       = "${var.subnet_cluster_one}"
  private_ips_count = 2
}

resource "aws_instance" "instance_attrix_cluster_one" {
    count = 2
    instance_type = "${var.aws_instance_type}"
    ami = "${var.attrix_ami}"
    subnet_id = "${var.subnet_cluster_one}"
    security_groups = "${var.aws_security_groups}"
    key_name = "${var.ssh_key}"
    tags = "${merge(var.default_tags, map("Name", "${format("attrix%02d", 
count.index + 1)}-${var.env_name}"))}"
}

我尝试在“aws_instance”块中添加以下代码

network_interface = "${floating_private.id ? count.index == 0 : count.index >= 0}"

但是,我看到以下错误 -

 Error reading config for aws_instance[instance_attrix_cluster_one]: floating_private.id: resource variables must be three parts: TYPE.NAME.ATTR in:

 ${floating_private.id ? count.index == 0 : count.index >= 0}

如果 count == 0,我如何设置 network_interface 属性?

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案1】:

    我没有创建两个自定义 ENI 并将它们附加到第一个实例,而是稍微更改了方法,以便使用实例创建默认 ENI,并且我们在创建实例后将附加 ENI 附加到第一个实例。

    允许 Terraform 为实例创建默认 ENI,然后为第一个实例创建附件。见下文。

    resource "aws_network_interface" "floating_private" {
      subnet_id = "${data.aws_subnet.example.id}"
    }
    
    resource "aws_instance" "instance_attrix_cluster_one" {
      count         = 2
      instance_type = "t2.micro"
      subnet_id     = "${data.aws_subnet.example.id}"
      ami           = "${data.aws_ami.ubuntu.id}"
    
      tags {
          Name = "test-${count.index}"
      }
    }
    
    resource "aws_network_interface_attachment" "test" {
      instance_id          = "${element(aws_instance.instance_attrix_cluster_one.*.id, count.index)}"
      network_interface_id = "${element(aws_network_interface.floating_private.*.id, count.index)}"
      device_index         = "${count.index + 1}"
    }
    

    稍作修改以在我的机器上进行一些测试,但希望您能理解并应用到您的解决方案中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 2021-08-06
      • 2021-07-16
      • 2019-01-23
      • 2020-01-16
      • 2019-06-28
      • 2023-01-14
      相关资源
      最近更新 更多