How to Resize an EC2 Instance: Step-by-Step Guide for Changing Instance Types with EBS


2 views

Amazon EC2 allows you to change your instance type through a process called instance resizing. Unlike some cloud providers that offer in-place resizing, AWS requires you to stop the instance (for EBS-backed instances) before modifying the instance type.

  • Your instance must be EBS-backed (not instance-store)
  • You need appropriate IAM permissions
  • The desired instance type must be available in your Availability Zone

Here's how to change your instance type from t2.micro to t2.large:

# Using AWS CLI
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
aws ec2 modify-instance-attribute --instance-id i-1234567890abcdef0 --instance-type "t2.large"
aws ec2 start-instances --instance-ids i-1234567890abcdef0

Downtime: The instance must be stopped, which means your application will experience downtime during this process.

Compatibility: Not all instance types can be resized to all other types. Check AWS documentation for compatible families.

For frequent resizing operations, consider using AWS Lambda:

import boto3

def lambda_handler(event, context):
    ec2 = boto3.client('ec2')
    
    # Stop instance
    ec2.stop_instances(InstanceIds=[event['instance_id']])
    
    # Wait for instance to stop
    waiter = ec2.get_waiter('instance_stopped')
    waiter.wait(InstanceIds=[event['instance_id']])
    
    # Change instance type
    ec2.modify_instance_attribute(
        InstanceId=event['instance_id'],
        InstanceType={'Value': event['new_instance_type']}
    )
    
    # Start instance
    ec2.start_instances(InstanceIds=[event['instance_id']])

For production environments, consider launching a new instance with the desired size and switching traffic:

  1. Create AMI of current instance
  2. Launch new instance from AMI with desired type
  3. Update DNS or load balancer to point to new instance
  4. Terminate old instance after verification

Resizing an Amazon EC2 instance (often referred to as "instance type modification") allows you to scale your compute resources vertically. Unlike Rackspace's Rebuild feature, AWS requires a more deliberate approach, especially for production environments.

  • EBS-backed instance (as specified in the question)
  • Basic AWS CLI setup (recommended)
  • Understanding of your instance's current configuration

Here's the most reliable method for resizing an EBS-backed instance:

# Step 1: Create an AMI of your current instance
aws ec2 create-image \
    --instance-id i-1234567890abcdef0 \
    --name "My server backup" \
    --description "AMI before instance resize" \
    --no-reboot

# Step 2: Stop the instance (can't resize running instances)
aws ec2 stop-instances --instance-ids i-1234567890abcdef0

# Step 3: Modify the instance type
aws ec2 modify-instance-attribute \
    --instance-id i-1234567890abcdef0 \
    --instance-type "m5.large"  # Your target instance type

# Step 4: Start the instance
aws ec2 start-instances --instance-ids i-1234567890abcdef0

For mission-critical systems, consider launching a new instance:

  1. Create AMI from current instance
  2. Launch new instance with desired type using the AMI
  3. Test thoroughly before DNS switch
  • Downtime: Resizing requires instance stop/start
  • Compatibility: Some instance families require virtualization type changes
  • Pricing: Larger instances incur higher costs

For frequent resizing, you might want to automate with Lambda:

import boto3

def lambda_handler(event, context):
    ec2 = boto3.client('ec2')
    
    # Stop instance
    ec2.stop_instances(InstanceIds=[event['InstanceId']])
    
    # Wait for stopped state
    waiter = ec2.get_waiter('instance_stopped')
    waiter.wait(InstanceIds=[event['InstanceId']])
    
    # Change instance type
    ec2.modify_instance_attribute(
        InstanceId=event['InstanceId'],
        InstanceType={'Value': event['NewInstanceType']}
    )
    
    # Start instance
    response = ec2.start_instances(InstanceIds=[event['InstanceId']])
    return response

If you encounter "InsufficientInstanceCapacity", try:

  • Choosing a different Availability Zone
  • Selecting a slightly different instance type
  • Waiting and retrying during non-peak hours