1

I am looking AWS EC2 autoscaling feature that can automatically stop my EC2 instance at night and change my instance type (i.e: t2.xlarge to t2.small) for the whole night and in the morning again stop EC2 instance and change instance type (i.e t2.small to t2.xlarge) on daily basis.

Please note: I have already checked with IAM role > created role to grant permission for changes. & created Lambda function and insert code for stop and restart the instance with the help of AWS CloudWatch but I need code which can stop instance and then change type in a specific time for night and morning.

veben
  • 19,637
  • 14
  • 60
  • 80
  • 1
    Auto Scaling groups are designed to 'scale-out' by _adding_ instances and 'scale-in' by _removing_ instances. It is _not_ designed to change the instance type. You should scale by changing the **quantity** of instances rather than their **size**. Also, be very careful when using T-family instances with Auto Scaling groups -- if you have a Scaling Policy based on CPU Utilization, it will not work because T-family instances have limits in their CPU and they will never actually go to 100% for long periods. – John Rotenstein Nov 24 '21 at 11:35

1 Answers1

0

You can schedule CloudWatch events, that call AWS lambda functions. In the function you can change the instance type with AWS SDK ModifyInstanceAttribute API.

See below an example with boto3:

import boto3
client = boto3.client('ec2')
# ....
client.modify_instance_attribute(InstanceId=my_instance, Attribute='instanceType', Value='m3.xlarge')

See: https://stackoverflow.com/a/38671007/8718377

veben
  • 19,637
  • 14
  • 60
  • 80
  • 1
    The instance needs to be Stopped before the instance type can change. If the instance was launched by Auto Scaling, then the Auto Scaling group will probably Terminate the instance since it isn't running. So, this approach probably isn't a good idea for Auto Scaling groups. – John Rotenstein Nov 24 '21 at 11:33
  • Timely I am trying to auto stop and start but my code shows an error while testing it "Task timed out after 3.00 seconds " Do you have any idea how to fit it? – Ausaf Ulhaq Nov 24 '21 at 11:48