From 756f772c5ce86f3ee92f1376d4405022d2a567dc Mon Sep 17 00:00:00 2001 From: Dominik Schubert Date: Thu, 11 Nov 2021 13:50:07 +0100 Subject: [PATCH] EC2 - fix missing AZ on auto-generated volumes for instances (#4560) --- moto/ec2/models.py | 4 ++-- tests/test_ec2/test_elastic_block_store.py | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index f80a02e03..1cdce36cf 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -796,7 +796,7 @@ class Instance(TaggedEC2Resource, BotoInstance, CloudFormationModel): ): volume = self.ec2_backend.create_volume( size=size, - zone_name=self.region_name, + zone_name=self._placement.zone, snapshot_id=snapshot_id, encrypted=encrypted, kms_key_id=kms_key_id, @@ -808,7 +808,7 @@ class Instance(TaggedEC2Resource, BotoInstance, CloudFormationModel): def setup_defaults(self): # Default have an instance with root volume should you not wish to # override with attach volume cmd. - volume = self.ec2_backend.create_volume(size=8, zone_name="us-east-1a") + volume = self.ec2_backend.create_volume(size=8, zone_name=self._placement.zone) self.ec2_backend.attach_volume(volume.id, self.id, "/dev/sda1", True) def teardown_defaults(self): diff --git a/tests/test_ec2/test_elastic_block_store.py b/tests/test_ec2/test_elastic_block_store.py index 926722bbb..8ffee9d04 100644 --- a/tests/test_ec2/test_elastic_block_store.py +++ b/tests/test_ec2/test_elastic_block_store.py @@ -561,6 +561,13 @@ def test_volume_attach_and_detach_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") reservation = client.run_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1) instance = reservation["Instances"][0] + + volumes = client.describe_volumes( + Filters=[{"Name": "attachment.instance-id", "Values": [instance["InstanceId"]]}] + )["Volumes"] + volumes.should.have.length_of(1) + volumes[0]["AvailabilityZone"].should.equal("us-east-1a") + volume = ec2.create_volume(Size=80, AvailabilityZone="us-east-1a") volume.reload()