New Relic API key in Terraform

Hi guys

Im facing an error when I’m trying to use New Relic provider in Terraform
Error: 401 response returned: Bad API Key or no API Key provided

on main.tf line 7, in resource “newrelic_alert_policy” “apache_policy”:
7: resource “newrelic_alert_policy” “apache_policy” {

I’m getting API key form Account settings -> API keys -> REST API key

or

https://rpm.newrelic.com/accounts//integrations?page=api_keys

I have tried to use API in my code as wall as set it as a Env variable “NEW_RELIC_API_KEY”
but facing same error again.

Can you please help ?

Thanks in advance

1 Like

Hi @sargis1177,

You’ll need a Personal API key for configuring the New Relic Terraform provider. To retrieve your Personal API key, please follow the instructions here:

https://docs.newrelic.com/docs/apis/nerdgraph/examples/use-nerdgraph-manage-license-keys-personal-api-keys

Here’s an example of a Nerdgraph query to locate existing personal API keys:

{
  actor {
    apiAccess {
      keySearch(query: {types: USER}) {
        nextCursor
        keys {
          id
          key
          name
        }
      }
    }
  }
}

If no keys are returned as part of this query, you can create one with the dropdown in the top of the Nerdgraph graphiql explorer, or with a query like this:

mutation {
  apiAccessCreateKeys(keys: {user: {accountId: <YOUR_ACCOUNT_ID>, name: "My new key", userId: <YOUR_USER_ID>}}) {
    createdKeys {
      id
      key
      name
    }
  }
}

Chris

1 Like