Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report