EC2 T3 instances don’t have a fixed CPU speed; they burstable, meaning they can ramp up to full performance when needed.
Let’s see this in action. Imagine a T3.micro instance running a simple web server. By default, it starts with 144 CPU credit balance. When your web server receives a burst of traffic, the instance uses those credits to spin up its CPU cores to the maximum frequency of the underlying hardware.
# Check your current CPU credit balance
aws ec2 describe-instance-credits --instance-ids i-0123456789abcdef0
This command will show you the CPUBalance and CPUInstanceLimit for your instance. For a T3.micro, CPUInstanceLimit is 100%. If your CPUBalance is high, you can burst.
The core concept is that T3 instances earn credits when they are using less CPU than their baseline. The baseline is a percentage of a single CPU core’s capacity, and it varies by instance size. For a T3.micro, the baseline is 5% of one vCPU. So, if your instance is only using 2% of its CPU, it’s earning credits at a rate of 3% of a vCPU-hour. If it’s using 10% of its CPU, it’s using credits faster than it earns them.
Here’s how you can stop running out of CPU credits:
-
Monitor Your Credit Balance Closely:
- Diagnosis: Use CloudWatch metrics. The
CPUCreditBalancemetric is your primary indicator. Set up alarms when this balance drops below a certain threshold, say 30 credits for a T3.micro. - Fix:
aws cloudwatch put-metric-alarm --alarm-name T3MicroCPUAlarm --metric-name CPUCreditBalance --namespace AWS/EC2 --statistic Minimum --period 300 --threshold 30 --comparison-operator LowerThanThreshold --dimensions 'Name=InstanceId,Value=i-0123456789abcdef0' - Why it works: This proactively alerts you before your instance performance degrades due to running out of credits, giving you time to react.
- Diagnosis: Use CloudWatch metrics. The
-
Adjust Your Workload’s Baseline Usage:
- Diagnosis: Analyze your application’s CPU usage patterns. If your application consistently runs at or above its baseline for extended periods, it will inevitably deplete its credits. Use CloudWatch
CPUUtilizationalongsideCPUCreditBalance. - Fix: Optimize your application code to be more CPU-efficient. This might involve profiling your application to identify bottlenecks, optimizing algorithms, or reducing unnecessary computations. For example, if your web server is doing heavy log processing, defer that to a background job.
- Why it works: By reducing the average CPU usage below the baseline, the instance earns credits faster, building a buffer for bursts.
- Diagnosis: Analyze your application’s CPU usage patterns. If your application consistently runs at or above its baseline for extended periods, it will inevitably deplete its credits. Use CloudWatch
-
Scale Up to a Larger Instance Type:
- Diagnosis: If your workload legitimately requires sustained CPU performance above the T3 baseline, a T3 instance is likely the wrong choice. You’ll see your
CPUCreditBalanceconstantly hovering near zero, andCPUUtilizationfrequently hitting 100%. - Fix: Migrate to an instance type with a higher baseline CPU performance, such as a general-purpose M5 instance. For example, switch from a
t3.micro(1 vCPU, 5% baseline) to anm5.large(2 vCPUs, 40% baseline). - Why it works: Larger instance types have higher baselines and also earn credits faster, providing more sustained performance without relying on bursting.
- Diagnosis: If your workload legitimately requires sustained CPU performance above the T3 baseline, a T3 instance is likely the wrong choice. You’ll see your
-
Consider the
unlimitedMode:- Diagnosis: You’re running out of credits frequently, and scaling up isn’t feasible or cost-effective for your current needs, but you still need occasional bursts.
- Fix: Enable unlimited mode for your T3 instance. This is done at launch or by modifying the instance.
aws ec2 modify-instance-attribute --instance-id i-0123456789abcdef0 --cpu-credits unlimited - Why it works: In unlimited mode, if your instance exhausts its CPU credit balance, it can continue to burst beyond its baseline performance by incurring a low, per-vCPU-hour charge. This prevents performance degradation but adds cost if used extensively.
-
Use Dedicated Instances for Predictable Performance:
- Diagnosis: Your application requires consistent, predictable CPU performance and cannot tolerate any variation, even short bursts or the potential for charges in unlimited mode.
- Fix: Launch your workload on a Dedicated Instance type. These instances are physically isolated from other AWS customers.
- Why it works: Dedicated Instances offer a fixed, predictable level of CPU performance without the credit system, ensuring consistent throughput for critical applications.
-
Analyze Burst Usage Patterns:
- Diagnosis: You’re seeing frequent, short bursts of high CPU usage that deplete credits quickly, but the overall average utilization is low.
- Fix: If these bursts are caused by specific, infrequent tasks (e.g., batch jobs, cron jobs), consider scheduling them during off-peak hours or on instances specifically designed for batch processing. Alternatively, if these bursts are truly necessary, enable unlimited mode.
- Why it works: By understanding when and why bursts occur, you can either mitigate them by rescheduling or accommodate them through cost-effective means like unlimited mode or dedicated batch instances.
The next issue you’ll likely encounter after resolving CPU credit depletion is seeing NetworkIn and NetworkOut metrics hitting their instance limits, especially if your application is network-bound.