terraform-provider-newrelic
terraform-provider-newrelic copied to clipboard
Workflow support slack notifications
Feature Description
Currently it doesn't look like the Terraform provider supports creating a workflow and sending notifications to Slack.
I want to be able to configure the below via Terraform and link it to an existing policy.
Hi @huzie I believe that because Slack requires interactive authentication through OAuth, it can't be created by Terraform. The solution is to use the UI to create a Slack destination and import into Terraform. I'll also tag @lzaga-newrelic who may have some more context.
Hi @huzie , @mbazhlekova is correct. Slack requires interactive authentication through OAuth, therefore Slack destinations can only be imported to terraform after creating through the UI. Slack channels can be created via Terraform.
Hi @mbazhlekova and @lzaga-newrelic, thank you for your advice. We have imported the Slack destination and then followed the approach outlined here: https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/resources/workflow however seeing " Error: CONNECTION_ERROR: Unable to connect to external service to perform this action" It seems to be a problem with the new relic notification channel resource.
Hi @mbazhlekova @lzaga-newrelic, further to @xvella97 comment, we want to be able to create an alert policy which has an alert condition which is then linked to a workflow which has the slack destination. Is this possible?
Hi.
I am not sure I understand the situation. We are already creating Slack Channels in Terraform using the newrelic_notification_channel resource.
I am not sure I understand why we are now not able to do this in the new newrelic_notification_destination resource?
For us this appears to be a step backwards in functionality.
Hi @huzie , @mbazhlekova is correct. Slack requires interactive authentication through OAuth, therefore Slack destinations can only be imported to terraform after creating through the UI. Slack channels can be created via Terraform.
i'm struggling to get anywhere with workflows using the terraform provider. the documentation gives examples of using channel_id
which would be ok for now until we can get a data resource but when i export the destination id from the destinations tab as below i get an error
Error: INVALID_PARAMETER: A channel with id ***** was not found
i've also tried using an id from within alerts classic as below
and also using the UUID from the URL
clearly i'm being a dafty and would really appreciate some help :slightly_smiling_face:
Hi @tormodmacleod, the channel ID mentioned in the docs is the notifications channel but the channel ID you are trying to use is from the alerts channel. You can find the notifications channels as a section in workflows. At the moment you can get the ID of notifications channels from Nerd Graph.
We struggle too with the new workflow with Slack.
I imported the slack destination as described.
In the newrelic UI, "destination" are set for Slack Workspace and for "workflow" you specifiy the slack channel according the destination.
But in the workflow resource in Terraform, you can only put channel id who is the slack workspace.
How do you specify the Slack channel in terraform?
Hi @tormodmacleod, the channel ID mentioned in the docs is the notifications channel but the channel ID you are trying to use is from the alerts channel. You can find the notifications channels as a section in workflows. At the moment you can get the ID of notifications channels from Nerd Graph.
If I'm creating the workflow & notification channel via terraform how will this work? The Nerd Graph link you sent requires a workflow to exist already right?
@gdubroeucq You can get the Slack channel id from this NerdGraph query. You can add filters as you wish, like the channel name etc.
Also, if you imported the destination to your terraform, you can use a reference as described here:
Hi @huzie, @xvella97, @trustthewhiterabbit, @tormodmacleod, @gdubroeucq, @ayk33
Looks like there are several misunderstandings on several different topics in this issue. I'll try to explain how to create a Slack destination and channel and connect it to an alert policy and to a workflow.
First, as I mentioned here, There is no option to create a Slack destination via New Relic Terraform provider. Slack requires interactive authentication through OAuth, therefore Slack destinations can only be imported to terraform after creating through the UI.
Second, Slack channels can be created via Terraform. To create a Slack channel you'll have to provide a Slack channel id. We are working on adding to our docs more information on how to export the Slack channel id from Slack API.
Steps to create all resources mentioned in this issue:
- Create an Alert Policy:
resource "newrelic_alert_policy" "my-policy" {
name = "my_policy"
}
-
Create a Slack destination via the UI and authenticate: In New Relic One -> Alerts & AI Tab -> Under Destinations -> Choose Slack:
-
Import the Slack notification destination into terraform:
-
Get the destination id from the UI Destinations Table: In New Relic One -> Alerts & AI Tab -> Under Destinations -> Destinations Tab (attached photo) -> Filter Slack and pick your Slack destination -> Copy the destination Id in the 3 dots menu to the right:
-
Add an empty resource to your terraform file:
resource "newrelic_notification_destination" "slack-imported-destination" {
}
- Run import command: terraform
import newrelic_notification_destination.foo <destination_id>
. The destination Id is the one you picked in the previous step.
- Create a Slack notification channel via terraform:
resource "newrelic_notification_channel" "my-channel" {
account_id = 12345678
name = "slack-example"
type = "SLACK"
destination_id = "00b6bd1d-ac06-4d3d-bd72-49551e70f7a8"
product = "IINT"
property {
key = "channelId"
value = "123456"
}
}
Notice: you have to provide the Slack channel id here!
- Create workflow via terraform and connect it to the Slack notification channel:
resource "newrelic_workflow" "my-workflow" {
name = "workflow-example"
muting_rules_handling = "NOTIFY_ALL_ISSUES"
issues_filter {
name = "Filter-name"
type = "FILTER"
predicate {
attribute = "labels.policyIds"
operator = "EXACTLY_MATCHES"
values = [ newrelic_alert_policy.my-policy.id ] # Here is the policy you created in step #1
}
}
destination {
channel_id = newrelic_notification_channel.my-channel.id # Here is the notification channel you created in step #4
}
}
Notice: if you want to use another existing notification channel, that is not created via Terraform, you can look for it in NerdGraph API here.
Hope this helps to answer all of your questions.
you cannot create an empty newrelic_notification_destination
block, it's not a valid resource.
resource "newrelic_notification_destination" "slack-imported-destination" {}
Too few blocks specified for "property": At least 1 block(s) are expected for "property"Terraform
At least that's what my IDE is telling me.
My resource look like this:
resource "newrelic_notification_destination" "slack" {
name = "Slack Integration"
type = "SLACK"
property {
key = "scope"
label = "Permissions"
value = "app_mentions:read,channels:join,channels:read,chat:write,chat:write.public,commands,groups:read,links:read,links:write,team:read,users:read"
}
property {
key = "teamName"
label = "Team Name"
value = "xxx"
}
lifecycle {
ignore_changes = [auth_token]
}
}
I'm importing this resource using the import directive
import {
id = "xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxx"
to = newrelic_notification_destination.slack
}
and I still get this error
╷
│ Error: CONNECTION_ERROR: Unable to connect to external service to perform this action
│
│ with newrelic_notification_destination.slack,
│ on newrelic.tf line 1, in resource "newrelic_notification_destination" "slack":
│ 1: resource "newrelic_notification_destination" "slack" {
│
╵
It seems that you cannot rename a slack destination. The import seems to have worked, but applying new name is failing with the error above.
I wonder what is the point of having a resource
if you cannot update it, the documentation should recommend to use data
instead for slack. Note that the plan is happy with renaming the destination, and it's not suggesting that it will re-create it either.
I'm having the same issue with renaming after import. @Maxwell2022 did you find any workaround?
I believe the workaround is to create a new slack destination manually in the UI with the name you want and import it again. If you don't want to delete the existing destination you can remove it from your terraform state
terraform state rm newrelic_notification_destination.slack