Duplicate "choose best AZ" when using non-default subnets

0

Up until, our networking usage in EC2 has been pretty vanilla. In our primary region, we have one VPC, with default subnets in all of the AZs. When we run an instance in ec2, we just leave subnet blank. This causes AWS to put our new instance in, what I assumed, was the "best" AZ, which I guessed was the least-busy one at the time. Or recently known as least-busy. Whatever...in other words, we left it up to EC2 to decide which subnet/AZ to put the instance in.

But now, we want to save money on IPv4 rental. We have some work that can be done in IPv6 only subnets. So we've created new subnets in the VPC that are IPv6 only. But, obviously, they can't be the default subnets in this VPC. So, to run an instance in one of these IPv6 only subnets, we have to specify the subnet. Which means we have to pick the AZ.

Is there any way to duplicate the functionality that EC2 provides when you don't specify a subnet and it chooses one for you? In other words, is there an API call that I can make that would tell me what the current preferred AZ in a region would be for running new instances?

Kevin
asked a month ago656 views
1 Answer
0

Hello,

When we choose a subnet where to place our EC2 instance, by default, AWS will provide us with a random AZ.

However, when we now create custom subnets that support (IPv6 or IPv4), we have to place these subnets in an AZ as well. If you have now created these subnets in the default VPC, AWS will automatically only choose the default (so the one that AWS created), as it states when we create an EC2 instance.

If we create our own VPC, we will define the subnet where we place the EC2 instance. It doesn't matter if we have IPv4 or IPv6; AWS will always choose the default subnet. However, a possible solution is that we can add IPv6 CIDR to these default subnets.

**There is no direct API call to the problem ** ; however, using the CLI, we can write a command to launch the EC2 instance; when launching the instance, we also have to specify the subnet-id, which can be randomly generated.

aws ec2 run-instances
--image-id ami-1234567890abcdef0
--instance-type t2.micro
--subnet-id subnet-01234567abcdef01

Of course, this does not only have to be written in the CLI. A very easy way of doing this is by defining a CDK (Cloud Development Kit) program. Some basic programming language is required; however, within some lines of code, we can easily accomplish that.

profile picture
Julian
answered a month ago
profile picture
EXPERT
reviewed a month ago
  • I was under the impression that EC2 actually did more than just randomly choose one of the default subnets. I thought it actually did a bit of load balancing to choose which subnet/AZ might better be able to handle the request.

    I'm not sure where I got that idea.

    If I made that up in my head then yeah, we can just randomly pick them as well.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions