PowerShell을 사용하여 여러 Amazon Elastic Compute Cloud(Amazon EC2) 인스턴스를 시작할 때 RequestLimitExceed 오류가 가끔 발생합니다.
해결 방법
Amazon EC2 API에 대한 RequestLimitExceeded 오류는 일반적으로 요청 속도 제한 또는 리소스 속도 제한을 나타냅니다. API 스로틀링을 나타냅니다. 재시도 로직과 지수 백오프 전략을 조합하여 이 문제를 해결할 수 있습니다.
Amazon EC2 인스턴스를 시작하는 것은 변경 호출이며 요청 속도 및 리소스 속도 제한의 적용을 받습니다. 인스턴스를 시작하는 데 사용하는 스크립트는 토큰 버킷의 리필 속도를 수용해야 합니다.
다음 지연된 호출 또는 재시도 전략 중 하나를 사용하여 RequestLimitExceeded 오류를 방지하세요.
참고: AWS SDK for .NET에는 기본적으로 켜져 있는 기본 제공 재시도 메커니즘이 있습니다.
시간 초과를 사용자 지정하려면 재시도 및 시간 초과를 참조하세요.
다음 예제에는 요청에 대한 지연된 호출 메커니즘이 포함되어 있습니다. 지연 호출을 사용하면 요청 버킷이 채워질 수 있습니다.
# 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 = 10
}
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!"
}
다음 예제에는 스크립트에 재시도 로직이 포함되어 있습니다.
# 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 = 50
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)
관련 정보
아마존 EC2 API에 대한 요청 스로틀링
재시도 동작