terraform data_error ai_generated true

Error: State data in S3 does not have the expected content: unexpected end of JSON input

ID: terraform/terraform-state-version-mismatch

Also available as: JSON · Markdown · 中文
70%Fix Rate
80%Confidence
1Evidence
2024-03-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Terraform v1.5 active
Terraform v1.6 active
Terraform v1.7 active
Terraform v1.8 active

Root Cause

The state file in the backend is empty, corrupted, or truncated, often due to concurrent writes or incomplete uploads.

generic

中文

后端中的状态文件为空、损坏或被截断,通常由并发写入或不完整上传引起。

Official Documentation

https://developer.hashicorp.com/terraform/language/state/backends#s3

Workarounds

  1. 85% success Restore the state file from a previous backup version in S3 (if versioning is enabled) using AWS CLI: 'aws s3api get-object-version --bucket <bucket> --key <key> --version-id <version-id> terraform.tfstate.backup'.
    Restore the state file from a previous backup version in S3 (if versioning is enabled) using AWS CLI: 'aws s3api get-object-version --bucket <bucket> --key <key> --version-id <version-id> terraform.tfstate.backup'.
  2. 50% success If no backup exists, use 'terraform state pull' to download the current state, manually fix the JSON if possible, then push it back using 'terraform state push fixed.tfstate'.
    If no backup exists, use 'terraform state pull' to download the current state, manually fix the JSON if possible, then push it back using 'terraform state push fixed.tfstate'.

中文步骤

  1. Restore the state file from a previous backup version in S3 (if versioning is enabled) using AWS CLI: 'aws s3api get-object-version --bucket <bucket> --key <key> --version-id <version-id> terraform.tfstate.backup'.
  2. If no backup exists, use 'terraform state pull' to download the current state, manually fix the JSON if possible, then push it back using 'terraform state push fixed.tfstate'.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Reinitialization does not fix a corrupted state file; it only reconfigures the backend connection.

  2. 80% fail

    Deleting the state file without a backup leads to complete loss of state, requiring manual re-import of all resources, which is error-prone.

  3. 95% fail

    'terraform state pull' only downloads the state as-is; it does not repair corrupted data.