amazon.aws icon indicating copy to clipboard operation
amazon.aws copied to clipboard

ec2_instance - Upgrade / downgrade ec2 type on the fly

Open gregharvey opened this issue 3 years ago • 7 comments

OP is @pkaramol - see https://github.com/ansible/ansible/issues/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 https://github.com/ansible/ansible/issues/33373#issuecomment-350321979) or potentially a new module (as suggested here - https://github.com/ansible/ansible/issues/33373#issuecomment-381685354)

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.

gregharvey avatar Jan 05 '21 16:01 gregharvey

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.

markuman avatar Jan 11 '21 20:01 markuman

cc @Shaps @jillr @s-hertel @tremble @wimnat click here for bot help

ansibullbot avatar Jan 13 '21 00:01 ansibullbot

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

  • PR https://github.com/ansible-collections/community.aws/pull/461
  • comment related to this issue: https://github.com/ansible-collections/community.aws/pull/461#issuecomment-803266114

markuman avatar Aug 18 '21 11:08 markuman

Hi @gregharvey,

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

goneri avatar Aug 24 '21 19:08 goneri

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 avatar Feb 06 '23 08:02 tremble

@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 https://github.com/ansible/ansible/issues/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!

pgrenaud avatar Feb 22 '23 18:02 pgrenaud

@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.

tremble avatar Feb 23 '23 11:02 tremble