EC2 user data script failure alert

0

Hi Team,

Could you please help to find the way to setup some alert mechanism in case EC2 user data script failure. So that team can look at the logs later to troubleshoot later.

質問済み 1ヶ月前99ビュー
1回答
0

Hi,

As per https://stackoverflow.com/questions/70683772/cloudformation-how-to-handle-errors-in-userdata:

This is something that should be configured at the UserData level. Think of the UserData as simply a shell script which runs on the machine. You have full control over what happens while the script is run. Few options come to mind to achieve the behavior you are looking for:

  1. Setup the script so that on error it sends an alarm to CloudWatch
  2. Setup the script so that on error it uses SNS/SES to send you an email
  3. Setup the script so that on error the instance shuts itself down automatically (or indeed you can combine the above).

As to how the shell script should be configured, I suggest the use of trap, so that you can easily run whatever logic you want if the script ever errors.

An example:

#!/bin/bash -xe

notify() {
  echo "UserData was unsuccessful!"
  ... 
  # use this function to implement the notification/shutdown behavior
}

trap 'notify' ERR

cd /var/www
git clone myrepo html
...
profile pictureAWS
エキスパート
回答済み 1ヶ月前
profile picture
エキスパート
レビュー済み 1ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ