[opswitch] EC2のバックアップ作成タスクを複数ジョブで同時実行した場合の挙動を確認してみた

同一タスクを複数のジョブで同時実行した場合、片方のジョブが失敗します。実行時間に注意してジョブを定義するようにしましょう。
2024.04.10

日次・週次で同じジョブを実行させたい

こんにちは!AWS事業本部のおつまみです。

みなさん、Opswitchを利用したことありますか?私はあります。

Opswitchとは、EC2 や RDS のバックアップ・起動・開始など、AWS でのジョブ実行を誰でも簡単にスケジューリングできるサービスです。
そして、こちらなんと無料で利用することができます。詳細は下記をご確認ください。

先日こちらのツールをお客様に提案したところ、下記の質問をいただきました。

同一タスク(例えばEC2のバックアップ作成など)で日次・週次のジョブを作成し、それを同時刻で実行させた場合にジョブが被ることになる。被った際にどのような挙動になるのか?

その際、その場で回答することができなかったので、実際に挙動を確認しました。
今回はその結果をお伝えします!

いきなり結論

  • 同一タスクを2つ以上のジョブで同時実行された場合、片方のジョブが失敗する。
  • 日次・週次でジョブを起動させたい場合は、タスクを分け、ジョブの時間が重複しないように設定する必要がある。

前提知識

Opswitchを知らない方向けに、ジョブとタスクの関係についておさらいします。

opswitchを使ってインスタンスの起動・停止やバックアップ等を行うためには、タスクとジョブの作成が必要となります。

タスク

何をするかの定義になります。例えばEBSのスナップショットやEC2のバックアップの作成を、どのAWSアカウントのどこのリージョンでどのタグがついたリソースを対象にするかを設定します。ジョブを作成する前にまずタスクから作成します。

ジョブ

どのタスクをどの順番でどのようなスケジュールで実行するか、という定義になります。例えば、毎日3:00にタスクA、タスクBを順番に実行する、というような設定を行います。

図にするとこのようなイメージです。
タスク:ジョブ=N:1の関係で成り立っています。

やってみた

今回はEC2バックアップ作成のタスクを含んだ2つのジョブを同時実行させ、その挙動を確認しました。

事前準備

  • あらかじめテスト用のEC2インスタンスを起動しておきます。

タスクを作成

まずはタスクを作成します。

  • opswitchにログイン後、タスク-EC2-EC2バックアップの作成をクリックします。
  • +作成ボタンをクリックして、タスクを作成していきます。
  • ターゲットはバックアップ対象のEC2に設定するタグを指定する必要があります。今回はNameでターゲットを指定しました。

参考:EC2バックアップタスクの作成 – opswitch Support

ジョブを作成

今回作成したタスク「EC2バックアップの作成」を含むようにジョブを定義します。

  • opswitchにログイン後、ジョブをクリックします。
  • +作成ボタンをクリックして、ジョブを作成していきます。

参考:ジョブの追加 – opswitch Support

同手順で2つ目のジョブも定義します。

このように同時刻で同じジョブが実行されるように定義しました。

ジョブの実行結果確認

定義した時間を過ぎると、片方のジョブのみ成功していることが確認できました。

AMIも1つしか取得できていないようです。

実行履歴からログを確認することが可能なので、失敗したジョブのログを確認してみます。
該当のジョブで実行履歴カウンターを選択します。

確認したい実行履歴のログを選択します。

すると、このように実行ログを確認できます。

今回は以下のようなジョブの実行履歴のログが表示されていました。(xxxxxxxxxxxxは実際にはアカウントIDが表示されています。)

[2024-04-10 09:46:25,590] ******************************************************************************************
[2024-04-10 09:46:25,590] ジョブ: job_ec2_backup_2 ee4dc69a-c618-4912-86f7-571edd9015c6
[2024-04-10 09:46:25,590] 組織: 17c3e645-4e66-4d06-b4a7-eae28f225393
[2024-04-10 09:46:25,590] スケジュール: 2024-04-10 09:45:00
[2024-04-10 09:46:25,590] 開始日時: 2024-04-10 09:45:29
[2024-04-10 09:46:21,444]****************************************************************************************
[2024-04-10 09:46:21,444] タスク: task_ec2_backup
[2024-04-10 09:46:21,444] タスク種別: EC2バックアップの作成
[2024-04-10 09:46:21,444]-------------------------------------------------------------------------------------------
[2024-04-10 09:45:34,342] xxxxxxxxxxxx ap-northeast-1 Create Images 対象のインスタンスが1個あります。
[2024-04-10 09:45:34,342] xxxxxxxxxxxx ap-northeast-1 Create Images [test-ec2(i-0336e59739a4e0cee)]
[2024-04-10 09:45:34,621] xxxxxxxxxxxx ap-northeast-1 Create Images An error occurred (InvalidAMIName.Duplicate) when calling the CreateImage operation: AMI name i-0336e59739a4e0cee_2024-04-10_0945 is already in use by AMI ami-02563998776c5593c
[2024-04-10 09:45:39,459] xxxxxxxxxxxx ap-northeast-1 Create Images 対象のインスタンスが1個あります。
[2024-04-10 09:45:39,459] xxxxxxxxxxxx ap-northeast-1 Create Images [test-ec2(i-0336e59739a4e0cee)]
[2024-04-10 09:45:39,667] xxxxxxxxxxxx ap-northeast-1 Create Images An error occurred (InvalidAMIName.Duplicate) when calling the CreateImage operation: AMI name i-0336e59739a4e0cee_2024-04-10_0945 is already in use by AMI ami-02563998776c5593c
[2024-04-10 09:45:44,640] xxxxxxxxxxxx ap-northeast-1 Create Images 対象のインスタンスが1個あります。
[2024-04-10 09:45:44,640] xxxxxxxxxxxx ap-northeast-1 Create Images [test-ec2(i-0336e59739a4e0cee)]
[2024-04-10 09:45:44,841] xxxxxxxxxxxx ap-northeast-1 Create Images An error occurred (InvalidAMIName.Duplicate) when calling the CreateImage operation: AMI name i-0336e59739a4e0cee_2024-04-10_0945 is already in use by AMI ami-02563998776c5593c
[2024-04-10 09:45:49,516] xxxxxxxxxxxx ap-northeast-1 Create Images 対象のインスタンスが1個あります。
[2024-04-10 09:45:49,516] xxxxxxxxxxxx ap-northeast-1 Create Images [test-ec2(i-0336e59739a4e0cee)]
[2024-04-10 09:45:49,726] xxxxxxxxxxxx ap-northeast-1 Create Images An error occurred (InvalidAMIName.Duplicate) when calling the CreateImage operation: AMI name i-0336e59739a4e0cee_2024-04-10_0945 is already in use by AMI ami-02563998776c5593c
[2024-04-10 09:45:54,804] xxxxxxxxxxxx ap-northeast-1 Create Images 対象のインスタンスが1個あります。
[2024-04-10 09:45:54,804] xxxxxxxxxxxx ap-northeast-1 Create Images [test-ec2(i-0336e59739a4e0cee)]
[2024-04-10 09:45:55,022] xxxxxxxxxxxx ap-northeast-1 Create Images An error occurred (InvalidAMIName.Duplicate) when calling the CreateImage operation: AMI name i-0336e59739a4e0cee_2024-04-10_0945 is already in use by AMI ami-02563998776c5593c
[2024-04-10 09:45:59,683] xxxxxxxxxxxx ap-northeast-1 Create Images 対象のインスタンスが1個あります。
[2024-04-10 09:45:59,683] xxxxxxxxxxxx ap-northeast-1 Create Images [test-ec2(i-0336e59739a4e0cee)]
[2024-04-10 09:45:59,907] xxxxxxxxxxxx ap-northeast-1 Create Images An error occurred (InvalidAMIName.Duplicate) when calling the CreateImage operation: AMI name i-0336e59739a4e0cee_2024-04-10_0945 is already in use by AMI ami-02563998776c5593c
[2024-04-10 09:46:04,853] xxxxxxxxxxxx ap-northeast-1 Create Images 対象のインスタンスが1個あります。
[2024-04-10 09:46:04,853] xxxxxxxxxxxx ap-northeast-1 Create Images [test-ec2(i-0336e59739a4e0cee)]
[2024-04-10 09:46:05,120] xxxxxxxxxxxx ap-northeast-1 Create Images An error occurred (InvalidRequest) when calling the CreateImage operation: Another CreateImage operation is in progress on this instance. While the instance is rebooting during this operation, only one CreateImage operation can be in progress. Wait until the instance has rebooted, and then try again
[2024-04-10 09:46:09,808] xxxxxxxxxxxx ap-northeast-1 Create Images 対象のインスタンスが1個あります。
[2024-04-10 09:46:09,808] xxxxxxxxxxxx ap-northeast-1 Create Images [test-ec2(i-0336e59739a4e0cee)]
[2024-04-10 09:46:10,024] xxxxxxxxxxxx ap-northeast-1 Create Images An error occurred (InvalidRequest) when calling the CreateImage operation: Another CreateImage operation is in progress on this instance. While the instance is rebooting during this operation, only one CreateImage operation can be in progress. Wait until the instance has rebooted, and then try again
[2024-04-10 09:46:15,039] xxxxxxxxxxxx ap-northeast-1 Create Images 対象のインスタンスが1個あります。
[2024-04-10 09:46:15,039] xxxxxxxxxxxx ap-northeast-1 Create Images [test-ec2(i-0336e59739a4e0cee)]
[2024-04-10 09:46:15,284] xxxxxxxxxxxx ap-northeast-1 Create Images An error occurred (InvalidRequest) when calling the CreateImage operation: Another CreateImage operation is in progress on this instance. While the instance is rebooting during this operation, only one CreateImage operation can be in progress. Wait until the instance has rebooted, and then try again
[2024-04-10 09:46:19,946] xxxxxxxxxxxx ap-northeast-1 Create Images 対象のインスタンスが1個あります。
[2024-04-10 09:46:19,946] xxxxxxxxxxxx ap-northeast-1 Create Images [test-ec2(i-0336e59739a4e0cee)]
[2024-04-10 09:46:20,185] xxxxxxxxxxxx ap-northeast-1 Create Images An error occurred (InvalidRequest) when calling the CreateImage operation: Another CreateImage operation is in progress on this instance. While the instance is rebooting during this operation, only one CreateImage operation can be in progress. Wait until the instance has rebooted, and then try again
[2024-04-10 09:46:21,444]-------------------------------------------------------------------------------------------
[2024-04-10 09:46:21,444] ターゲットタグKey: Name
[2024-04-10 09:46:21,444] ターゲットタグValue: test-ec2
[2024-04-10 09:46:21,444] その他の設定: [ 世代数: 7, EC2インスタンスの再起動: する, 説明: , ターゲットインスタンスからタグをコピーする: いいえ, 紐づくEBSスナップショットに同じタグをつける: はい, AMIのステータスチェック: チェックしてfailedの場合、再実行する ]
[2024-04-10 09:46:21,444] タスク実行結果: 失敗
[2024-04-10 09:46:21,444] 開始日時: 2024-04-10 09:45:32,652
[2024-04-10 09:46:21,444] 終了日時: 2024-04-10 09:46:21,379
[2024-04-10 09:46:21,444]-------------------------------------------------------------------------------------------
[2024-04-10 09:46:21,444] An error occurred (InvalidAMIName.Duplicate) when calling the CreateImage operation: AMI name i-0336e59739a4e0cee_2024-04-10_0945 is already in use by AMI ami-02563998776c5593c
[2024-04-10 09:46:21,444] An error occurred (InvalidRequest) when calling the CreateImage operation: Another CreateImage operation is in progress on this instance. While the instance is rebooting during this operation, only one CreateImage operation can be in progress. Wait until the instance has rebooted, and then try again
[2024-04-10 09:46:21,444]-------------------------------------------------------------------------------------------
[2024-04-10 09:46:21,444] 作成したEC2バックアップ数: 0
[2024-04-10 09:46:21,444] 削除したEC2バックアップ数: 0
[2024-04-10 09:46:25,596] ******************************************************************************************
[2024-04-10 09:46:25,596] ジョブの実行に失敗しました。
[2024-04-10 09:46:25,596] 完了日時: 2024-04-10 09:46:24
[2024-04-10 09:46:25,596] ******************************************************************************************

ログ後半にエラーメッセージが出力されていましたね。

[2024-04-10 09:46:21,444]-------------------------------------------------------------------------------------------
[2024-04-10 09:46:21,444] An error occurred (InvalidAMIName.Duplicate) when calling the CreateImage operation: AMI name i-0336e59739a4e0cee_2024-04-10_0945 is already in use by AMI ami-02563998776c5593c
[2024-04-10 09:46:21,444] An error occurred (InvalidRequest) when calling the CreateImage operation: Another CreateImage operation is in progress on this instance. While the instance is rebooting during this operation, only one CreateImage operation can be in progress. Wait until the instance has rebooted, and then try again
[2024-04-10 09:46:21,444]-------------------------------------------------------------------------------------------

以下がエラー原因です。

  1. InvalidAMIName.Duplicateエラー:
    • AMIの名前が既存のAMIと重複しているため、新しいAMIを作成できませんでした。
    • AMIの名前を変更する必要があります。
  2. InvalidRequestエラー:
    • 同じインスタンスで複数のCreateImage操作を同時に実行しようとしたため、エラーが発生しました。
    • インスタンスの再起動が完了するまで待ち、その後でCreateImage操作を再試行する必要があります。

つまりEC2バックアップのタスクを2つ以上のジョブで同時実行された場合、片方のジョブが失敗するということですね。
よって、日次・週次でジョブを起動させたい場合は、タスクを分け、ジョブの時間が重複しないように設定する必要があるようです。

さいごに

今回は、EC2のバックアップ作成タスクを2つ以上のジョブで同時実行された場合の挙動を確認してみました。

Opswitchでジョブを管理するときには、ご注意ください!

最後までお読みいただきありがとうございました!
どなたかのお役に立てれば幸いです。

以上、おつまみ(@AWS11077)でした!

参考

AWS運用かんたん自動化ツール opswitch(オプスウィッチ) by クラスメソッド