Moto: Instance.modify_attribute() not working for blockDeviceMapping

Created on 12 Oct 2017  路  5Comments  路  Source: spulec/moto

Reporting Bugs

I am trying to set the DeleteOnTermination attribute of a volume attached to an instance to True using the boto3 method EC2.Instance.modify_attribute(), but it doesn't seem to be doing the trick.

I tried using the procedure described in this link. I also tried using EC2.Client.modify_instance_attribute(), and neither option worked. No errors at all, just that the attribute itself wasn't updated.

I've wrote the following python snippet to reproduce this issue:

from moto import mock_ec2
import boto3
from pprint import pprint


@mock_ec2
def main():
    ec2_client = boto3.resource('ec2', region_name='us-west-1')
    result = ec2_client.create_instances(ImageId='ami-12345678', MinCount=1, MaxCount=1)
    instance = result[0]
    instance.load()
    pprint(instance.block_device_mappings)
    instance.modify_attribute('blockDeviceMapping', {'/dev/sda1': True})
    instance.load()
    pprint(instance.block_device_mappings)


if __name__ == "__main__":
    main()

Which when executed, should set the DeleteOnTermination attribute of the attached EBS volume to True. But this is not the case, as per this bash output:

root@ip-10-132-17-214:~# ./a.py
[{'DeviceName': '/dev/sda1',
  'Ebs': {'AttachTime': datetime.datetime(2017, 10, 12, 3, 24, 23, tzinfo=tzlocal()),
          'DeleteOnTermination': False,
          'Status': 'in-use',
          'VolumeId': 'vol-8d707b2a'}}]
[{'DeviceName': '/dev/sda1',
  'Ebs': {'AttachTime': datetime.datetime(2017, 10, 12, 3, 24, 23, tzinfo=tzlocal()),
          'DeleteOnTermination': False,
          'Status': 'in-use',
          'VolumeId': 'vol-8d707b2a'}}]
root@ip-10-132-17-214:~#

I'm using Moto 1.1.22 and Boto3 1.4.7, which I installed using pip install <packagename> on a freshly launched Ubuntu 16.04.03 instance running on AWS.

Most helpful comment

Fixed on master, will release in the next week sometime.

All 5 comments

Have pretty much solved this, technically moto was setting terminate to true but the response it was giving to boto was being interpreted as false.

It might be worth mentioning that the original parameters for modify_attribute were incorrect also:

instance.modify_attribute('blockDeviceMapping', {'/dev/sda1': True})

I was debugging this and noticed that the parameters were not even getting to the API. The doc in the link supplied is misleading in that the parameters are correct for mock_ec2_deprecated, not mock_ec2. @terrycain would it be worth quickly updating that doc to reflect this in your PR?

Yeah done.

Fixed on master, will release in the next week sometime.

@JackDanger this is released in 1.1.23, can now be closed.

Was this page helpful?
0 / 5 - 0 ratings