Feature Request: Change EC2 instance type more easily

0

I often change the EC2 instance type using the AWS CLI:

# Instance is currently running.
aws ec2 stop-instances --instance-ids i-SOMEID
# Wait up to a few minutes till the instance has shut down.
aws ec2 modify-instance-attribute --instance-id i-SOMEID --instance-type "{\"Value\": \"t3.xlarge\"}"
aws ec2 start-instances --instance-ids i-SOMEID
# Wait a few minutes
# The instance is running again.

However, it takes a while (up to a few minutes) for the instance to stop. Only when it is stopped, I can change the instance type and start it again. Otherwise, I get An error occurred (IncorrectInstanceState) when calling the ModifyInstanceAttribute operation: The instance 'i-SOMEID' is not in the 'stopped' state.

It would be great if I could stop the instance, switch the instance type, and start it again by running the 3 commands directly after the other. Then waiting a few minutes is ok, I just want the two wait times to be merged into one.

asked 3 months ago119 views
2 Answers
2
Accepted Answer

There are a few options.

Assuming your EC2 instance is being managed by Systems Manager, you can use Systems Manager AWS-ResizeInstance to change instance type. The console provides AWS CLI command to run, e.g.

aws ssm start-automation-execution --document-name "AWS-ResizeInstance" --document-version "\$DEFAULT" --parameters '{"InstanceId":["i-XXXXX"],"InstanceType":["m7g.xlarge"],"SleepWait":["PT5S"],"AutomationAssumeRole":[""]}' --region ap-southeast-1

Since you are a developer, perhaps modify your script to get EC2 InstanceState. Wait for InstanceState to change from running to stopped before you execute start-instance

 aws ec2 describe-instance-status --instance-ids i-XXXXXXXX
{
    "InstanceStatuses": [
        {
            "AvailabilityZone": "ap-southeast-1a",
            "InstanceId": "i-XXXXXXXX",
            "InstanceState": {
                "Code": 16,
                "Name": "running"
            },

Or you can use Python to do it. Code is something like below (Reference: https://medium.com/@hasnain.hakim25/change-the-instance-type-in-aws-using-python-with-custom-metrics-memory-and-lambda-function-14f9ca5c9b77)

import boto3
import time

client = boto3.client('ec2')
ec2_instance = 'i-XXXXXXXX'
    
client.stop_instances(InstanceIds=[my_instance])
waiter=client.get_waiter('instance_stopped')
waiter.wait(InstanceIds=[my_instance])
    
client.modify_instance_attribute(InstanceId=ec2_instance, Attribute='instanceType', Value='t3.large')
    
 client.start_instances(InstanceIds=[my_instance])
AWS
EXPERT
Mike_L
answered 3 months ago
profile picture
EXPERT
reviewed a month ago
0

Hello.

How about managing EC2 with CloudFormation?
By managing EC2 with CloudFormation, you can restart the instance just by changing the instance type parameters within CloudFormation.

profile picture
EXPERT
answered 3 months ago
  • The instance whose type I change regularly is a single developer machine. I want to be able to manually start it, stop it or change the instance type via terminal. Cloud Formation seems to target a different use case (managing many instances) instead.

  • I think it will be useful even if you only have one EC2. For example, in the template below, just change "t3.micro" to "t3.medium" and run the "aws cloudformation update-stack" command and EC2 will be restarted. In other words, there is no need to execute the EC2 stop and start commands.

    AWSTemplateFormatVersion: 2010-09-09
    Description: test.
    
    Resources:
      Ec2Instance:
        Type: AWS::EC2::Instance
        Properties:
          KeyName: test_key
          InstanceType: t3.micro
          ImageId: ami-yyyyyyyyyy
          BlockDeviceMappings: 
            - DeviceName: /dev/xvda
              Ebs:
                DeleteOnTermination: true
                Encrypted: true
                Iops: 3000
                VolumeSize: 20
                VolumeType: gp3
          DisableApiTermination: false
          EbsOptimized: true
          NetworkInterfaces: 
            - AssociatePublicIpAddress: true
              DeleteOnTermination: true
              DeviceIndex: 0
              GroupSet: 
                - sg-yyyyyyyyy
              SubnetId: subnet-yyyyyy
    
  • After changing the CloudFormation template, just run the following command.

    aws cloudformation update-stack --stack-name test --template-body file://test.yml
    

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions