Hello. I need to create a notification channel fo...
# support
e
Hello. I need to create a notification channel for a Slack channel by using Terraform. This article shows the following code for a notification channel as an email:
Copy code
resource "signoz_notification_channel" "email_channel" {
  name = "Email Channel"
  type = "email"

  receivers {
    email_config {
      to = ["alerts@example.com"]
    }
  }
}
But the provider doc does not show the signoz_notification_channel resource. Is there a doc for resource signoz_notification_channel?
n
Hey @Edgar Peixoto, We don't support defining "notification channel" with our Terraform Provider.
e
I could try to create a custom module for that. But by looking at the requests in Signoz UI, there is a POST request with a bearer token for /api/v1/channels. Could I do the same POST request with an API token? Authorization: Bearer API_KEY?
a
Hi @Edgar Peixoto, what did you end up doing?
e
I created a custom terraform module. The idea is to send http requests to create (or destroy) a slack notification channel. Here is a piece of code to help you:
Copy code
terraform {
  required_providers {
    null = {
      source  = "hashicorp/null"
      version = "~> 3.0"
    }
  }
}

resource "null_resource" "slack_notification_channel" {
  triggers = {
    name              = var.name
    slack_webhook_url = var.slack_webhook_url
    slack_channel     = var.slack_channel
    send_resolved     = var.send_resolved
    signoz_endpoint   = var.signoz_endpoint
    signoz_api_token  = var.signoz_api_token
    title             = var.title
    text              = var.description
  }

  provisioner "local-exec" {
    command = <<-EOT
      # First, check if the channel already exists
      echo "Checking if channel '${var.name}' exists..."
      CHANNELS_RESPONSE=$(curl -s -X GET ${var.signoz_endpoint}/api/v1/channels \
        -H "SIGNOZ-API-KEY: ${var.signoz_api_token}" \
        -H "Accept: application/json")
      
      CHANNEL_ID=$(echo "$CHANNELS_RESPONSE" | jq -r '.data[]? | select(.name == "${var.name}") | .id')
      
      if [ "$CHANNEL_ID" != "null" ] && [ "$CHANNEL_ID" != "" ]; then
        # Channel exists, update it
        echo "Channel '${var.name}' exists with ID: $CHANNEL_ID, updating..."
        RESPONSE=$(curl -s -X PUT ${var.signoz_endpoint}/api/v1/channels/$CHANNEL_ID \
          -H "SIGNOZ-API-KEY: ${var.signoz_api_token}" \
          -H "Content-Type: application/json" \
          -H "Accept: application/json" \
          -d '${jsonencode(local.slack_config)}')
        
        echo "SigNoz API Update Response: $RESPONSE"
        
        # Check if the update was successful
        if echo "$RESPONSE" | grep -q '"status":"error"'; then
          echo "Error updating channel: $RESPONSE"
          exit 1
        fi
        
        echo "Channel '${var.name}' updated successfully"
      else
        # Channel doesn't exist, create it
        echo "Channel '${var.name}' doesn't exist, creating..."
        RESPONSE=$(curl -s -X POST ${var.signoz_endpoint}/api/v1/channels \
          -H "SIGNOZ-API-KEY: ${var.signoz_api_token}" \
          -H "Content-Type: application/json" \
          -H "Accept: application/json" \
          -d '${jsonencode(local.slack_config)}')
        
        echo "SigNoz API Create Response: $RESPONSE"
        
        # Check if the response contains the duplicate name error
        if echo "$RESPONSE" | grep -q "alertmanager_config_conflict"; then
          echo "Channel '${var.name}' already exists, skipping creation"
          exit 0
        fi
        
        # Check if the response indicates success (no "error" field)
        if echo "$RESPONSE" | grep -q '"status":"error"'; then
          echo "Error creating channel: $RESPONSE"
          exit 1
        fi
        
        echo "Channel '${var.name}' created successfully"
      fi
    EOT
  }

  provisioner "local-exec" {
    when    = destroy
    command = <<-EOT
      # Get the channel ID first
      CHANNEL_ID=$(curl -s -X GET ${self.triggers.signoz_endpoint}/api/v1/channels \
        -H "SIGNOZ-API-KEY: ${self.triggers.signoz_api_token}" \
        -H "Accept: application/json" | \
        jq -r '.data[]? | select(.name == "${self.triggers.name}") | .id')
      
      # Delete the channel if it exists
      if [ "$CHANNEL_ID" != "null" ] && [ "$CHANNEL_ID" != "" ]; then
        echo "Deleting channel '${self.triggers.name}' with ID: $CHANNEL_ID"
        curl -s -X DELETE ${self.triggers.signoz_endpoint}/api/v1/channels/$CHANNEL_ID \
          -H "SIGNOZ-API-KEY: ${self.triggers.signoz_api_token}" \
          -H "Accept: application/json"
        echo "Channel '${self.triggers.name}' deleted successfully"
      else
        echo "Channel '${self.triggers.name}' not found, nothing to delete"
      fi
    EOT
  }
}
a
That’s pretty cool! I am trying to do something similar. Great work @Edgar Peixoto