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

Also available as: JSON · Markdown · 中文
90%Fix Rate
81%Confidence
1Evidence
2023-11-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.5.0 active
1.6.0 active
1.7.0 active
1.8.0 active

Root Cause

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

generic

中文

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

Official Documentation

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

Workarounds

  1. 95% success Use the correct resource address format: terraform apply -target="aws_instance.web". For modules: -target="module.vpc.aws_subnet.public". Verify the address with terraform state list.
    Use the correct resource address format: terraform apply -target="aws_instance.web". For modules: -target="module.vpc.aws_subnet.public". Verify the address with terraform state list.
  2. 90% success If targeting a resource with count or for_each, include the index: -target="aws_instance.web[0]" or -target='aws_instance.web["key1"]'.
    If targeting a resource with count or for_each, include the index: -target="aws_instance.web[0]" or -target='aws_instance.web["key1"]'.
  3. 85% success Use terraform plan -target to preview before applying: terraform plan -target="aws_instance.web" -out=tfplan && terraform apply tfplan.
    Use terraform plan -target to preview before applying: terraform plan -target="aws_instance.web" -out=tfplan && terraform apply tfplan.

中文步骤

  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。

Dead Ends

Common approaches that don't work:

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

    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% fail

    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% fail

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