
When a Terraform run kicks off or a plan finishes, someone usually needs to know. If your team already lives in Microsoft Teams, the obvious place for that signal is the channel you're already watching. Pushing Terraform notifications straight into Teams is a common way to apply ChatOps: the run talks to you where you work, instead of in a log you have to go check.
The basic idea is to relay key events from Terraform operations into a Teams channel: the start of a plan, the plan output itself for review, a successful apply, or a failure. That keeps the people who care about a change aware of it as it happens. But you get more than plain alerting. Here's what running notifications this way buys you:
terraform plan can show the proposed changes right away for review, and a failure notification lets the team jump on the issue instantly.apply.plan enable teams to address problems before they escalate or impact end-users.For teams looking to implement this themselves, the typical process involves using Microsoft Teams' Incoming Webhook functionality:
Create an Incoming Webhook in Teams:
Prepare a Script to Send Notifications: You'll need a script that Terraform can trigger. This script will take Terraform output (or custom messages) and send it to the Teams webhook URL. This can be done using various languages; PowerShell or Python are common choices.
Trigger the Script from Terraform: You can use Terraform's local-exec provisioner (for resource-specific events, though less common for general run notifications) or, more typically, integrate this scripting into your CI/CD pipeline (e.g., Jenkins, GitLab CI, GitHub Actions). After a terraform plan or terraform apply command, your CI/CD script would call your notification script with the relevant status message.
Example (Conceptual - CI/CD script snippet):
# After terraform apply
if [ $? -eq 0 ]; then
./send_teams_notification.sh "Terraform apply successful for staging environment." "$TEAMS_WEBHOOK_URL"
else
./send_teams_notification.sh "ERROR: Terraform apply FAILED for staging environment." "$TEAMS_WEBHOOK_URL"
fiExample (Conceptual - Python):
import json
import requests
def send_teams_notification(message, webhook_url):
payload = {
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "0076D7",
"summary": "Terraform Notification",
"sections": [{
"activityTitle": "Terraform Operation Update",
"activitySubtitle": message,
"markdown": True
}]
}
try:
response = requests.post(webhook_url, data=json.dumps(payload), headers={'Content-Type': 'application/json'})
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Error sending Teams notification: {e}")
# Usage: send_teams_notification("Terraform plan initiated for production.", "YOUR_WEBHOOK_URL")Example (Conceptual - PowerShell):
param (
[string]$Message,
[string]$WebhookUrl
)
$payload = @{
"@type" = "MessageCard";
"@context" = "http://schema.org/extensions";
"themeColor" = "0076D7"; # Blue, or choose based on status (e.g., red for failure)
"summary" = "Terraform Notification";
"sections" = @(
@{
"activityTitle" = "Terraform Operation Update";
"activitySubtitle" = $Message;
"markdown" = $true
}
)
}
Invoke-RestMethod -Uri $WebhookUrl -Method Post -Body (ConvertTo-Json $payload) -ContentType 'application/json'While this DIY approach offers complete control, it does require setup, scripting, and ongoing maintenance of the webhook URLs and notification logic.
If you'd rather not own the scripts, some CI/CD and infrastructure automation platforms handle the integration for you. Scalr, for example, has a built-in notification system: instead of writing custom scripts and tracking webhook URLs by hand, you pick Microsoft Teams from a list of notification services, authenticate, and choose which Terraform events should fire an alert, all through a user interface. You end up with one less pipeline to maintain, and the events still land in an auditable record.
If you want full control and can absorb the maintenance, build the webhook integration yourself with the scripts above. If you'd rather skip the scripting, use a platform like Scalr that supports Teams natively. Both get the run's status in front of the people who need it while there's still time to act on it.
