Search code examples
amazon-web-servicesamazon-ec2boto

AWS Python SDK - guarantee that EBS volume and EC instance are in the same availability zone


I am scripting the setup of AWS resources using the Python SDK (boto3). I can create ec2 instances with:

ec2_resource.create_instances( .... )

and a block storage volume with:

ec2_client.create_volume(..., AvailabilityZone = "eu-north-1a")

As indicated I can specify the availability zone for the block storage, however I have not understood how to specify that for the ec2 instance; when looking in the console I generally find that the ec2 instance has been created in another availability zone - e.g. "eu-north-1c", and then instance.attach_volume() fails because they are not in the same availability zone.

What is the correct way to approach this?


Solution

  • I hope you're using boto3 and not the outdated boto library ;-)

    By specifying the subnet ID to launch the instance into, you implicitly set the availability zone, because each subnet is located in exactly one availability zone.