diff --git a/backend/remote-state/s3/backend_state.go b/backend/remote-state/s3/backend_state.go index f38b199b04..25b68d9617 100644 --- a/backend/remote-state/s3/backend_state.go +++ b/backend/remote-state/s3/backend_state.go @@ -39,9 +39,13 @@ func (b *Backend) States() ([]string, error) { // extract the env name from the S3 key func (b *Backend) keyEnv(key string) string { - // we have 3 parts, the prefix, the env name, and the key name - parts := strings.SplitN(key, "/", 3) - if len(parts) < 3 { + if b.workspaceKeyPrefix == "" { + parts := strings.Split(key, "/") + return parts[0] + } + parts := strings.SplitAfter(key, b.workspaceKeyPrefix) + + if len(parts) < 2 { // no env here return "" } @@ -51,6 +55,12 @@ func (b *Backend) keyEnv(key string) string { return "" } + parts = strings.Split(parts[1], "/") + + if len(parts) < 3 { + return "" + } + // not our key, so don't include it in our listing if parts[2] != b.keyName { return ""