Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ec2_instance - Support throughtput parameter for GP3 volume types #433

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/433-ec2_instance-throughput.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_instance - add ``throughput`` parameter for gp3 volume types (https://github.com/ansible-collections/amazon.aws/pull/433).
10 changes: 10 additions & 0 deletions plugins/modules/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
- A list of block device mappings, by default this will always use the AMI root device so the volumes option is primarily for adding more storage.
- A mapping contains the (optional) keys device_name, virtual_name, ebs.volume_type, ebs.volume_size, ebs.kms_key_id,
ebs.iops, and ebs.delete_on_termination.
- Set ebs.throughput value requires botocore>=1.19.27.
- For more information about each parameter, see U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_BlockDeviceMapping.html).
type: list
elements: dict
Expand Down Expand Up @@ -922,6 +923,15 @@ def build_volume_spec(params):
for int_value in ['volume_size', 'iops']:
if int_value in volume['ebs']:
volume['ebs'][int_value] = int(volume['ebs'][int_value])
if 'volume_type' in volume['ebs'] and volume['ebs']['volume_type'] == 'gp3':
if not volume['ebs'].get('iops'):
volume['ebs']['iops'] = 3000
if 'throughput' in volume['ebs']:
tremble marked this conversation as resolved.
Show resolved Hide resolved
module.require_botocore_at_least('1.19.27', reason='to set throughtput value')
volume['ebs']['throughput'] = int(volume['ebs']['throughput'])
Copy link
Member

@markuman markuman Aug 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe set throughput also to its default value 125 if not set?
what happens when the instance exist already? will it keeps the instance values (in case they were changed manually) or will it overwrite them (reset them)? I've no idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @markuman, Thank you for reviewing. I set the default value for throughput.

If my understanding is correct, you can specify different parameters for ebs like throughput and iops only when a new instance is created. In the case an EC2 instance is already present and you would like to modify throughput or iops or both you won’t be allowed because the API allows to modify only a specific subset of BlockDeviceMappings as shows here https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.modify_instance_attribute
I tried by myself to only change these params (throughput for example) and the task resulted in unchanged reflecting the documentation. Therefore, if you want to change throughput or iops values you would need to use the ec2_vol module.

else:
volume['ebs']['throughput'] = 125

return [snake_dict_to_camel_dict(v, capitalize_first=True) for v in volumes]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@
ec2_instance:
state: absent
instance_ids: "{{ block_device_instances.instance_ids }}"

- name: "New instance with an extra block device - gp3 volume_type and throughput"
ec2_instance:
state: present
name: "{{ resource_prefix }}-test-ebs-vols-gp3"
image_id: "{{ ec2_ami_image }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
volumes:
- device_name: /dev/sdb
ebs:
volume_size: 20
delete_on_termination: true
volume_type: gp3
throughput: 500
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
instance_type: "{{ ec2_instance_type }}"
wait: true
register: block_device_instances_gp3

- assert:
that:
- block_device_instances_gp3 is not failed
- block_device_instances_gp3 is changed
- block_device_instances_gp3.spec.BlockDeviceMappings[0].DeviceName == '/dev/sdb'
- block_device_instances_gp3.spec.BlockDeviceMappings[0].Ebs.VolumeType == 'gp3'
- block_device_instances_gp3.spec.BlockDeviceMappings[0].Ebs.VolumeSize == 20
- block_device_instances_gp3.spec.BlockDeviceMappings[0].Ebs.Throughput == 500

always:
- name: "Terminate block_devices instances"
Expand Down