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
8
Help us understand the problem. What is going on with this article?
@satoshi_iwashita

Reserved Instances が expire される前に通知する

More than 5 years have passed since last update.

そういう機能がある気もしますが、bash で書けたので晒してみます。
cron などで日次で動かすことを想定しています。

この内容だと、30/15/7日前に通知メールを送信するようになっています。

ri_expire_checker.sh
#!/bin/bash

# Reserved Instance expire checker

from_address="_from_mail_address_"
to_address="_to_mail_address_"

notification()
{
    MAILBODY="""
ReservedInstances will expire ${d} days after.
please consider buying.

AZ : ${AZ}
InstanceType : ${INSTANCE_TYPE}
InstanceCount : ${INSTANCE_COUNT}
"""

    echo "${MAILBODY}" | mail -s "ReservedInstances Notification" -r ${from_address} ${to_address}

}

check_ri()
{
    ACTIVE_RIS=$(aws --region ap-northeast-1 ec2 describe-reserved-instances \
        --query 'ReservedInstances[?State!==\`Retired\`].[End,AvailabilityZone,InstanceType,InstanceCount,State]' \
        --output text)

    echo "${ACTIVE_RIS}" | while read line
    do
        AZ=$(echo "${line}" | awk {'print $2'})
        INSTANCE_TYPE=`echo "${line}" | awk {'print $3'}`
        INSTANCE_COUNT=`echo "${line}" | awk {'print $4'}`

        TODAY=$(date +'%Y%m%d')
        END_DATE=$(echo "${line}" | awk {'print $1'} | cut -d T -f 1)
        CHECK_DATE_1=`date -d "${END_DATE} - 30 days" +'%Y%m%d'`
        CHECK_DATE_2=`date -d "${END_DATE} - 15 days" +'%Y%m%d'`
        CHECK_DATE_3=`date -d "${END_DATE} - 7 days" +'%Y%m%d'`

        if [ ${TODAY} -eq ${CHECK_DATE_1} ]; then
            d="30"
            notification
        elif [ ${TODAY} -eq ${CHECK_DATE_2} ]; then
            d="15"
            notification
        elif [ ${TODAY} -eq ${CHECK_DATE_3} ]; then
            d="7"
            notification
        fi
    done
}

check_ri && exit 0

元記事はこちら

8
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

Comments

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