ansible-collections / amazon.aws

Ansible Collection for Amazon AWS
GNU General Public License v3.0
309 stars 341 forks source link

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

Closed gregharvey closed 1 month ago

gregharvey commented 3 years ago

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
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:

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

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 commented 3 years ago

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 commented 3 years ago

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

markuman commented 3 years ago

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

goneri commented 3 years ago

Hi @gregharvey,

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

tremble commented 1 year ago

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.

pgrenaud commented 1 year ago

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

tremble commented 1 year ago

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