{"record":{"id":"9ff16ebf82069d15","repo":"hashicorp/terraform","slug":"can-t-serialize-backend-configuration-as-json-s-9ff16e","errorCode":null,"errorMessage":"Can't serialize backend configuration as JSON: %s","messagePattern":"Can't serialize backend configuration as JSON: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/state_migrate.go","lineNumber":197,"sourceCode":"\t\t\tmigrateOpts.DestinationType = rootMod.Backend.Type\n\t\t\tmigrateOpts.Destination = dstB\n\n\t\t\t// Capture details of the destination backend for updating the backend state file after a successful migration.\n\t\t\t_, cHash, bcDiags := c.backendConfig(&BackendOpts{\n\t\t\t\tBackendConfig: rootMod.Backend,\n\t\t\t})\n\t\t\tdiags = diags.Append(bcDiags)\n\t\t\tif bcDiags.HasErrors() {\n\t\t\t\tview.Diagnostics(diags)\n\t\t\t\treturn 1\n\t\t\t}\n\t\t\tbsf.Backend = &workdir.BackendConfigState{\n\t\t\t\tType: rootMod.Backend.Type,\n\t\t\t\tHash: uint64(cHash),\n\t\t\t}\n\t\t\terr := bsf.Backend.SetConfig(dstConfig, dstB.ConfigSchema())\n\t\t\tif err != nil {\n\t\t\t\tdiags = diags.Append(fmt.Errorf(\"Can't serialize backend configuration as JSON: %s\", err))\n\t\t\t\tview.Diagnostics(diags)\n\t\t\t\treturn 1\n\t\t\t}\n\t\t}\n\t} else if rootMod.StateStore != nil {\n\t\t// Get single required_providers entry for state store provider.\n\t\tdstReq, dstReqDiags := c.getDestinationStateStoreProviderRequirements(rootMod.StateStore.ProviderAddr, rootMod.ProviderRequirements)\n\t\tdiags = diags.Append(dstReqDiags)\n\t\tif dstReqDiags.HasErrors() {\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\n\t\t// Load any pre-existing destination provider lock file.\n\t\tvar lockfilePath string\n\t\tif args.DestinationLockFilePath != \"\" {\n\t\t\tlockfilePath = args.DestinationLockFilePath\n\t\t} else {","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/state_migrate.go#L179-L215","documentation":"Thrown during state migration when bsf.Backend.SetConfig(dstConfig, dstB.ConfigSchema()) fails. The destination backend's config schema cannot serialize the provided hcl config value into the workdir.BackendConfigState JSON form. The error uses %s (not %w) so the underlying error chain is flattened to a string.","triggerScenarios":"Destination backend schema rejects the config value: required attribute missing, type mismatch (string vs list), unknown hcl value remaining after no variable evaluation, or the schema returned by ConfigSchema() is empty/malformed.","commonSituations":"Migrating from local to S3 backend with a backend block missing required fields (e.g. `region`, `bucket`); typo in backend attribute name; variables in backend config that are not defined; custom backend with incomplete schema.","solutions":["Read the wrapped err string for the schema validation reason.","Compare your destination backend block against the backend's documented required attributes.","Validate the configuration statically first: `terraform validate` after `terraform init -backend=false`.","Remove any undefined variable references from the backend block; backend blocks cannot use locals or undefined vars."],"exampleFix":"// before\n terraform {\n   backend \"s3\" {\n     bucket = \"my-bucket\"\n   }\n }\n\n// after\n terraform {\n   backend \"s3\" {\n     bucket = \"my-bucket\"\n     region = \"us-east-1\"\n     key    = \"prod/terraform.tfstate\"\n   }\n }","handlingStrategy":"validation","validationCode":"// Validate the destination backend block schema before migration.\ncfg, diags := c.loadBackendConfig(rootMod)\nif diags.HasErrors() { return diags }\nif err := bsf.Backend.SetConfig(cfg, dstB.ConfigSchema()); err != nil {\n    return fmt.Errorf(\"destination backend config is invalid for type %q: %w\", dstB.Type(), err)\n}","typeGuard":null,"tryCatchPattern":"err := bsf.Backend.SetConfig(dstConfig, dstB.ConfigSchema())\nif err != nil {\n    diags = diags.Append(fmt.Errorf(\"Can't serialize backend configuration as JSON: %s\", err))\n    view.Diagnostics(diags)\n    hintInvalidBackendBlock(view, dstB.Type())\n    return 1\n}","preventionTips":["Run `terraform validate` after editing the backend block.","Cross-check required attributes against backend docs before migration.","Do not use undefined variables or locals inside the backend block.","Test the destination backend with `terraform init -backend-config=...` first."],"tags":["terraform","state-migration","backend","config","schema","hcl"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}