{"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":"exception","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/c9def3e214014c1188faabfc4a5bde5095139765/internal/configs/hcl2shim/flatmap.go#L182-L218","documentation":"Returned by hcl2ValueFromFlatmapPrimitive (flatmap.go:200) when a raw string value read from a legacy flatmap state map cannot be converted (via cty convert.Convert) into the declared primitive type. The code reads the string, wraps it as cty.StringVal, then converts to the target type; failure means the stored string is not a valid encoding of that type — i.e. the state data is inconsistent with the schema.","triggerScenarios":"A flatmap state entry at the given key holds a string that cannot coerce to the schema's type (e.g. the schema says number but the state string is \"abc\", or the schema says bool but the value is \"maybe\"). Happens during state read/import/refresh when decoding legacy or externally-modified state.","commonSituations":"Manually editing the terraform.tfstate file and introducing a malformed value. A provider schema changed an attribute type (string→number) without a state migration. Third-party tools or older Terraform versions writing values in an unexpected format. State produced by a different provider version with a divergent schema.","solutions":["Open the state file and inspect the key named in the message; correct the value to a valid literal for the attribute's declared type.","If the schema type legitimately changed, write a state upgrader / MigrateState function to transform old values before they reach flatmap decoding.","If the value is genuinely unknown, remove the offending resource instance from state (terraform state rm) and re-import it.","Pin provider versions so the schema that wrote the state matches the schema reading it."],"exampleFix":"// before — state has: \"instance_count\": \"not-a-number\"\n// schema says: Type: cty.Number\n\n// after — correct the literal in state.json\n\"instance_count\": \"3\"","handlingStrategy":"validation","validationCode":"// Validate a flatmap primitive string before handing it to the shim\nfunc validatePrimitive(raw string, ty cty.Type) error {\n    v, err := convert.Convert(cty.StringVal(raw), ty)\n    if err != nil {\n        return fmt.Errorf(\"value %q not coercible to %s: %w\", raw, ty.FriendlyName(), err)\n    }\n    _ = v\n    return nil\n}","typeGuard":"func isCoerciblePrimitive(raw string, ty cty.Type) bool {\n    if !ty.IsPrimitiveType() {\n        return false\n    }\n    _, err := convert.Convert(cty.StringVal(raw), ty)\n    return err == nil\n}","tryCatchPattern":"val, err := hcl2shim.HCL2ValueFromFlatmap(m, key, ty)\nif err != nil {\n    // log and substitute null rather than crashing a refresh/plan\n    log.Printf(\"[WARN] state value at %s is invalid (%v); using null\", key, err)\n    return cty.NullVal(ty), nil\n}","preventionTips":["Never hand-edit terraform.tfstate values; use terraform state commands or re-apply.","When changing an attribute's type in a provider schema, always ship a state upgrader.","Pin provider versions so the schema that wrote state matches the one reading it."],"tags":["flatmap","hcl2shim","state","type-coercion","corrupt-state"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}