0

We have a custom VPC with public and private subnets, in several Availbility Zone (AZ). Via, AWS CLI, we would like to launch/run ec2 instances in that VPC, private subnets, spread out to different AZ, thus spread out among the private subnet (as a subnet can only be in a single AZ)

From thhis doc, when launching without subnet: the cli will choose the default VPC and a subnet for you. But I cannot see how to specify a VPC rather than using the default one.

Is there a convenient solution that is not manually specifying subnet (and managing ourself the spread out) ?

This is actually a bit similar to this question: Boto3 run_instances: How to specify more than one Subnet?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
mhtrinh
  • 21
  • 2

1 Answers1

1

"Convenient Solution"? No.

Since your goal is to "spread out" instances among the private subnets, you could write a script that:

  • Retrieves a list of subnets
  • Retrieves a list of currently-running Amazon EC2 instances
  • Determines which subnet has the least number of running instances
  • Launches an instance in that subnet

Alternatively, you could launch the instances in an EC2 Auto Scaling group because Auto Scaling always tries to balance instances across Availability Zones. If you have one subnet per AZ, then Auto Scaling will always keep the instances balanced across AZs.

However, if you use an Auto Scaling group, then each instance would need to be identical (as defined in the EC2 Launch Template) and you would add/remove instances by asking Auto Scaling to change the Desired Capacity rather than directly launching an instance.

See: What is Amazon EC2 Auto Scaling?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470