2.7 KiB
Custom provider resources
Sometimes you want to add custom, provider related resources to your service to use provider specific functionality
which is not yet available through events or plugins. Serverless has you covered and enables you a convenient way to add
those resources with the help of resources section in the serverless.yaml
file.
Adding custom provider resources
Serverless uses the services resources object as a place to store all the provider specific resources like compiled
functions or events.
After initialization, Serverless will try to load the resources object from the
serverless.yaml file into memory.
It will create an own, empty one if it doesn't exist.
You can use this place to add custom provider resources by writing the resource definition in YAML syntax inside the
resources object.
# serverless.yaml
resources:
Resources:
CustomProviderResource:
Type: ResourceType
Properties:
Key: Value
Referencing an external .json file
Sometimes it's hard to translate the provider specific resources into valid YAML syntax. Furthermore the resource code
might be verbose and make the serverless.yaml file bloated.
You can always use JSON-Ref to reference external .json files. This way you can organize your resource related code
into an own .json file and reference it from within serverless.yaml
like this:
resources:
Resources:
$ref: ./cloudformation-resources.json
The corresponding resources which are defined inside the cloudformation-resources.json file will be resolved and loaded
into the Resources section.
How custom provider resources are added
On deployment Serverless will load the base stack template and merge the custom resources you've defined in the resources
section of the service alongside the compiled function and corresponding event resources into it.
After that the template (with all merged resources) will be deployed on the providers infrastructure.
Conclusion
The resources section inside the serverless.yaml file is a place
where you can add custom, provider specific resource definitions which should be created on service deployment.
It gives you access to the whole feature set your provider offers and makes Serverless even more extensible.
The last thing we need to learn is how we can remove our service. Let's take a look at this now.