forked from imreFitos/aws-cost-to-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.js
91 lines (79 loc) · 2.1 KB
/
lambda.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
84
85
86
87
88
89
90
91
const AWS = require('aws-sdk')
const costexplorer = new AWS.CostExplorer()
const sns = new AWS.SNS({region: process.env.AWS_REGION})
// Handler
exports.handler = async function(event, context) {
try {
let cost = await getCost()
let amount = parseFloat(cost.ResultsByTime[0].Total.UnblendedCost.Amount).toFixed(2)
console.log(process.env.MESSAGE + " $" + amount)
let status = await sendSNSMessage(process.env.MESSAGE + " $" + amount)
return formatResponse(process.env.MESSAGE + " $" + amount)
} catch(error) {
return formatError(error)
}
}
var formatResponse = function(body){
var response = {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"isBase64Encoded": false,
"body": body
}
return response
}
var formatError = function(error){
var response = {
"statusCode": error.statusCode,
"headers": {
"Content-Type": "text/plain",
"x-amzn-ErrorType": error.code
},
"isBase64Encoded": false,
"body": error.code + ": " + error.message
}
return response
}
var getCost = function(){
let today = new Date()
let yesterday = new Date()
yesterday.setDate(today.getDate() - 1)
let y = yesterday.toISOString().slice(0,10)
let t = today.toISOString().slice(0,10)
let params = {
Granularity: 'DAILY',
Metrics: [ 'UnblendedCost' ],
TimePeriod: {
Start: y,
End: t
}
}
return costexplorer.getCostAndUsage(params).promise()
}
var sendSNSMessage = function(body){
let params = {
TopicArn: process.env.TOPIC,
Message: JSON.stringify({
"version": "0",
"id": "00000000-0000-0000-0000-000000000000",
"account": process.env.ACCOUNT,
"time": "1970-01-01T00:00:00Z",
"region": process.env.AWS_REGION,
"source": "aws.health",
"detail-type": "AWS Health Event",
"resources": [],
"detail": {
"eventDescription": [{
"language": "en_US",
"latestDescription": body
}]
}
})
}
return sns.publish(params).promise()
}
var serialize = function(object) {
return JSON.stringify(object, null, 2)
}