LoginSignup
9

More than 5 years have passed since last update.

AWSのイベントを取得してメールでお知らせするshell script

Last updated at Posted at 2015-01-19

AWSがインスタンス(EC2,RDS)に対して予定・実施する特定のメンテナンスイベント(再起動やリタイヤなど)をAWS CLIで取得してメール発報をするshell scriptを作成しました。

既に同様のスクリプトは多々有ると思いますが、折角ですので公開します。

どなたかの参考にでもなれば幸いです。

※2015/10/15 にRDSのメンテナンスイベントも取得するように修正しました

【参考ドキュメント】
「AWS CLIのフィルターとクエリーの使い方についてまとめてみた」
http://dev.classmethod.jp/cloud/awscli-enhanced-query-opt-snipets/

「【速報】Amazon RDS:メンテナンスへの柔軟な対応が可能に(pending-maintenance)」
http://dev.classmethod.jp/cloud/aws/rds-pending-maintenance-actions/

「jq tutorial」
http://stedolan.github.io/jq/tutorial/

「JMESPath Tutorial」
http://jmespath.org/tutorial.html

check_aws_evens.sh
#!/bin/bash
#
# @(#) check_aws_events.sh ver.0.0.2
#
# 前提条件:
# ・AWSのIamロールに"ec2 describe-instance-status"及び"rds describe-pending-maintenance-actions"を実行する権限が付与されている
#   http://aws.amazon.com/jp/iam/details/manage-permissions/
#
# ・AWS CLI(AWS-CLI 1.7.0以上)がインストールされている
#   http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html
#
# ・jqがインストールされている(本スクリプトでは /usr/bin を想定)
#   http://stedolan.github.io/jq/
#
# ・AWSのcredentials fileを取得し、その情報を配置している
#   (※配置場所は定数"CRED_FILE"で指定)
#
############################################################

# 実行時エラーでの停止(-e)を設定
set -e

# 定数設定
WORK_DIR=/path/to/work
CRED_FILE=/path/to/credencials_file
FROM_ADDRESS=address which send from
MAIL_ADDRESS=address which receive
MAIL_ADDRESS2=address which receive
SUBJECT="Event occurs!"
BODY1="here's the details."
BODY2="EC2
Instance Id, Availability Zone, Event Code, Event description, Event NotBefore, Event NotAfter"
BODY3="RDS
Action, ForcedApplyDate, CurrentApplyDate, AutoAppliedAfterDate, Description"


# 環境変数の設定
ACCESS_KEY=`/bin/cat $CRED_FILE | tail -1 | cut -d',' -f2`
ACCESS_SECRET_KEY=`/bin/cat $CRED_FILE | tail -1 | cut -d',' -f3`

export AWS_ACCESS_KEY_ID=$ACCESS_KEY
export AWS_SECRET_ACCESS_KEY=$ACCESS_SECRET_KEY
export AWS_DEFAULT_REGION=ap-northeast-1
export AWS_DEFAULT_OUTPUT=json


# 実処理
# EC2に関するEventsの有無を確認
ret=`/usr/bin/aws ec2 describe-instance-status --query "InstanceStatuses[?Events!=null]" | /usr/bin/jq -r '.[] | [.InstanceId, .AvailabilityZone, .Events[].Code, .Events[].description, .Events[].NotBefore, .Events[].NotAfter]|@csv'`


# rdsに関するメンテナンスの有無を確認
rds_ret=`/usr/bin/aws rds describe-pending-maintenance-actions | /usr/bin/jq -r '.PendingMaintenanceActions[]|[.ResourceIdentifier, .PendingMaintenanceActionDetails[].Action, .PendingMaintenanceActionDetails[].ForcedApplyDate, .PendingMaintenanceActionDetails[].CurrentApplyDate, .PendingMaintenanceActionDetails[].AutoAppliedAfterDate, .PendingMaintenanceActionDetails[].Description]|@csv'`

# Eventsを検知したらメール発報
if [ -n "$ret" ] || [ -n "$rds_ret" ]; then

  /usr/sbin/sendmail -t << EOF
From: ${FROM_ADDRESS}
To: ${MAIL_ADDRESS}
Cc: ${MAIL_ADDRESS2}
Subject: ${SUBJECT}

${BODY1}

${BODY2}
${ret}

${BODY3}
${rds_ret}


EOF

fi

exit 0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9