terragrunt: terragrunt state pull throws an "unexpected end of JSON input" error

I’m not sure what’s going on here, but if you run:

% terragrunt init
<snip>
Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.
<snip>
Initializing provider plugins...

Terraform has been successfully initialized!
% terragrunt state pull > terraform.tfstate
[terragrunt] [/Users/chrisswingler/src/infra_terraform] 2018/07/06 12:03:12 Running command: terraform --version
[terragrunt] 2018/07/06 12:03:13 Reading Terragrunt config file at /Users/chrisswingler/Sprout/src/infra_terraform/terraform.tfvars
[terragrunt] 2018/07/06 12:03:13 unexpected end of JSON input
[terragrunt] 2018/07/06 12:03:13 Unable to determine underlying exit code, so Terragrunt will exit with error code 1

Terragrunt pukes on something not being valid JSON. I don’t know what it’s puking on, though. terraform state pull works fine.

Debug output:

➜  infra_terraform [default] git:(terragrunt_k8s) ✗  TERRAGRUNT_DEBUG=trye terragrunt state pull > terraform.tfstate
[terragrunt] [/Users/chrisswingler/Sprout/src/infra_terraform] 2018/07/06 13:16:51 Running command: terraform --version
[terragrunt] 2018/07/06 13:16:51 Reading Terragrunt config file at /Users/chrisswingler/Sprout/src/infra_terraform/terraform.tfvars
[terragrunt] 2018/07/06 13:16:51 *json.SyntaxError unexpected end of JSON input
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/remote/terraform_state_file.go:79 (0x142fbbb)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/remote/terraform_state_file.go:71 (0x142faed)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/remote/terraform_state_file.go:56 (0x142f925)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/remote/remote_state.go:69 (0x142c10b)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:571 (0x14d4182)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:441 (0x14d3361)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:410 (0x14d31d9)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:323 (0x14d27b6)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:235 (0x14d1a48)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:204 (0x14d1950)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/cli/cli_app.go:185 (0x14d1644)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/vendor/github.com/urfave/cli/app.go:502 (0x14762c8)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/src/github.com/gruntwork-io/terragrunt/vendor/github.com/urfave/cli/app.go:268 (0x14743ec)
/private/tmp/terragrunt-20180703-977-jbnzes/terragrunt-0.15.2/main.go:20 (0x14d7be4)
/usr/local/Cellar/go/1.10.3/libexec/src/runtime/proc.go:198 (0x102ccb2)
/usr/local/Cellar/go/1.10.3/libexec/src/runtime/asm_amd64.s:2361 (0x1056f11)

[terragrunt] 2018/07/06 13:16:51 Unable to determine underlying exit code, so Terragrunt will exit with error code 1

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 27 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Do you by chance have a .tfstate file sitting around in the current directory? E.g., if you run this all from a clean folder and with the --terragrunt-source-update flag, does it work?

I also ran into this issue, following what seemed to me to be a fairly standard use case.

  1. I started with a simple manifest and local state storage
  2. I then migrated to using terragrunt to handle configuring remote state in S3
  3. terragrunt init properly migrated the state to S3
  4. terragrunt plan broke with the invalid JSON error
  5. Removing the *.tfstate files from my local file system fixed the error

It would be helpful if terragrunt would either:

  1. Offer to backup the local state file in a manner that allows terragrunt to run after migrating to remote state
  2. Print a warning about local state files if remote state has been configured

@brikis98 - could this issue be reopened?

I ran into this as well. Removing the terraform.tfstate file fixed the issue.

Do you by chance have a .tfstate file sitting around in the current directory? E.g., if you run this all from a clean folder and with the --terragrunt-source-update flag, does it work?

@brikis98 Could you please add this as a warning to the stderr, I guess it will safe a lot of time for all of us.

I had this issue today and wanted to chime in that yes having *.tfstate files in the directory will cause problems. I ran in to it when I pulled remote state to terraform.tfstate and it started messing with everything.

I recommend using *.out or similar instead.

i ran into it as well. The error message:

unexpected end of JSON input

is really quite useless. I had a hunch that it could be about state, but unfortunatelly i just checked remote state json from s3 bucket and it was ok - didn’t notice local “terraform.tfstate” and “.terraform.tfstate.lock.info” files, as they were in .gitignore - what a waste of time it was!

Would be really useful to get more informative error messages (in this case mentioning terraform.tfstate and perhaps even suggesting deleting it, as 0 bytes state file doesn’t make sense)

I just ran into this. Seems like either deleting the empty file or issuing a warning would be a good fix.

Happened to me too (Terragrunt v0.23.10) after setting up S3 remote store backend.

The initialization asked for migration like this

Initializing the backend...
Do you want to copy existing state to the new backend?
  Pre-existing state was found while migrating the previous "local" backend to the
  newly configured "s3" backend. No existing state was found in the newly
  configured "s3" backend. Do you want to copy this state to the new "s3"
  backend? Enter "yes" to copy and "no" to start with an empty state.

  Enter a value: yes

and succeeded. The following attempt to do terragrunt plan resulted in

[terragrunt] 2020/04/25 00:32:37 unexpected end of JSON input

which was caused by an empty terraform.tfstate file in the cache. Removing the file fixed it.

I’m running into this currently and the only terraform.tfstate i have is 0 bytes b/c it is remotely managed in s3, if i remove it it wants to create and destroy everything. @thirstydeveloper or @koka424 - did the tfstate file have anything inside?

Edit: yes, removing the 0 byte file worked…it just didn’t seem like it because i had other errors that this was masking. Which may speak to better messaging, like adding the filename. Clearly my mistake, and thanks for the great project.

Closing due to inactivity