简体   繁体   中英

Terraform create multiple ec2 instances in multiple subnets

I am trying to create multiple ec2 instances with access to multiple su.nets.

I've found questions and answers on doing these things individually, but not combined.

First, I create a private and a public su.net, then I setup a local to store the IDs once they are created:

locals {
  subnets =  [ aws_subnet.public_subnet.id, aws_subnet.private_subnet.id ]
}

Next, I can create a variable number of servers in the private_su.net using for_each and the below:

servers = [ "s1", "s2" ]
resource "aws_instance" "system" {
  for_each                    = var.servers 
  ami                         = var.aws_ami
  instance_type               = var.instance_type
  #subnet_id                  = aws_subnet.private_subnet.id
  count = 2
  subnet_id                   = element(local.subs, count.index)
}

What I want to have, is that the server can access both su.nets (it doesn't exist as far as I can tell, but the equivalent of su.net_ids = [aws_su.net.public_su.net.id, aws_su.net.private_su.net.id] ).

I found a nice answer which works for a specific instance by creating two NICs ( Terraform one EC2 instance with two su.nets ), however I need to do this var.servers times so it's difficult to hardcode the var.servers * 2 NICs with my current aws_instance setup (and I trip up when combining for_each and count ).

Can someone please point me in the right direction?

Two create multiple servers (in your case 4 in total) in private (two servers) and public (two servers) su.nets you can use count :

resource "aws_instance" "system" {
  count                    = length(var.servers) * length(local.subnets)
  ami                         = var.aws_ami
  instance_type               = var.instance_type
  subnet_id                   = element(local.subnets, count.index)
}

For those looking to have a similar setup there are a few steps (assuming su.nets and route tables already exist):

  • Create a machine on a single su.net
  • Create an additional.network interface
  • Attach the.network interface to the 'other' su.net (for the existing machine)

Create a variable for machines to create:

domains = [
  "asd.com",
  "asd2.com"
]

Create the machines on a single su.net:

resource "aws_instance" "domain" {
  for_each                    = var.domains
  ami                         = var.aws_ami
  subnet_id                   = aws_subnet.public_subnet.id
  associate_public_ip_address = true
  tags = {
    Name           = "Instance - ${each.key}"
  }
}

Create the additional interfaces for the 'other' su.net:

resource "aws_network_interface" "nics" {
  for_each          = var.domains
  subnet_id         = aws_subnet.private_subnet.id
  tags = {
    Name           = "NIC - ${each.key}"
  }
}

Attach the.network interfaces to the 'other' su.net (for the existing machine):

resource "aws_network_interface_attachment" "attach_nics" {
  for_each              = var.domains
  instance_id           = aws_instance.domain[each.key].id
  network_interface_id  = aws_network_interface.nics[each.key].id
  device_index          = 1 # public_subnet = 0
}

The 'trick' here (that I didn't know) is understanding that you can access data from created resources based on their names in the existing script (which is used in the aws.network_interface_attachment component).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM