Rightsize EC2 Instances with Compute Optimizer and the CLI
Get a recommendation backed by at least 30 hours of metrics, then change the instance type with a planned stop.
Rightsizing an EC2 instance is two separate jobs: deciding the new type from measured utilisation, and changing the type, which requires a stop. AWS Compute Optimizer does the first; the EC2 API does the second. This guide covers both from the CLI.
1. Opt in to Compute Optimizer
aws compute-optimizer update-enrollment-status --status Activeaws compute-optimizer get-enrollment-status --query status --output textOpting in needs compute-optimizer:UpdateEnrollmentStatus plus iam:CreateServiceLinkedRole and iam:PutRolePolicy for the AWSServiceRoleForComputeOptimizer role. Compute Optimizer also asks you to enable Cost Explorer so it can use billing data when it calculates savings.
2. Wait for enough data
An EC2 instance needs at least 30 hours of CloudWatch metric data in the past 14 days before Compute Optimizer produces a recommendation. With the enhanced infrastructure metrics preference switched on, it looks back up to 93 days. The first analysis can take up to 24 hours after opt-in.
3. Pull the over-provisioned instances
aws compute-optimizer get-ec2-instance-recommendations \ --filters name=Finding,values=Overprovisioned \ --query "instanceRecommendations[].{instance:instanceArn,current:currentInstanceType,finding:finding,options:recommendationOptions[].instanceType}" \ --output jsonfinding is one of Overprovisioned, Underprovisioned or Optimized. The filter also accepts FindingReasonCodes (for example CPUUnderprovisioned) and tag filters such as tag:Owner, which is a practical way to hand each team its own list. Take the first option unless it changes processor architecture or drops a feature the workload needs, such as instance store volumes or a network bandwidth tier; the EC2 guide on compatibility for changing the instance type lists what must match.
4. Change the instance type
The instance must be stopped before its type can change. Plan the downtime, then:
aws ec2 stop-instances --instance-ids i-0abc1234def567890aws ec2 wait instance-stopped --instance-ids i-0abc1234def567890
aws ec2 modify-instance-attribute --instance-id i-0abc1234def567890 \ --instance-type "{\"Value\": \"m7g.large\"}"
aws ec2 start-instances --instance-ids i-0abc1234def567890aws ec2 wait instance-running --instance-ids i-0abc1234def567890A stop and start moves the instance to new hardware. If it has a public IPv4 address that is not an Elastic IP, AWS releases it and assigns a new one, and any instance store data is lost. Permissions: ec2:StopInstances, ec2:ModifyInstanceAttribute, ec2:StartInstances, ec2:DescribeInstances.
5. Verify, and know the rollback
aws ec2 describe-instances --instance-ids i-0abc1234def567890 \ --query "Reservations[].Instances[].{type:InstanceType,state:State.Name}"Watch the application’s own latency and error rates for the next business cycle, not just CPU. Rolling back is the same four commands with the old type. Once the new type has its own 30 hours of data, re-run step 3 and check whether the finding for this instance has moved to Optimized.
How ZopNight approaches the same decision
ZopNight’s EC2 rightsizing rule works from a 30-day average CPU window: below 5 percent it proposes one tier down in the same family, below 2 percent two tiers, and it vetoes the downsize if memory would be too high afterwards. Where memory was not measured it marks the recommendation low confidence and says so. Each recommendation carries current cost, optimized cost and the metrics that triggered it, and the remediation wizard can apply it as a resize that includes the stop, with precondition and validation steps around it. That needs a read and write account connection; read-only accounts still receive the recommendations. The rightsizing methodology and rightsizing overview go deeper.