pytest-serverless
pytest-serverless copied to clipboard
Add support for dynamically defined resource names
The Problem
Sometimes you want to dynamically define resources and environment variables in your serverless.yml file (i.e. using Fn::Join or Fn::Transform). This may get parsed as a python dict instead of a string, which breaks the current logic of this package (TypeError: str expected, not dict).
Proposed Solution
When parsing the serverless.yml file, transform certain properties (like the resource name) from a dict to a string. My solution simply strips invalid characters from the string, so you'd get a resource name like FnJoin-my-microservicetable. In my opinion it's not all that important what the transformed string becomes, since you'll want to pass that in as an environment variable anyway if your code needs to know it (see the unit tests I added for an example).
Disclaimer
I realize this could be a bit of a rabbit hole... there are all kinds of ways to define things in serverless/cloudformation/yaml, and those things aren't necessarily just the resource name (which is the only thing I've added here). That said, I don't think this hurts anything, and I'd rather the package supports some of these use cases than none at all