Add support for tagging Volumes in ec2:RunInstances (#3946)

This commit is contained in:
Brian Pandola 2021-05-20 22:35:34 -07:00 committed by GitHub
parent 7f49cd0ed6
commit 16f4dc87dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -968,6 +968,7 @@ class InstanceBackend(object):
tags = kwargs.pop("tags", {})
instance_tags = tags.get("instance", {})
volume_tags = tags.get("volume", {})
for index in range(count):
kwargs["ami_launch_index"] = index
@ -996,6 +997,11 @@ class InstanceBackend(object):
)
else:
new_instance.setup_defaults()
# Tag all created volumes.
for _, device in new_instance.get_block_device_mapping:
volumes = self.describe_volumes(volume_ids=[device.volume_id])
for volume in volumes:
volume.add_tags(volume_tags)
return new_reservation

View File

@ -338,6 +338,35 @@ def test_create_with_tags():
len(instances["Instances"][0]["Tags"]).should.equal(3)
@mock_ec2
def test_create_with_volume_tags():
ec2 = boto3.client("ec2", region_name="us-west-2")
volume_tags = [
{"Key": "MY_TAG1", "Value": "MY_VALUE1"},
{"Key": "MY_TAG2", "Value": "MY_VALUE2"},
]
instances = ec2.run_instances(
ImageId=EXAMPLE_AMI_ID,
MinCount=2,
MaxCount=2,
InstanceType="t2.micro",
TagSpecifications=[{"ResourceType": "volume", "Tags": volume_tags}],
).get("Instances")
instance_ids = [i["InstanceId"] for i in instances]
instances = (
ec2.describe_instances(InstanceIds=instance_ids)
.get("Reservations")[0]
.get("Instances")
)
for instance in instances:
instance_volume = instance["BlockDeviceMappings"][0]["Ebs"]
volumes = ec2.describe_volumes(VolumeIds=[instance_volume["VolumeId"]]).get(
"Volumes"
)
for volume in volumes:
sorted(volume["Tags"], key=lambda i: i["Key"]).should.equal(volume_tags)
@mock_ec2_deprecated
def test_get_instances_filtering_by_state():
conn = boto.connect_ec2()