Search code examples
terraform

When to implement datasource returning only one resource in terraform provider?


I am currently implementing a Terraform provider and I have some questions about Datasources.

A datasource can return either one resource or and array of resources. In my case for all of my resources, I implemented a datasource that returns an array of resources.

I am trying to understand when it is needed to have a datasource returning only one resource.


Solution

  • I am trying to understand when it is needed to have a datasource returning only one resource.

    I think this is a question for the people that will use your provider - try to understand what are the use cases, i.e. how people will use it.

    But in doubt, why not both? This gives users flexibility and allows them to choose the approach that best fits their needs.

    Finally, maybe you can take some ideas from some existing providers such as AWS?

    AWS provider - data sources

    As you can see, there are data sources for a single resource (e.g. aws_ec2_host) but also for single/multiple resources of the same type (e.g. aws_availability_zone and aws_availability_zones)