{"record":{"id":"d97ee9bafd5d6e59","repo":"hashicorp/terraform","slug":"invalid-value-for-q-in-state-s","errorCode":null,"errorMessage":"invalid value for %q in state: %s","messagePattern":"invalid value for %q in state: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/configs/hcl2shim/flatmap.go","lineNumber":200,"sourceCode":"\treturn val, nil\n}\n\nfunc hcl2ValueFromFlatmapPrimitive(m map[string]string, key string, ty cty.Type) (cty.Value, error) {\n\trawVal, exists := m[key]\n\tif !exists {\n\t\treturn cty.NullVal(ty), nil\n\t}\n\tif rawVal == UnknownVariableValue {\n\t\treturn cty.UnknownVal(ty), nil\n\t}\n\n\tvar err error\n\tval := cty.StringVal(rawVal)\n\tval, err = convert.Convert(val, ty)\n\tif err != nil {\n\t\t// This should never happen for _valid_ input, but flatmap data might\n\t\t// be tampered with by the user and become invalid.\n\t\treturn cty.DynamicVal, fmt.Errorf(\"invalid value for %q in state: %s\", key, err)\n\t}\n\n\treturn val, nil\n}\n\nfunc hcl2ValueFromFlatmapObject(m map[string]string, prefix string, atys map[string]cty.Type) (cty.Value, error) {\n\tvals := make(map[string]cty.Value)\n\tfor name, aty := range atys {\n\t\tval, err := hcl2ValueFromFlatmapValue(m, prefix+name, aty)\n\t\tif err != nil {\n\t\t\treturn cty.DynamicVal, err\n\t\t}\n\t\tvals[name] = val\n\t}\n\treturn cty.ObjectVal(vals), nil\n}\n\nfunc hcl2ValueFromFlatmapTuple(m map[string]string, prefix string, etys []cty.Type) (cty.Value, error) {","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/configs/hcl2shim/flatmap.go#L182-L218","documentation":"Returned by hcl2ValueFromFlatmapPrimitive when the raw string stored in the flatmap cannot be converted to the declared attribute type via cty's convert.Convert. The shim first wraps the raw string as cty.StringVal then attempts a conversion; if conversion fails (e.g. the string is not parseable as the target number/bool/list), it reports the offending state key plus the underlying conversion error. As the comment notes, this only triggers for tampered/corrupt state — well-formed state always converts cleanly.","triggerScenarios":"Manually editing a state file and inserting a non-numeric string for an attribute whose schema type is number/bool; downgrading a provider whose new schema is stricter than the value persisted in state; restore of a state backup whose values were transformed by an external tool.","commonSituations":"Hand-edited .tfstate files; partial state corruption from disk failure or merge conflicts in remote state; switching a schema attribute from string to a stricter type while old state still holds incompatible values.","solutions":["Inspect the state key named in the error and the conversion error message — the value is malformed for the declared type.","Restore the attribute from a known-good state backup rather than editing it by hand.","If the schema type legitimately changed, write a state upgrader that migrates the value before the new schema is applied.","Avoid manual edits to .tfstate; use 'terraform state' subcommands or import flows instead."],"exampleFix":"// not a code fix — operational recovery\n// before: state contains \"size\": \"not-a-number\" for a number-typed attribute\n// after: restore from backup, or run a state upgrader that casts the value","handlingStrategy":"validation","validationCode":"// Caller-side: verify the raw flatmap string coerces to the target type before invoking the shim.\nfunc canConvertFlatmapString(raw string, ty cty.Type) bool {\n    _, err := convert.Convert(cty.StringVal(raw), ty)\n    return err == nil\n}","typeGuard":null,"tryCatchPattern":"if val, err := hcl2shim.HCL2ValueFromFlatmap(m, key, ty); err != nil {\n    // Distinguish corrupt state from a real bug: re-decode as string and surface\n    // a friendlier 'state value at <key> is malformed' message.\n    log.Printf(\"state decode failed for %q: %v; falling back to manual repair\", key, err)\n    return repairOrAbort(key, err)\n}","preventionTips":["Never hand-edit .tfstate files; use 'terraform state' subcommands instead.","Take state backups before provider upgrades that tighten attribute types.","Write a state upgrader when changing an attribute type so old state values are coerced before the new schema is applied."],"tags":["flatmap","hcl2shim","state-corruption","type-conversion","go"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}