terraform config_error ai_generated true

错误:无效的目标:给定的目标无效:资源地址必须是有效的资源实例

Error: Invalid target: the given target is not valid: resource address must be a valid resource instance

ID: terraform/targeted-apply-without-plan

其他格式: JSON · Markdown 中文 · English
90%修复率
81%置信度
1证据数
2023-11-05首次发现

版本兼容性

版本状态引入弃用备注
1.5.0 active
1.6.0 active
1.7.0 active
1.8.0 active

根因分析

使用了格式不正确的资源地址的 -target 选项,例如缺少资源类型或名称。

English

The -target option was used with an incorrectly formatted resource address, such as missing the resource type or name.

generic

官方文档

https://developer.hashicorp.com/terraform/cli/commands/plan#target-address

解决方案

  1. 使用正确的资源地址格式:terraform apply -target="aws_instance.web"。对于模块:-target="module.vpc.aws_subnet.public"。使用 terraform state list 验证地址。
  2. 如果针对具有 count 或 for_each 的资源,请包含索引:-target="aws_instance.web[0]" 或 -target='aws_instance.web["key1"]'。
  3. 在应用之前使用 terraform plan -target 进行预览:terraform plan -target="aws_instance.web" -out=tfplan && terraform apply tfplan。

无效尝试

常见但无效的做法:

  1. Use the resource name only without the type, e.g., -target=my_resource. 100% 失败

    Terraform requires the full address format: resource_type.resource_name. A bare name is not recognized.

  2. Use -target with a module path that doesn't exist. 90% 失败

    The target must reference an existing resource in the configuration; non-existent paths cause the same error.

  3. Run terraform apply without any target, ignoring the need for targeted operations. 60% 失败

    This applies all changes, which may not be desired and could cause unintended side effects.