You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform导入NewRelic工作流Slack目标失败求助

Terraform导入NewRelic Slack通知目标时提示缺少account_id的解决方法

问题场景

尝试从现有NewRelic工作流中使用Terraform导入Slack通知目标,已编写main.tf配置,但执行导入命令时持续提示Missing required argument: account_id,即使将account_id改为变量仍无法解决。

现有配置

main.tf内容如下:

terraform {
  required_version = ">= 0.13.2"
  required_providers {
    newrelic = {
      source  = "newrelic/newrelic"
      version = "~> 3.4.1"
    }
  }
}

resource "newrelic_notification_destination" "foo" {
  account_id = 1111111
  type = "SLACK"
}

执行的导入命令及错误

执行命令:

terraform import newrelic_notification_destination.foo "11111111-11111-11cd-aaaaa-1111111111111aa"

报错信息:

│ Error: Missing required argument
│ 
│ The argument "account_id" is required, but was not set.
╵

解决步骤

1. 补全NewRelic Provider配置

NewRelic Provider需要明确配置账号信息,仅在resource块中设置account_id不足以让导入命令识别。需在配置中添加provider块:

terraform {
  required_version = ">= 0.13.2"
  required_providers {
    newrelic = {
      source  = "newrelic/newrelic"
      version = "~> 3.4.1"
    }
  }
}

provider "newrelic" {
  account_id = 1111111 # 可替换为变量,或使用环境变量NEW_RELIC_ACCOUNT_ID
  api_key    = "你的NewRelic API密钥" # 或使用环境变量NEW_RELIC_API_KEY
}

resource "newrelic_notification_destination" "foo" {
  account_id = 1111111
  type       = "SLACK"
}

2. 修正导入命令的ID格式

newrelic_notification_destination资源的导入ID需采用account_id:destination_id的格式,而非单独的目标ID。正确的导入命令应为:

terraform import newrelic_notification_destination.foo "1111111:11111111-11111-11cd-aaaaa-1111111111111aa"

3. 变量配置的正确用法(若使用变量)

若通过变量管理account_id,需确保变量被正确定义和引用:

  • 在variables.tf中定义变量:
variable "newrelic_account_id" {
  type    = number
  description = "NewRelic账号ID"
}

variable "newrelic_api_key" {
  type    = string
  description = "NewRelic API密钥"
  sensitive = true
}
  • 在terraform.tfvars中设置变量值:
newrelic_account_id = 1111111
newrelic_api_key = "你的API密钥"
  • 在provider和resource中引用变量:
provider "newrelic" {
  account_id = var.newrelic_account_id
  api_key    = var.newrelic_api_key
}

resource "newrelic_notification_destination" "foo" {
  account_id = var.newrelic_account_id
  type       = "SLACK"
}

内容的提问来源于stack exchange,提问作者coolcoder2022

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 04:35:39