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

[WIP] ec2_instance: ability to change the instance type #1890

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -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).
2 changes: 2 additions & 0 deletions plugins/modules/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

In addition to not matching the name of the task I suspect this is the cause of the CI failure. state: stopped probably introduces a race condition: since the instance is already in the stopped state there's no actual waiting and the change may not yet be reflected in the fetched instance description.

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'
Loading