-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (76 loc) · 2.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const AWS = require('aws-sdk');
const axios = require('axios');
AWS.config.update({region:'us-east-1'});
const getFirstDateOfMonth = () => {
let temp = String(new Date().toISOString().substring(0,10));
return temp.substring(0,8)+'01'+temp.substring(10);
}
const getCurrentDate = () => {
return String(new Date().toISOString().substring(0,10));
}
const getUsageData = (event) =>{
var costexplorer = new AWS.CostExplorer();
var params = {
Metrics: [
'BlendedCost',
'UnblendedCost',
'UsageQuantity',
'AmortizedCost',
'NetAmortizedCost',
'NetUnblendedCost',
'NormalizedUsageAmount'
],
TimePeriod: {
End: getCurrentDate(),
Start: getFirstDateOfMonth()
},
Granularity: 'MONTHLY'
};
var result = null;
if(getCurrentDate() != getFirstDateOfMonth()) {
costexplorer.getCostAndUsage(params, function (err, data) {
if (err) {
console.log("ERROR OCCURRED");
console.log(err, err.stack);
}
else {
console.log("SUCCESS REQUEST");
console.log(data.ResultsByTime[0]);
for (let [key, value] of Object.entries(event)) {
sendNotification(value,data);
}
}
});
}
}
const sendNotification = (url, data) => {
axios({
method: 'post',
url: url,
data: {
Content:
"/md \n"
+`Hey **Shubham**, You monthly bill till today is : ** ${String(data.ResultsByTime[0].Total.BlendedCost.Amount).substring(0,6)} USD ** :flushed::racehorse:\n\n\n`
+" ----------------------------------- \n"
+"Name | From | To | Total Bill |\n"
+"------------ | ------------- | ----------- | ---------- |\n"
+`Shubham | ${getFirstDateOfMonth()} | ${getCurrentDate()} | ** ${String(data.ResultsByTime[0].Total.BlendedCost.Amount).substring(0,6)} USD**`
}
})
.then((response) => {
console.log(response);
}, (error) => {
console.log(error);
});
}
exports.handler = (event) => {
console.log("CW Bill Usage");
getUsageData(event);
console.log("#########");
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
console.log("END");
return response;
};