diff --git a/changelogs/fragments/1890-ec2-instance-change-instance-type.yml b/changelogs/fragments/1890-ec2-instance-change-instance-type.yml new file mode 100644 index 00000000000..95ff7561103 --- /dev/null +++ b/changelogs/fragments/1890-ec2-instance-change-instance-type.yml @@ -0,0 +1,2 @@ +minor_changes: + - modules/ec2_instance - add the ability to change the ``instance-type`` (https://github.com/ansible-collections/amazon.aws/pull/1890). \ No newline at end of file diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index e9e9efaf145..b7d4f0edf53 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -55,6 +55,7 @@ - Only required when instance is not already present. - At least one of I(instance_type) or I(launch_template) must be specificed when launching an instance. + - To change the I(instance_type), the instance must be in I(state=stopped). type: str count: description: @@ -1563,6 +1564,7 @@ def value_wrapper(v): param_mappings = [ ParamMapper("ebs_optimized", "EbsOptimized", "ebsOptimized", value_wrapper), ParamMapper("termination_protection", "DisableApiTermination", "disableApiTermination", value_wrapper), + ParamMapper('instance_type', 'InstanceType', 'instanceType', value_wrapper), # user data is an immutable property # ParamMapper('user_data', 'UserData', 'userData', value_wrapper), ] diff --git a/tests/integration/targets/ec2_instance_cpu_options/tasks/main.yml b/tests/integration/targets/ec2_instance_cpu_options/tasks/main.yml index 4aea79c047a..71a41ccbf7c 100644 --- a/tests/integration/targets/ec2_instance_cpu_options/tasks/main.yml +++ b/tests/integration/targets/ec2_instance_cpu_options/tasks/main.yml @@ -82,4 +82,61 @@ - name: "Confirm existence of instance id." assert: that: - - "{{ checkmode_instance_fact.instances | length }} == 0" + - checkmode_instance_fact.instances | length == 0 + + - name: "create t3.nano instance" + ec2_instance: + state: running + name: "{{ resource_prefix }}-test-t3nano-latest-t3micro" + image_id: "{{ ec2_ami_id }}" + tags: + TestId: "{{ ec2_instance_tag_TestId }}" + vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" + instance_type: t3.nano + wait: true + + - name: "fact ec2 instance" + ec2_instance_info: + filters: + "tag:Name": "{{ resource_prefix }}-test-t3nano-latest-t3micro" + register: checkmode_instance_fact + + - name: "Confirm existence of instance id." + assert: + that: + - checkmode_instance_fact.instances | length == 1 + + - name: "stopp instance" + ec2_instance: + state: stopped + name: "{{ resource_prefix }}-test-t3nano-latest-t3micro" + image_id: "{{ ec2_ami_id }}" + tags: + TestId: "{{ ec2_instance_tag_TestId }}" + vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" + instance_type: t3.nano + wait: true + register: stopped_instance + + - name: "Confirm instance is stopped" + assert: + that: + - stopped_instance.msg == 'Instances stopped' + + - name: "start instance as t3.micro" + ec2_instance: + state: stopped + name: "{{ resource_prefix }}-test-t3nano-latest-t3micro" + image_id: "{{ ec2_ami_id }}" + tags: + TestId: "{{ ec2_instance_tag_TestId }}" + vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" + instance_type: t3.micro + wait: true + register: micro_instance + + - name: "Confirm new instance type" + assert: + that: + - micro_instance.instances.0.instance_type == 't3.micro' + - micro_instance.changes[0].InstanceType.Value == 't3.micro' \ No newline at end of file