What version of the serverless framework are you using? I just tried again on a project running v1.74.1 and it works as advertised.
If you copied and pasted this into VS Code then try deleted it and then type them in by hand, sometimes it copies HTML code gets copied but VS Code doesn’t show it but when the file gets processed it messes things up.
Also, check indentation. Failing that, share your serverless.yml
in a gist and I can take a look for you.