No, there is not way to conditionally include/exclude parts of the
serverless.yml, because it's not a great idea to do so.
While I can totally understand your use-case, what you're doing may come back to bite you (and often does in larger projects and environments); Since your dev/test environment does not represent your prod environment, it's conceivable your application will works in dev/test, but have a bug in prod.
That being said (and I feel dirty suggesting this ), you could try to use CFN Conditionals to do what you've described. Remember that the
resources section of your
serverless.yml is merged with the final generated CFN template, so by adding in
Conditions to your resources you might be able to do it (e.g. have two function definitions, one for dev/test that is VPC'd, and one for prod that is not).