Cloud Function to send GCP alerts to MS Teams
Medium Article: https://medium.com/@mathmartns/gcp-alerts-on-microsoft-teams-309f7bd8bd0a
- Webhook for the MS Teams channel, for that, visit this well-covered guide by Microsoft;
- Pub/Sub Topic and subscription to receive the Alert;
- The Policy that will trigger the alert;
- Create the Cloud Function using the provided files, you NEED to create a runtime environment variable named webhookURL that contains the webhook URL that the MS Teams generated for you (or you can hardcode it, use a variable inside your code, here it's your choice).
As the name says, this is the main file of our function, Our main function receives event and context, both come from the pub/sub and are mandatory, but the info that we need is inside the event on its data section.
Here is where the magic happens, we get the required info from the Pub/Sub original Alert and insert them on the fields of the JSON alert that will be sent to the MS Teams.
A lot of transformation is going on there and I won’t be able to cover it here, but the strategy is basically the same, We assign to a variable the value we want from the original alert message (if necessary perform modifications, for example, the Timestamp we need to adjust it to a human-readable format) and “paste” it on the respective field of the new alert JSON template.
This is the template file that will be filled with the information that will come from the Pub/Sub and this format is readable by the MS Teams, you can find more about it here.