terraform-provider-newrelic: new_relic_alert_policy_channel returns consistent 422 (Unprocessable Entity)

Hi,

Terraform Version

Terrafrom Version: 0.11.14.7
Plugin Version: 2.1.1

Affected Resource(s)

  • new_relic_alert_policy_channel

Terraform Configuration Files

resource "newrelic_alert_policy" "slack" {
    name = "${var.application_name} Policy"
}

resource "newrelic_alert_channel" "slack" {
  name = "${var.application_name} Channel"
  type = "slack"

  config = {
    url = "${var.slack_channel_url}"
  }
}

resource "newrelic_alert_policy_channel" "slack" {
  policy_id = "${newrelic_alert_policy.slack.id}"
  channel_ids = ["${newrelic_alert_channel.slack.id}"]
}

Debug Output

newrelic_alert_channel.slack: Creating...
  config.#:     "" => "1"
  config.0.url: "<sensitive>" => "<sensitive>"
  name:         "" => "Team Channel"
  type:         "" => "slack"
newrelic_alert_channel.slack: Creation complete after 0s (ID: <channel-id>)
newrelic_alert_policy_channel.slack: Creating...
  channel_ids.#: "" => "1"
  channel_ids.0: "" => "<channel-id>"
  policy_id:     "" => "<policy-id>"

Error: Error applying plan:

1 error occurred:
	* newrelic_alert_policy_channel.slack: 1 error occurred:
	* newrelic_alert_policy_channel.slack: 422 response returned: There was an error. Unable to add channel IDs: [<channel-id>].

Expected Behavior

The channel id should have been associated to the policy.

Actual Behavior

The channel id failed to be associated correctly resulting in a 422 response code from New Relic.

Steps to Reproduce

  1. Create a Slack alert channel.
  2. Create an alert policy.
  3. Associate Slack alert channel to alert policy.
  4. terraform apply

Important Factoids

The slack channel is an existing channel and is maintained in our Terraform state. It is being recreated every time due to what appears to be GH-763. The response from New Relic however is consistently a 422.

References

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 18 (3 by maintainers)

Most upvoted comments

We are also facing this problem while adding email alert channels to certain policies. The notification channel ID given in the error message refers to the notification channel in our test account even though we are fully in production. We have double check the tfstate file and even deleted the same to make sure everything is being created from scratch.

best mathias

@sdedovic I had a similar issue with multiple accounts. In my case I was trying to move a notification channel from the parent account to the child account. The notification channel already existed in the parent account, but I did not destroy the channel in the parent account before using the API key for the child account to create the new notification channel. Creating the policy then failed with a 422.

I resolved this by:

  1. Manually deleting the notification channel (and all policies and conditions associated with that channel) in the parent account
  2. Re-creating the channel in the child account
  3. Re-creating policies, conditions, etc in child account

I am also getting this problem. Consistent 422 error. The channel seems to create just fine but does not show up in the NewRelic UI. I can even re-import the channel back into my tfstate file so it does seem to be there. But when I try to assign to an alert policy it fails every time.

Support?

Just set up a new alert for the first time for a new team and I’m getting this too. I’m using Pulumi but it’s Terraform-by-proxy:

Diagnostics: newrelic:index:AlertPolicyChannel (wwwMarketingTest-ap-channel-newrelic-marketing WWW Test Policy): error: 422 response returned: There was an error. Unable to add channel IDs: [4451817].

The channel ID is my email address which I created manually in the console and I can verify both its existence and the channel ID is correct. The code does a lookup of the channel based on name and then passes the ID into the constructor for the AlertPolicyChannel object.

I ran into this problem.

Based on @rlmv’s comment I identified my state file was tracking a resource newrelic_alert_policy.policy which didn’t seem to exist in the account I was expecting. Turns out it had created it in our development environment (different account ID) because the automation used a default value for account_id in provider the first time it deployed. All of the dependent resources that referenced the alert policy were trying to refer to an “existing” resource that wasn’t in the account it was now correctly configured with.

Manually cleaning up the resource and removing from state with terraform state rm <address> resolved my issue.

I spent some time digging into this and here is what I found:

The channels-create API only accepts the following:

  • New channel’s name
  • Type of channel
  • Configuration values

And as such, it seems to use the Account of your User API Key for creation. It can’t create a channel under a different Account. See the API docs


Policies and Conditions, on the other hand, can be created under a specified Account. This is all fine as long as you have the proper permissions, i.e. the User API Key can operate on all Accounts.

Now the problem seems to be when you have a Policy/Condition under a different Account than the Alert Channel. Wiring the two together fails with this 422 error. I think, and I’m testing it, this can all work fine if your User API Key has access to everything. Hopefully I’ll have an update shortly.

Update: Creating an User API Key under the Account where the Alert Channel is located fixed the issue.