{"record":{"id":"142b09162d0a0c17","repo":"hashicorp/terraform","slug":"invalid-count-value-for-q-in-state-s","errorCode":null,"errorMessage":"invalid count value for %q in state: %s","messagePattern":"invalid count value for %q in state: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/configs/hcl2shim/flatmap.go","lineNumber":237,"sourceCode":"\tvar vals []cty.Value\n\n\t// if the container is unknown, there is no count string\n\tlistName := strings.TrimRight(prefix, \".\")\n\tif m[listName] == UnknownVariableValue {\n\t\treturn cty.UnknownVal(cty.Tuple(etys)), nil\n\t}\n\n\tcountStr, exists := m[prefix+\"#\"]\n\tif !exists {\n\t\treturn cty.NullVal(cty.Tuple(etys)), nil\n\t}\n\tif countStr == UnknownVariableValue {\n\t\treturn cty.UnknownVal(cty.Tuple(etys)), nil\n\t}\n\n\tcount, err := strconv.Atoi(countStr)\n\tif err != nil {\n\t\treturn cty.DynamicVal, fmt.Errorf(\"invalid count value for %q in state: %s\", prefix, err)\n\t}\n\tif count != len(etys) {\n\t\treturn cty.DynamicVal, fmt.Errorf(\"wrong number of values for %q in state: got %d, but need %d\", prefix, count, len(etys))\n\t}\n\n\tvals = make([]cty.Value, len(etys))\n\tfor i, ety := range etys {\n\t\tkey := prefix + strconv.Itoa(i)\n\t\tval, err := hcl2ValueFromFlatmapValue(m, key, ety)\n\t\tif err != nil {\n\t\t\treturn cty.DynamicVal, err\n\t\t}\n\t\tvals[i] = val\n\t}\n\treturn cty.TupleVal(vals), nil\n}\n\nfunc hcl2ValueFromFlatmapMap(m map[string]string, prefix string, ty cty.Type) (cty.Value, error) {","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/configs/hcl2shim/flatmap.go#L219-L255","documentation":"Returned by hcl2ValueFromFlatmapTuple (flatmap.go:237) when the count marker string stored at prefix+\"#\" in the flatmap is not parseable as an integer via strconv.Atoi. In legacy flatmap, tuple/list containers store their length in a special '#'-suffixed key; a non-numeric value there means the state is corrupt or was written by incompatible code.","triggerScenarios":"Decoding a tuple-typed attribute from flatmap where the '<prefix>#' key holds a non-integer string (e.g. empty, a word, or UnknownVariableValue that bypassed the earlier guard). Triggered during state read/refresh/import when the schema declares a tuple type.","commonSituations":"Hand-edited state files where a '#'-suffixed length key was altered. A schema changed from list to tuple (or vice versa) and the count key format changed. State written by a much older/newer Terraform version with a different flatmap convention.","solutions":["Inspect the state file at the key reported in the message (it is the prefix); ensure the '#'-suffixed entry is a non-negative integer.","If the container should be empty/unknown, set the count to \"0\" or remove stale keys so the earlier guards handle it.","If the schema type changed, add a state migration to rewrite the length key before decoding.","Regenerate the state by re-applying or re-importing the resource if the flatmap is unrecoverable."],"exampleFix":"// before — state has: \"config.#\": \"three\"\n\n// after — flatmap length keys must be integers\n\"config.#\": \"3\"","handlingStrategy":"validation","validationCode":"// Verify a tuple/list count key is a valid integer before decoding\nfunc validFlatmapCount(m map[string]string, prefix string) bool {\n    s, ok := m[prefix+\"#\"]\n    if !ok || s == hcl2shim.UnknownVariableValue {\n        return true\n    }\n    n, err := strconv.Atoi(s)\n    return err == nil && n >= 0\n}","typeGuard":"func isIntegerCount(s string) bool {\n    if s == hcl2shim.UnknownVariableValue {\n        return true\n    }\n    n, err := strconv.Atoi(s)\n    return err == nil && n >= 0\n}","tryCatchPattern":null,"preventionTips":["Treat the terraform.tfstate file as machine-generated; avoid manual edits to '#'-suffixed length keys.","Run a state migration whenever a collection-typed attribute changes arity.","Pin provider versions to keep the flatmap convention stable."],"tags":["flatmap","hcl2shim","state","tuple","corrupt-state"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}