はじめに
AWSの使用料金をSlackに通知させるシステムを紹介されているサイトや記事をよく見かけるので今回私は個人的によく使っており、馴染みのあるDiscordにAWSの使用料金を通知するシステムを開発しました。
毎日午後11時にその日分の利用料金を通知、そして毎月1日の午後11時には先月分の利用料金合計を通知するという流れです。
為替APIも利用したので日本円に自動で換算できます。
完成品: https://github.com/K-Rintaro/aws-cost-discord-notify
注意
・本通知システムはAWS Cost Explorer APIを利用します。API リクエストごとに 0.01 USD (約1円)発生するのでご注意ください。
利用するもの
Discord bot
AWS Cost Explorer API
npmパッケージ類
・aws-cost-explorer
・dotenv
・discord.js
・node-cron
・node-fetch
作成手順
- Discord botをhttps://discord.com/developers/applications にて作成します。
- AWS IAMユーザーを新規作成し、アクセスキーIDとシークレットアクセスキーを控えます。
アタッチするポリシーの例:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ce:*"
],
"Resource": [
"*"
]
}
]
}
3. コード書いたりenvファイル作ったりします。
require('dotenv').config();
const config = {
apiVersion: '2017-10-25',
accessKeyId : process.env.AccessKeyId,
secretAccessKey : process.env.SecretAccessKey,
region : 'us-east-1'
}
const CostExplorer = require('aws-cost-explorer');
const ce = CostExplorer(config);
const fetch = require('node-fetch')
const cron = require('node-cron')
const Discord = require('discord.js');
const client = new Discord.Client();
cron.schedule('0 0 23 * * *', () => {
ce.getTodayCosts(null, function(err, data) {
if (err) {
console.log(err);
} else {
console.log(JSON.stringify(data))
var amount = data.Total.Amount
fetch('https://www.gaitameonline.com/rateaj/getrate')
.then(res => res.json())
.then(body => {
var usdjpy = body.quotes[20].bid
var jpyfinal = usdjpy * amount
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.channels.cache.get('YOUR_DISCORD_CHANNEL_ID').send({
embed: {
title: "Today's AWS Cost Notification",
color: 7506394,
timestamp: new Date(),
footer: {
text: "©️2021 Rintaro Kobayashi | MIT License"
},
thumbnail: {
url: "https://futurumresearch.com/wp-content/uploads/2020/01/aws-logo.png"
},
fields: [
{
name: "The amount of AWS cost",
value: `${amount} USD`
},
{
name: "USD :arrow_right: JPY",
value: `${jpyfinal} JPY`
}
]
}
})
})
}
});
})
cron.schedule('0 0 23 1 * *', () => {
ce.getLastMonthCosts(null, function(err, data) {
if (err) {
console.log(err);
} else {
console.log(JSON.stringify(data))
var amount = data.Total.Amount
fetch('https://www.gaitameonline.com/rateaj/getrate')
.then(res => res.json())
.then(body => {
var usdjpy = body.quotes[20].bid
var jpyfinal = usdjpy * amount
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.channels.cache.get('YOUR_DISCORD_CHANNEL_ID').send({
embed: {
title: "Last month's AWS Cost Notification",
color: 7506394,
timestamp: new Date(),
footer: {
text: "©️2021 Rintaro Kobayashi | MIT License"
},
thumbnail: {
url: "https://futurumresearch.com/wp-content/uploads/2020/01/aws-logo.png"
},
fields: [
{
name: "The amount of AWS cost",
value: `${amount} USD`
},
{
name: "USD :arrow_right: JPY",
value: `${jpyfinal} JPY`
}
]
}
})
})
}
});
})
client.login(process.env.Discord_Token);
4. ホスティングはHerokuやCloud9などお好きなものをご利用ください
おわりに
今回はDiscordを用いたAWS料金通知システムをご紹介しました。
一人でも多くの人のお役に立てられると幸いです。