How to Resize an EC2 Instance on AWS Easily
To resize an EC2 instance, first stop the instance, then change its
InstanceType to the desired size in the AWS Management Console or CLI, and finally start the instance again. This process updates the instance's hardware resources like CPU and memory without losing data on the attached volumes.Syntax
To resize an EC2 instance using AWS CLI, use the modify-instance-attribute command with the --instance-type option. The instance must be stopped before resizing.
--instance-id: The ID of the EC2 instance to resize.--instance-type: The new instance type (e.g.,t3.medium).
bash
aws ec2 stop-instances --instance-ids i-1234567890abcdef0 aws ec2 modify-instance-attribute --instance-id i-1234567890abcdef0 --instance-type "{\"Value\":\"t3.medium\"}" aws ec2 start-instances --instance-ids i-1234567890abcdef0
Example
This example shows how to resize an EC2 instance from t2.micro to t3.medium using AWS CLI. The instance is stopped first, resized, then started again.
bash
aws ec2 stop-instances --instance-ids i-0abcdef1234567890 aws ec2 wait instance-stopped --instance-ids i-0abcdef1234567890 aws ec2 modify-instance-attribute --instance-id i-0abcdef1234567890 --instance-type "{\"Value\":\"t3.medium\"}" aws ec2 start-instances --instance-ids i-0abcdef1234567890
Output
Stopped instance: i-0abcdef1234567890
Instance stopped.
Modified instance type to t3.medium.
Started instance: i-0abcdef1234567890
Common Pitfalls
- Trying to change the instance type while the instance is running will fail; always stop the instance first.
- Not all instance types are available in every AWS region or compatible with your instance's AMI or EBS volume.
- Failing to check instance limits or account quotas can block resizing.
- Remember to verify your application compatibility with the new instance type.
bash
aws ec2 modify-instance-attribute --instance-id i-0abcdef1234567890 --instance-type "{\"Value\":\"t3.medium\"}" # This will fail if the instance is running. # Correct approach: aws ec2 stop-instances --instance-ids i-0abcdef1234567890 aws ec2 wait instance-stopped --instance-ids i-0abcdef1234567890 aws ec2 modify-instance-attribute --instance-id i-0abcdef1234567890 --instance-type "{\"Value\":\"t3.medium\"}" aws ec2 start-instances --instance-ids i-0abcdef1234567890
Quick Reference
Remember these steps to resize your EC2 instance:
- Stop the instance.
- Modify the instance type.
- Start the instance again.
- Check compatibility and limits before resizing.
Key Takeaways
Always stop your EC2 instance before changing its instance type.
Use the AWS CLI or Console to modify the instance type safely.
Verify the new instance type is available and compatible with your setup.
Restart the instance after resizing to apply changes.
Check AWS limits and application compatibility before resizing.