简体   繁体   English

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

[英]Creating multiple ec2 aws instances and attaching multiple networking interfaces with specific IP using (count, for_each) Terraform

I am unable to implement the creation of multiple instances with a attached multiple network interface.我无法使用附加的多个网络接口来实现创建多个实例。 I have a code where three instances are created and 3 network interfaces are attached.我有一个代码,其中创建了三个实例并附加了 3 个网络接口。 I would like to do this using count, for_each.我想使用 count, for_each 来做到这一点。 I could no5 find implemented methods on the Internet.我在 Internet 上找不到实现的方法。 I ask you to help me how to implement the code using count我请您帮助我如何使用 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"
  }

You can do it with count ( for_each also possible):您可以使用count来完成(也可以使用for_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}"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Terraform 在多个子网中创建 EC2 实例 - Creating EC2 instances in multiple subnets with Terraform using for each 使用每个 terraform 在单独的 AZ 中创建不同的 EC2 实例 - Creating different EC2 instances in separate AZs using for each terraform 使用 for_each 在 Terraform 中创建多个别名记录 - Creating multiple Alias records in Terraform with for_each 如何使用 Terraform 从单个 VPC 启动多个 AWS EC2 实例? - How to launch multiple AWS EC2 instances from a single VPC using Terraform? 使用计数在 terraform 中定义多个 ec2 实例并将它们注册到负载均衡器 - Defining multiple ec2 instances in terraform using count and registering them with a load balancer 使用计数与 terraform 在多个子网中启动 EC2 服务器 - Launch EC2 servers in multiple subnets using count with terraform 在 Terraform 中获取多个 ec2 实例(标签名称和私有 ip)的 output - getting output of multiple ec2 instances (tag name and Private ip) in Terraform AWS CloudWatch向多个EC2实例发出警报 - AWS CloudWatch Alarms to multiple EC2 instances 多个EC2实例中的AWS ECS - AWS ECS in multiple EC2 Instances AWS EC2实例:启动多个实例 - AWS EC2 Instances: Launch Multiple Instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM