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 - Upgrade / downgrade ec2 type on the fly #469

Open
gregharvey opened this issue Jan 5, 2021 · 7 comments · May be fixed by #2298
Open

ec2_instance - Upgrade / downgrade ec2 type on the fly #469

gregharvey opened this issue Jan 5, 2021 · 7 comments · May be fixed by #2298
Labels
feature This issue/PR relates to a feature request has_pr jira module module plugins plugin (any type) waiting_on_contributor Needs help. Feel free to engage to get things unblocked

Comments

@gregharvey
Copy link

OP is @pkaramol - see ansible/ansible#33373 (closed by bot)

SUMMARY

Enable on-the-fly instance type change (upgrade / downgrade).

Currently when running a script that creates ec2 instance(s), there is no way to perform a type change (e.g. from t2.micro to t2.small).

ISSUE TYPE
  • Feature Idea
COMPONENT NAME

ec2_instance (as proposed in original ticket - see ansible/ansible#33373 (comment))
or potentially a new module (as suggested here - ansible/ansible#33373 (comment))

ADDITIONAL INFORMATION

If you want to use Ansible to manage infrastructure then you need to be able to up and down grade EC2 instance type as part of that. Currently we need to use the CLI, which is simple enough, but it would be more elegant of Ansible just handled this.

Here's the OP again:

Using vars from below

     my_instances:
     - instance_type: "t2.micro"
       exact_count: "1"
       assign_public_ip: "no"
     - instance_type: "t2.micro"
       exact_count: "1"
       assign_public_ip: "no"

and running the following task based on those vars:

    - name: Create my instances
      ec2:
        instance_type: "{{ item.ec2_instance_type }}"
        region: "us-east-1"
        image: "some-image"
        group_id: "some-group-id"
        vpc_subnet_id: "some-subnet-id"
        instance_tags:
      with_items: "{{ my_instances }}"

if those instances are there and running and one wants to re-run the script, having one of the instance types changed (from t2.micro --> t2.small), e.g. as follows:

 - instance_type: "t2.small"

the script runs idempotently again, although the output yields:

ok: [localhost] => (item={instance_type': u't2.small'})

(output trimmed).

OP's point being he expected the instance size to change. There's some follow-up discussion between @ryansb and @bobobox about how this might be implemented, and a plan seems to be formed, specifically:

  • User provides instance_type parameter, and if the instance does not exist it's created (as in ec2 module)
  • User provides no instance_type parameter: no changes are made if the instance exists
  • User provides an instance_type parameter that doesn't match:
    • If the instance is running, no change & the module displays a warning
    • If the instance is stopped, or desired state is stopped, then the instance is stopped& the type can be changed, relying on the user to follow up with a task to start the instance
    • If the instance is running, and a force: flag is set to true, then ec2_instance stops the instance, changes the type, and starts it back up.

And some specific concerns, which caused the suggestion the default behaviour ought not to be just blindly change instance type:

  • ...changing an instance type can require numerous other changes as well: NVMe EBS volumes used on M5/C5 instances show up with different device names, enhanced networking and EBS optimization may need to be toggled appropriately, and of course the OS may or may not even boot successfully with a different instance type, depending on things like different virtual network adapters etc.

It's this complexity which makes the original participants consider that maybe this is a separate module entirely, as instance_type is already optional for ec2_instance.

Anyway, putting this back in as it feels valid and it seems to have been dropped.

@markuman
Copy link
Member

I think all described possibilities will break backwards compatility.
A new module is not a solution and will gain entropy of the aws modules.
The only way to implement it, is to introduce a new parameter modify_instance_type which default value is False.

In case the instance_type differ from existing instance, you must differentiate between a running and stopped instance.
A stopped instance can changed immediately. A running instance must be stopped by using a waiter, changed and start again.

In case the AZ is running out of capacity of the new instance type, you got an error/problem.

@ansibullbot
Copy link

@tremble tremble transferred this issue from ansible-collections/community.aws Aug 18, 2021
@ansibullbot ansibullbot added feature This issue/PR relates to a feature request module module needs_triage plugins plugin (any type) labels Aug 18, 2021
@markuman
Copy link
Member

markuman commented Aug 18, 2021

There is a none-active PR left in community.aws that is targeting in this direction:

@goneri goneri added waiting_on_contributor Needs help. Feel free to engage to get things unblocked and removed needs_triage labels Aug 24, 2021
@goneri
Copy link
Member

goneri commented Aug 24, 2021

Hi @gregharvey,

Thank you for taking the time to reopen this. It's indeed an interesting feature idea.

@tremble
Copy link
Contributor

tremble commented Feb 6, 2023

Thanks for taking the time to open this issue.

The ec2 module was based upon the original boto SDK and was dropped in release 4.0.0 please use the ec2_instance module instead which I believe has this functionality.

@tremble tremble closed this as completed Feb 6, 2023
@pgrenaud
Copy link

@tremble Is it possible to reopen this issue? Module ec2_instance does not have this capability at the moment.

I'm currently trying to write a playbook to change the instance type, and I'm unable to do it, even with the instance being shutdown.

I know OP mentioned the module ec2 in their examples, but the they did says ec2_instance under COMPONENT NAME. This request was originally made in the ansible/ansible#33373 repository before the collection migration, so the ambiguity probably comes from that.

It would probably be better to rename this issue to something like ec2_instance: allow modifying instance_type for existing instances for better clarity.

Let me know if I can be of any help!

@tremble tremble changed the title Upgrade / downgrade ec2 type on the fly ec2_instance - Upgrade / downgrade ec2 type on the fly Feb 23, 2023
@tremble
Copy link
Contributor

tremble commented Feb 23, 2023

@pgrenaud sure. For some reason I thought ec2_instance could modify the instance type, looking deeper at the code, it just doesn't throw errors.

If you're interested in trying to add support for this feature, then it may just be a case of updating diff_instance_and_params()

However, I'm not a big fan of trying to add the "force" parameter. The logic behind this has the potential to get very messy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature This issue/PR relates to a feature request has_pr jira module module plugins plugin (any type) waiting_on_contributor Needs help. Feel free to engage to get things unblocked
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants