fbpx

How-to Resize an EBS-backed EC2 Instance

illustration of a cloud against an orange gradient background to represent ec2 backup and ebs snapshots
Share This Post

AWS EC2 supports two types of AMIs based on your root device: Instance Store-Backed and EBS-Backed. There are a number of advantages to launching an instance from an EBS-Backed AMI, including:

  1. Crash-consistent snapshots for persistent root and additional storage devices
  2. The ability to stop the instance to save on costs – stopping an instance will not incur any costs in terms of running the EC2 instance, but will for EBS storage.
  3. Vertical scaling. AWS EC2 allows you to modify the instance size of an EBS-backed instance so long as the instance is in a stopped state. This is very useful, allowing you to increase or decrease the size of a volume, however it requires the instance to be stopped.
In this article, we will show you how to manually change the size of an EBS-backed medium instance to micro. Additionally, we will provide you with a script that can automatically stop the instance, change its size and then start the instance again. The same script can be reused to change the instance size from a larger to smaller or a smaller to larger instance, promoting automation. Part 1:  Modifying an instance size through the Amazon Management Console & CLI Let’s assume that you’ve already installed AWS CLI on your machine.
  1. Once you have launched an EC2 instance, go to the AWS EC2 Instances section of the Amazon Management Console and select any instance. You can find the current instance type in the description area. n2ws To perform the same step using AWS CLI, run the following command:   aws ec2 describe-instancesn2ws
  2. To stop the instance, select ‘Instance State’-> ‘Stop’ from the ‘Actions’ menu. (Note: As noted above, the instance type can only be changed in stopped state) n2wsUsing CLI: aws ec2 stop-instances –instance-ids i-6370 n2ws
  3. To modify the instance size, select ‘Instance Settings’->’Modify Instance Type’ from the ‘Actions’ menu. Select the appropriate size. Seeing as we have launched a medium sized instance, we will now reduce the size to ‘t2.micro’. n2wsUsing CLI: aws ec2 modify-instance-attribute –instance-id i-6370  –instance-type “{\”Value\”: \”t2.micro\”}” n2ws
  4. Here we can see the change to a ‘t2.micro’ instance. n2ws Using CLI: aws ec2 describe-instances n2ws
  5. Once the instance size is modified, you can start the instance using the ‘Actions’ menu: n2wsUsing CLI: aws ec2 start-instances –instance-ids i-6370 n2ws

Part 2:

Modifying an Instance Size Using an Automated Script As previously mentioned, it is cost effective to change instance sizes every day if your situation permits. Therefore, it’s recommended to make an automated script that can perform these tasks for you. In this section, we present scripts that will stop an instance, change its size, and restart it. You can run these scripts from local machines or a continuous running EC2 instance and set them as a cron job or set a daily schedule in Windows. It is important to note that these scripts use AWS CLI commands, so it is necessary to install AWS CLI before running the scripts. We have prepared two scripts: one PowerShell Script for Windows machines and one shell script for Linux. PowerShell Script (to change the instance size to micro): $inst_id= ‘<Enter Instance ID>’ $instance_size=’t2.micro’ $instancestate =  aws ec2 stop-instances –instance-ids  $inst_id $state = “Not Stopped Yet” Do { $state = (Get-EC2Instance $inst_id).Instances.State.Name “Current State =  $state” Sleep -Seconds 5 } While ($state -ne “stopped”) Edit-EC2InstanceAttribute -InstanceId $inst_id -Attribute “instanceType” -Value $instance_size aws ec2 start-instances –instance-ids $inst_id n2ws Linux Shell Script: #!bin/sh # chkconfig: 2345 96 14 us_region_name=’us-east-1′ inst_id='<Specify-Instance-id>’ status=” aws ec2 stop-instances –instance-ids $inst_id –region $us_region_name while [ “$status” != “stopped” ]; do status=( $(aws ec2 describe-instances –region $us_region_name –instance-ids $inst_id   –output text –query ‘Reservations[*].Instances[*].State.Name’) ) echo “The instance ID ” $inst_id “is stopping now!” sleep 5 if [ “$status” == “stopped” ]; then echo $inst_id ” has ” $status break fi done echo “Modifying Instance Size of ” $inst_id aws ec2 modify-instance-attribute –instance-type “{\”Value\”: \”t2.micro\”}” –instance-id $inst_id –region $us_region_name echo “Starting Instance “$inst_id aws ec2 start-instances –instance-ids $inst_id –region $us_region_name n2ws In the example above, we decreased the size of an EBS backed EC2 instance. You may not prefer to perform this task on production instances, however it can work seamlessly without causing downtime in production. If your production system is set up with ELB and more than one instances running, you can stop one of the instances, change its size, then start it again. This may not result in any downtime since other instances will continue to serve requests in the meantime. However, it is important to note that ELB works with IP addresses, so if an instance is stopped and started again, it must have the same public DNS (use VPC or bootstrapping to assign an Elastic IP) in order to pass the ELB health check. It is also very important to back up the associated EBS volume before performing any sizing changes. If the instance fails to start for any reason, you will have a backup available. N2WS Backup & Recovery (CPM) is an enterprise-class backup and disaster recovery solution for the EC2 compute cloud. It is available as a service, allowing you to register multiple AWS accounts. You can configure policies and schedules to take automated snapshot backups. Additionally, you can configure policies to remove old snapshots. CPM provides automated and regular backups using the features listed below:
  1. Flexible backup policies and schedules
  2. Consistent database backup for SQL Server, Oracle, MySQL, MongoDB and more
  3. Instance and data recovery across AWS regions in seconds
  4. “Pull” and “Push” based alerts and notifications
  5. Application consistent backup
  6. Automated backup based on Tags.
  7. Copy EBS Snapshots to Amazon S3 buckets
Disclaimer: The scripts above come with no warranty. Feel free to use them for any purpose, including redistribution or modification. Try N2WS Backup & Recovery (CPM) for FREE! Read Also ]]>

Next step

The easier way to recover cloud workloads

Allowed us to save over $1 million in the management of AWS EBS snapshots...

Try N2WS for Free