Created
April 18, 2022 13:01
-
-
Save saggie/bf5f70c51bc43f5797d65330875a1864 to your computer and use it in GitHub Desktop.
Posting a budget alert of Google Cloud Platform to Slack using Cloud Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const slackUrl = 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'; | |
exports.notifyBudgetAlert = (event, context) => { | |
if (event.data) { | |
const data = JSON.parse(Buffer.from(event.data, 'base64').toString()); | |
if (data.costAmount === 0) { | |
console.log('Skipped the process because the cost amount is zero. Budget name: ' + data.budgetDisplayName); | |
return; | |
} | |
const message = '' | |
+ '- Budget name: ' + data.budgetDisplayName + '\n' | |
+ '- Cost amount: ' + data.costAmount + '\n' | |
+ '- Budget amount: ' + data.budgetAmount + '\n' | |
console.log(message); | |
const body = { text: message }; | |
axios | |
.post(slackUrl, body) | |
.then(res => console.log(res.data)) | |
.catch(res => console.log(res)); | |
} else { | |
console.log('Skipped the process because the data is empty.'); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "notify-budget-alert", | |
"version": "0.0.1", | |
"dependencies": { | |
"@google-cloud/pubsub": "^0.18.0", | |
"axios": "^0.25.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment