AWS
EC2InstanceLimitExceeded - EC2 Instance Limit Exceeded
Hitting an **EC2InstanceLimitExceeded** error means you've reached the maximum number of EC2 instances you can launch in the specified region—this limit applies to the total number of running instances across all instance types. This client-side error (4xx) happens when AWS enforces EC2 instance limits. Most common when account-level instance limits are reached, but also appears when region-specific limits are exceeded, too many instances are running, instance type limits are hit, or VPC instance limits are reached.
#Common Causes
- →Identity: IAM policy allows EC2 launch but instance limit reached. Service Control Policy (SCP) enforces instance limits.
- →Network: VPC endpoint EC2 instance restrictions. Regional instance capacity limits.
- →Limits: Account-level instance limit reached (default: 20 On-Demand instances per region). Region-specific instance limit exceeded. Too many running instances. Instance type limit exceeded. VPC instance limit reached.
✓Solutions
- 1Step 1: Diagnose - Check current instance count: aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query 'length(Reservations[*].Instances[*])' --output text. Compare with account limit.
- 2Step 2: Diagnose - Check account limits: aws service-quotas get-service-quota --service-code ec2 --quota-code L-34B43A08 --query 'Quota.Value' --output text. Verify On-Demand instance limit (default: 20 per region).
- 3Step 3: Diagnose - List instances by type: aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query 'Reservations[*].Instances[*].[InstanceType,InstanceId]' --output table. Identify unused instances.
- 4Step 4: Fix - Request limit increase: aws service-quotas request-service-quota-increase --service-code ec2 --quota-code L-34B43A08 --desired-value 100. Or terminate unused instances: aws ec2 terminate-instances --instance-ids i-XXXXX.
- 5Step 5: Fix - Use different region or optimize: Launch instances in different region: aws ec2 run-instances --image-id ami-XXXXX --instance-type t3.micro --region us-west-2. Or review and optimize instance usage. Use Spot Instances for flexible capacity.
</>Code Examples
Check Current EC2 Instance Count and Limits
1#!/bin/bash
2echo "=== Current Running Instance Count ==="
3INSTANCE_COUNT=$(aws ec2 describe-instances \
4 --filters "Name=instance-state-name,Values=running" \
5 --query 'length(Reservations[*].Instances[*])' \
6 --output text)
7
8echo "Running instances: ${INSTANCE_COUNT}"
9
10# Check account limit
11echo "\n=== Account Limits ==="
12INSTANCE_LIMIT=$(aws service-quotas get-service-quota \
13 --service-code ec2 \
14 --quota-code L-34B43A08 \
15 --query 'Quota.Value' \
16 --output text 2>/dev/null || echo "20")
17
18echo "Instance limit: ${INSTANCE_LIMIT}"
19echo "Usage: ${INSTANCE_COUNT} / ${INSTANCE_LIMIT}"
20
21if [ ${INSTANCE_COUNT} -ge ${INSTANCE_LIMIT} ]; then
22 echo "✗ Instance limit reached (EC2InstanceLimitExceeded)"
23else
24 echo "✓ Instance count within limit"
25fi
26
27# List instances by type
28echo "\n=== Instances by Type ==="
29aws ec2 describe-instances \
30 --filters "Name=instance-state-name,Values=running" \
31 --query 'Reservations[*].Instances[*].[InstanceType,InstanceId]' \
32 --output tableRequest EC2 Instance Limit Increase
1#!/bin/bash
2echo "=== Requesting EC2 Instance Limit Increase ==="
3DESIRED_VALUE=100
4
5echo "Current limit: Check with aws service-quotas get-service-quota"
6echo "Desired limit: ${DESIRED_VALUE}"
7
8aws service-quotas request-service-quota-increase \
9 --service-code ec2 \
10 --quota-code L-34B43A08 \
11 --desired-value ${DESIRED_VALUE} \
12 --output json
13
14if [ $? -eq 0 ]; then
15 echo "\n✓ Limit increase requested"
16 echo "Check status: aws service-quotas get-requested-service-quota-change"
17 echo "Note: AWS Support may need to approve the request"
18else
19 echo "\n✗ Failed to request limit increase"
20 echo "Check IAM permissions or contact AWS Support"
21fiTerminate Unused EC2 Instances
1#!/bin/bash
2echo "=== Finding Unused Instances ==="
3
4# List all running instances
5aws ec2 describe-instances \
6 --filters "Name=instance-state-name,Values=running" \
7 --query 'Reservations[*].Instances[*].[InstanceId,InstanceType,LaunchTime,Tags[?Key==`Name`].Value|[0]]' \
8 --output table
9
10echo "\n=== Terminate Instance ==="
11echo "To terminate an instance:"
12echo "aws ec2 terminate-instances --instance-ids i-1234567890abcdef0"
13
14echo "\n=== Warning ==="
15echo "Terminating instances will:"
16echo "1. Stop the instance immediately"
17echo "2. Delete all data on instance store volumes"
18echo "3. Release the instance"
19echo "4. Free up instance quota"
20
21echo "\n=== Verify Before Terminating ==="
22echo "Check instance details:"
23echo "aws ec2 describe-instances --instance-ids i-XXXXX"↗Related Errors
Provider Information
This error code is specific to AWS services. For more information, refer to the official AWS documentation.