Terraform: Misleading error message when JSON parsing fails

Created on 12 Oct 2017  ยท  6Comments  ยท  Source: hashicorp/terraform

Terraform Version

Terraform v0.10.7

Terraform Configuration Files

This module:

module "ecs_service" {
  # When using these modules in your own templates, you will need to use a Git URL with a ref attribute that pins you
  # to a specific version of the modules, such as the following example:
  source = "a-secret-github.com-source-file"
  service_name = "${var.service_name}"
  task_arn                = "${aws_ecs_task_definition.task.arn}"
  desired_number_of_tasks = 3
}

depends on this definition:

resource "aws_ecs_task_definition" "task" {
  family                = "${var.service_name}"
  container_definitions = "${data.template_file.container_definition.rendered}"
  task_role_arn         = "${aws_iam_role.ecs_task_role.arn}"
}

I was getting this very misleading error message:

Error running plan: 1 error(s) occurred:

* module.ecs_service.var.task_arn: Resource 'aws_ecs_task_definition.task' not found for variable 'aws_ecs_task_definition.task.arn'

It's confusing, because I can see very clearly that the task definition exists. Running with TF_LOG=DEBUG revealed the following logs:

2017/10/12 14:12:45 [DEBUG] Resource state not found for "aws_ecs_task_definition.task": aws_ecs_task_definition.task
2017/10/12 14:12:45 [DEBUG] ReferenceTransformer: "aws_ecs_task_definition.task" references: []
2017/10/12 14:12:45 [ERROR] root: eval: *terraform.EvalValidateResource, err: Warnings: []. Errors: [ECS Task Definition container_definitions is invalid: Error decoding JSON: invalid character ']' looking for beginning of value]
2017/10/12 14:12:45 [ERROR] root: eval: *terraform.EvalSequence, err: Warnings: []. Errors: [ECS Task Definition container_definitions is invalid: Error decoding JSON: invalid character ']' looking for beginning of value]
2017/10/12 14:12:45 [ERROR] root: eval: *terraform.EvalInterpolate, err: Resource 'aws_ecs_task_definition.task' not found for variable 'aws_ecs_task_definition.task.arn'
2017/10/12 14:12:45 [ERROR] root: eval: *terraform.EvalSequence, err: Resource 'aws_ecs_task_definition.task' not found for variable 'aws_ecs_task_definition.task.arn'
2017/10/12 14:12:45 [TRACE] [walkPlan] Exiting eval tree: module.ecs_service.var.task_arn

Basically, the JSON was invalid. Here's the JSON file - there's a trailing comma that I missed.

    "environment": [
      {"name" : "SERVER_TEXT", "value" : "${server_text}"},
    ]

It would be nice if the JSON error message was bubbled up to the default log level, so I could see what the problem was, instead of the (very confusing) error message about how a resource didn't exist.

bug config

Most helpful comment

validated json from jsonlint ..still gets this error

All 6 comments

Just ran into this as well. I had an errant , in my container definition and it took me a while to track it down due to that confusing error message.

validated json from jsonlint ..still gets this error

Hi all! Sorry for this weird behavior and for the delayed response.

I've just tried this with the v0.12.0-alpha1 release using the following simplified configuration:

provider "aws" {
  region = "us-west-2"
}

resource "aws_ecs_task_definition" "task" {
  family                = "placeholder"
  container_definitions = "invalid"
}

In the alpha release, this validation problem is now being correctly caught and reported during the plan step:

$ terraform plan

Error: ECS Task Definition container_definitions is invalid: Error decoding JSON: invalid character 'i' looking for beginning of value

  on ecs-json-validation.tf line 8:
   8: }

Unfortunately the reported location of the invalid definition is not correct yet because the providers have not yet been updated to return detailed source location information and so it's just indicating the "aws_ecs_task_definition" "task" block as a whole rather than specifically the container_definitions attribute. That will be dealt with in a later change to the provider SDK, since this bug currently applies to _all_ resource-level validation errors.

Since the direct problem reported in this issue has been addressed -- that resource validation errors during plan were not always halting the plan -- I'm going to close this out. The error messages will be improved further in later issues/PRs. Thanks for reporting this, and sorry again for the delay in addressing it.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jrnt30 picture jrnt30  ยท  3Comments

thebenwaters picture thebenwaters  ยท  3Comments

rkulagowski picture rkulagowski  ยท  3Comments

ronnix picture ronnix  ยท  3Comments

shanmugakarna picture shanmugakarna  ยท  3Comments