使用 (count, for_each) Terraform 创建多个 ec2 aws 实例并附加具有特定 IP 的多个网络接口

扎曼别克·马克苏别科夫

我无法使用附加的多个网络接口来实现创建多个实例。我有一个代码,其中创建了三个实例并附加了 3 个网络接口。我想使用 count, for_each 来做到这一点。我在 Internet 上找不到实现的方法。我请你帮我如何使用 count 实现代码

resource "aws_network_interface" "private_elasticsearch" {
subnet_id       = aws_subnet.private_subnets.id


private_ips     = ["10.245.10.6"]
  security_groups = [aws_security_group.elastic_traffic.id, 
  aws_security_group.general.id]
  tags = {
    Environment = "Test"
    Project = "test"
    Name = "elasticsearch-interface"
  }
}


resource "aws_network_interface" "private_elasticsearch2" {
  subnet_id       = aws_subnet.private_subnets.id
  private_ips     = ["10.245.10.7"]
  security_groups = [aws_security_group.elastic_traffic.id, aws_security_group.general.id]
  tags = {
    Environment = "Test"
    Project = "test"
    Name = "elasticsearch-interface2"
  }
}


resource "aws_network_interface" "private_elasticsearch3" {
  subnet_id       = aws_subnet.private_subnets.id
  private_ips     = ["10.245.10.8"]
  security_groups = [aws_security_group.elastic_traffic.id, aws_security_group.general.id]
  tags = {
    Environment = "Test"
    Project = "test"
    Name = "elasticsearch-interface3"
  }
}

resource "aws_instance" "dev-elasticsearch" {
  ami = "ami-08bdc08970fcbd34a"
  availability_zone = "eu-north-1a"
  instance_type =  "t3.micro"
  network_interface {
     device_index=0
     network_interface_id = aws_network_interface.private_elasticsearch.id
  }
  tags = {
    Environment = "Test"
    Project = "test"
    Name = "elasticsearch-instance-dev"
  }
}

resource "aws_instance" "dev-elasticsearch-2" {
  ami = "ami-08bdc08970fcbd34a"
  availability_zone = "eu-north-1a"
  instance_type = "t3.micro"
  network_interface {
     device_index=0
     network_interface_id = aws_network_interface.private_elasticsearch2.id
  }
  tags = {
    Environment = "Test"
    Project = "test"
    Name = "elasticsearch-instance-dev-2"
  }
}

resource "aws_instance" "dev-elasticsearch-3" {
  ami = "ami-08bdc08970fcbd34a"
  availability_zone = "eu-north-1a"
  instance_type = "t3.micro"
  network_interface {
     device_index=0
     network_interface_id = aws_network_interface.private_elasticsearch3.id
}
  tags = {
    Environment = "Test"
    Project = "test"
    Name = "elasticsearch-instance-dev-3"
  }
马尔钦

您可以使用countfor_each也可以):

locals {
    ips = ["10.245.10.6", "10.245.10.7", "10.245.10.8"]
}



resource "aws_network_interface" "private_elasticsearch" {

  count           = length(local.ips)

  subnet_id       = aws_subnet.private_subnets.id
  private_ips     = [local.ips[count.index]]
  security_groups = [aws_security_group.elastic_traffic.id, 
                     aws_security_group.general.id]
  tags = {
    Environment = "Test"
    Project = "test"
    Name = "elasticsearch-interface${count.index}"
  }
}


resource "aws_instance" "dev-elasticsearch" {

  count           = length(local.ips)  

  ami = "ami-08bdc08970fcbd34a"
  availability_zone = "eu-north-1a"
  instance_type =  "t3.micro"
  network_interface {
     device_index=0
     network_interface_id = aws_network_interface.private_elasticsearch[count.index].id
  }
  tags = {
    Environment = "Test"
    Project = "test"
    Name = "elasticsearch-instance-dev${count.index}"
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章