Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@motchi0214

AWSのAPIでスロットリングされたらリトライするようにする

More than 1 year has passed since last update.

e.code === 'Throttling'でエラーがthrowされるので、これを拾ってリトライするように実装すればとりあえずOK。
ただし再帰呼び出しになるので、リトライ数を記録して無限ループにならないようにする必要はある。

// Wait処理
const sleep = async (time = 1000) => {
  return new Promise(resolve => setTimeout(resolve, time))
}

// CloudFrontのupdate処理
const worker = async (targetID, retryCount = 0) => {
  try {
    const dist = await cloudfront.getDistribution({
      Id: targetID
    }).promise()
    const distributionConfig = dist.Distribution.DistributionConfig
    distributionConfig.Enabled = false
    await cloudfront.updateDistribution({
      Id: dist.Distribution.Id,
      IfMatch: dist.ETag,
      DistributionConfig: distributionConfig
    }).promise()
  } catch (e) {
    if (e.code === 'Throttling') {
      if (retryCount < 2) {
        await sleep(2000)
        return worker(targetID, retryCount + 1)
      }
    }
    throw e
  }
}

const distIds = ['a', 'b', 'c']
Promise.all(distIds.map(async id => worker(id))

0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
motchi0214
AWS Samurai 2017 Alexa Champions
digitalcubeinc
デジタルキューブは AWS Partner Network (APN) アドバンスドテクノロジーパートナーです。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?