Skip to content

How do I troubleshoot "RequestLimitExceeded" errors that occur when I use PowerShell to programmatically launch multiple Amazon EC2 instances?

3 minute read
0

When I use PowerShell to launch multiple Amazon Elastic Compute Cloud (Amazon EC2) instances, I receive "RequestLimitExceeded" errors.

Resolution

The script that you use to launch an EC2 instance must adhere to the API request rate quota and the API resource rate quota. If you exceed these quotas, then you receive "RequestLimitExceeded" errors.

To resolve this issue, implement retry logic and exponential backoff strategies.

Note: By default, AWS SDK for .NET has a built-in retry mechanism.

Prerequisites:

If your request rate is predictable and you can control it in advance, then use delayed invocation to recover from throttling when it occurs.

Example script:

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

# Example Code to launch 50 EC2 instances of type 'm5a.large'.
try {    
  $params = @{
    ImageId = 'AMI_ID'
    InstanceType = 'm5a.large'
    AssociatePublicIp = $false
    SubnetId = 'Subnet_ID'
    MinCount = 10
    MaxCount = 15
     }
  for ($i=0;$i<5;$i++){
    $instance = New-EC2Instance @params
    Start-Sleep 5000 #Sleep for 5 seconds to allow Request bucket to refill at the rate of 2 requests per second
    }
} catch {
    Write-Error "An Exception Occurred!"
}

Note: Replace AMI_ID with your Amazon Machine Image (AMI) ID, Subnet_ID with your subnet ID, and m5a.large with your instance type. Also, replace 10 with the minimum number of instances to launch and 15 with the maximum number of instances to launch.

If your request rate isn't predictable, then include retry logic in the script.

Example script:

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

#Example Code to launch 50 EC2 instances of type 'm5a.large'.
$Stoploop = $false
[int] $Retrycount = "0"
do {
    try {
        $params = @{
            ImageId = 'AMI_ID'
            InstanceType = 'm5a.large'
            AssociatePublicIp = $false
            SubnetId = 'Subnet_ID'
            MinCount = 40
            MaxCount = 50
        }
    $instance = New-EC2Instance @params
    $Stoploop = $true
    } catch {
        if ($Retrycount -gt 3) {
            Write - Host "Could not complete request after 3 retries."
            $Stoploop = $true
        } else {
           Write-Host "Could not complete request retrying in 5 seconds."
           Start-Sleep -Seconds 25 
           #25 seconds of sleep allows for 50 request tokens to be refilled at the rate of 2/sec
           $Retrycount = $Retrycount + 1
           }
        }
    } While($Stoploop -eq $false)

Note: Replace AMI_ID with your AMI ID, Subnet_ID with your subnet ID, and m5a.large with your instance type. Also, replace 40 with the minimum number of instances to launch and 50 with the maximum number of instances to launch.

Related information

Request throttling for the Amazon EC2 API

Retry behavior

AWS OFFICIALUpdated 6 months ago
1 Comment

This article was reviewed and updated on 2026-03-09.

AWS
EXPERT

replied 6 months ago