Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use Shell script to realize EC2 reserved instance RI expiration reminder

Shulou Source: shulou.com Published: 2022-06-01 03:04:10 10月04日 Update

Editor to share with you how to use Shell script to achieve EC2 reserved instance RI expiration reminder. I hope you will get something after reading this article. Let's discuss it together.

I. scene description

As a cloud operation and maintenance engineer, there are as few as hundreds of machines to manage, from tens of thousands to tens of thousands. For enterprise customers, ALL Upfront is used to purchase AWS EC2 (full payment in advance, which can save between 50% and 70% of the cost per year). Therefore, we need to remind the customer before the reserved instance (Reserve Instance) purchased by the customer expires to avoid the huge cost of following On demand (running on demand).

II. Script dependency conditions

The prerequisite for executing this script:

1. Configure the AWS CLI environment.

two。 Configure key files for all AWS accounts managed

Example: use aws configure-profile test-acccount (the red font represents the account name, which is used to identify different accounts)

Third, the function of the script

This is the first version of the script, which is relatively simple and consists of several steps:

1. Polling to obtain the expiration time of each customer's AWS account EC2 reserved instance

two。 Calculate the expiration time and the current time

3. If the time difference is less than 15 days, send an alarm email to the specified mailbox.

IV. Content of the script

#! / bin/bash

# Author:Andy Wei

# Version:1.0

# Function: Reserve Instance retired Alert

# customer list

Customer_list= ("test-account"myacccount")

For customer in ${customer_list [@]}

Do

# obtain the RI expiration information of all instances

Ri_teach_time_list= `aws ec2 describe-reserved-instances-- profile $customer-- query 'ReservedInstances [*]. [End,State]'-- output t

Ext | grep active | awk-F "T"'{print $1}'`# echo $customer

# get the current time information

Cur_date= `date "+% Y-%m-%d" `

# calculate the difference between the expiration time and the current time

For time in $ri_teach_time_list

Do

# calculate the difference between the expiration time and the current time

Time_difference=$ (($(date +% s-d "$time")-$(date +% s-d "$cur_date"))

Difference_day=$ (($time_difference/24/60/60))

# send email within 15 days by comparing the expiration time of RI with the current time

If [$difference_day-le 15]; then

Echo "The $customer account $time will have EC2 RI retired." | mail-s "The $customer RI retired Notication" your email address

Fi

Done

Done

After reading this article, I believe you have a certain understanding of "how to use Shell script to achieve EC2 reserved instance RI expiration reminder". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Tags: Time script instance customer account information days articles email mailbox management configuration different huge tens of thousands thousands between representatives enterprises content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno Shulou Technology Apple macOS