How To: Wait for loooooooooooooong AWS operations

How To: Wait for loooooooooooooong AWS operations
Photo by NordWood Themes / Unsplash

Many AWS operations can take a long time. Starting up an EC2 instance, taking a database backup, or deploying a CloudFormation stack. It all takes time.

Sometimes, you have to wait for these operations to finish before you can move on with your task. For example, I was recently working on running an ECS task using bash. I wanted to display the logs of the ECS task in stdout once the ECS task was finished. To do this, I would have to:

  1. Start the ECS task
  2. Wait for my task to finish running
  3. Fetch the logs from CloudWatch

AWS CLI comes with the handy wait command for use cases like this.

AWS wait

In the AWS CLI, you can "wait" until a particular condition is satisfied.

The syntax is aws <service> wait <condition>. The wait command is implemented on services with long-running operations. If you google aws <service> wait, you can find the CLI official documentation to see which conditions you can wait for:

The specific condition I was waiting for was aws ecs wait tasks-stopped. I passed in the task ID to the command, and it will wait until my task has finished running. It comes in handy when you want to make sure that a certain condition is met before proceeding with your code. However, the command does come with some drawbacks.

AWS is impatient

Unfortunately, AWS's wait command can't wait forever. There are parameters within the CLI that determine how long the CLI waits before it times out. And these parameters are not something that we can adjust.

The particular ECS task I was working on was deploying Python programs to a web application platform. During deployment, the ECS task detects the user's Python environment and installs all the necessary Python libraries on the server. This can take a long time, depending on the size of the Python program and whether the libraries are cached or not. When the deployment took too long, my task exited with a Max attempts exceeded error.

Loop it

The solution to this issue is pretty simple, really. You just have to keep waiting until it works. Kudos to antonbormotov (https://stackoverflow.com/questions/30977532/aws-command-line-interface-aws-ec2-wait-max-attempts-exceeded)

while [ "${exit_status}" != "0" ]
do
    aws ecs wait tasks-stopped --tasks "${TASK_ID}"
    exit_status="$?"
done

In this while loop, we first run the AWS wait command. Once the wait command is finished, we evaluate the exit code (by inspecting the $? variable). We only exit out of the loop if the exit code is 0 (successful).

GOTCHA: -e

When you are using this pattern, you want to be careful with the bash set -e builtin! When this is set, your script will exit immediately when a command outputs a non-zero status. In our example, the wait command will output a non-zero status and exit without completing the while loop.

Be especially careful in CI/CD environments! My GitHub Actions runner had this set and my loop didn't work.

To work around this, you can temporarily set +e to turn off the error-on-exit mode.

# Turn off error-on-exit
set +e

while [ "${exit_status}" != "0" ]
do
    aws ecs wait tasks-stopped --tasks "${TASK_ID}"
    exit_status="$?"
done

set -e

And that's it! Happy hacking.

Read more

今年読んだ本を振り返る 2024

今年読んだ本を振り返る 2024

早いもので、今年も年の瀬。皆さん、年末いかがお過ごしでしょうか。 いきなりですが、読書歴って人の性格とマイブームを如実に写し出すものだと思うんですね。というわけで、年末というキリのいい時期に、今年読んだ本を振り返って見ようと思います。 「ああ、年初はこんなこと考えてたなぁ」 「こんな本読んでたっけ!?」 思い出にふける自分用の記事になってます。 「最強!」のニーチェ入門 幸福になる哲学 (河出文庫) 去年末、國分 功一郎先生の「暇と退屈の倫理学」を読んでから哲学にハマってました。その流れを汲んで2024年一発目の本は哲学書。「暇と退屈の倫理学」の中でニーチェに触れ、彼がカッコいいと思いニーチェ入門書を購入…みたいな理由だったと思います。 数年前、飲茶先生の著書「史上最強の哲学入門」を読み、"とにかく分かりやすいなー!"と思ったのは覚えてます。哲学の入門書といえば飲茶先生です。 この「ニーチェ入門」も、著者と一般人の対話形式なので凄く分かりやすいです。哲学書によくある難しい言い回しはないですし、ページ数も少ないので一般人でも読みやすい本になってます。 入門書なんですが、

By Roland Thompson
Keychron Q11のすゝめ

Keychron Q11のすゝめ

年がら年中パソコンに張り付いてるネット民の同志の皆様、ごきげんよう。快適なパソコンライフ送ってますか? 私はと言うと、実はとても調子がいいのです! え?なぜかって?実はこいつのおかげでしてね・・・ Keychron Q11!! このスパッと2つに割れたキーボードを使い始めてから体の調子がすこぶる良くて、 * 長年悩まされた肩痛が治った * 姿勢が良くなった * 彼女ができた * 宝くじがあたった * この前、初めてホームランを打ったの! と、いいこと三昧なんですね。 そこで今回はこのKeychron Q11を皆さんに宣伝紹介したいと思います。 私とキーボード 我々の生活にスマホが普及して久しいですが、パソコンもまだまだ現役。読者の皆様は常日頃からパソコンを使う方が多いと思います。 総務省の情報通信白書によると、パソコンの世帯保有率は70%、インターネット利用率は50%前後を推移しています。まだまだ高い水準ですね。 かく言う私もパソコンがないと生きていけない生活を送ってます。 * まず、仕事がエンジニアなので、パソコンがないと飯が食えない * 続

By Roland Thompson