{"record":{"id":"eb507710e600047a","repo":"hashicorp/terraform","slug":"key-can-not-start-and-end-with","errorCode":null,"errorMessage":"key can not start and end with '/'","messagePattern":"key can not start and end with '/'","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/backend/remote-state/cos/backend.go","lineNumber":148,"sourceCode":"\t\t\t\tType:        schema.TypeString,\n\t\t\t\tOptional:    true,\n\t\t\t\tDescription: \"The directory for saving the state file in bucket\",\n\t\t\t\tValidateFunc: func(v interface{}, s string) ([]string, []error) {\n\t\t\t\t\tprefix := v.(string)\n\t\t\t\t\tif strings.HasPrefix(prefix, \"/\") || strings.HasPrefix(prefix, \"./\") {\n\t\t\t\t\t\treturn nil, []error{fmt.Errorf(\"prefix must not start with '/' or './'\")}\n\t\t\t\t\t}\n\t\t\t\t\treturn nil, nil\n\t\t\t\t},\n\t\t\t},\n\t\t\t\"key\": {\n\t\t\t\tType:        schema.TypeString,\n\t\t\t\tOptional:    true,\n\t\t\t\tDescription: \"The path for saving the state file in bucket\",\n\t\t\t\tDefault:     \"terraform.tfstate\",\n\t\t\t\tValidateFunc: func(v interface{}, s string) ([]string, []error) {\n\t\t\t\t\tif strings.HasPrefix(v.(string), \"/\") || strings.HasSuffix(v.(string), \"/\") {\n\t\t\t\t\t\treturn nil, []error{fmt.Errorf(\"key can not start and end with '/'\")}\n\t\t\t\t\t}\n\t\t\t\t\treturn nil, nil\n\t\t\t\t},\n\t\t\t},\n\t\t\t\"encrypt\": {\n\t\t\t\tType:        schema.TypeBool,\n\t\t\t\tOptional:    true,\n\t\t\t\tDescription: \"Whether to enable server side encryption of the state file\",\n\t\t\t\tDefault:     true,\n\t\t\t},\n\t\t\t\"acl\": {\n\t\t\t\tType:        schema.TypeString,\n\t\t\t\tOptional:    true,\n\t\t\t\tDescription: \"Object ACL to be applied to the state file\",\n\t\t\t\tDefault:     \"private\",\n\t\t\t\tValidateFunc: func(v interface{}, s string) ([]string, []error) {\n\t\t\t\t\tvalue := v.(string)\n\t\t\t\t\tif value != \"private\" && value != \"public-read\" {","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/backend/remote-state/cos/backend.go#L130-L166","documentation":"ValidateFunc for the cos backend's `key` attribute (cos/backend.go:148). Despite the message wording ('start and end'), the code rejects a key that starts OR ends with '/'. Such keys would be directory markers or root-relative and are invalid as the state object key.","triggerScenarios":"Setting key = \"/terraform.tfstate\" or key = \"env/\" in the backend block; the ValidateFunc errors on HasPrefix(\"/\") or HasSuffix(\"/\").","commonSituations":"Pasting a leading slash from a URL; a trailing-slash typo; confusing the key with the prefix.","solutions":["Ensure key has neither a leading nor trailing slash, e.g. key = \"terraform.tfstate\".","Use the default key (terraform.tfstate) when in doubt.","Put directory structure in prefix, not in key."],"exampleFix":"// before\nterraform {\n  backend \"cos\" {\n    key = \"/terraform.tfstate\"\n  }\n}\n// after\nterraform {\n  backend \"cos\" {\n    key = \"terraform.tfstate\"\n  }\n}","handlingStrategy":"validation","validationCode":"// Validate the cos key before passing it to the backend\nfunc validKey(k string) error {\n    if strings.HasPrefix(k, \"/\") || strings.HasSuffix(k, \"/\") {\n        return fmt.Errorf(\"key must not start or end with '/'\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep the state object key simple, e.g. 'terraform.tfstate'.","Put directory structure in prefix, not in key.","Avoid trailing slashes when templating keys."],"tags":["cos","tencentcloud","validation","config","key"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}