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
已提問 1 個月前檢視次數 666 次
1 個回答
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
已回答 1 個月前
profile picture
專家
已審閱 1 個月前
  • 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.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南