Skip to content

Instantly share code, notes, and snippets.

@saggie
Created April 18, 2022 13:01
Show Gist options
  • Save saggie/bf5f70c51bc43f5797d65330875a1864 to your computer and use it in GitHub Desktop.
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
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.');
}
};
{
"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