Query for multiple values using a single data source block

Hey all, I have a (hopefully) simple use case where I’d like to query for N static IPs in my AWS account. However, it looks like the data block:

data "aws_eip" "static_ips" {
    tags = {
        environment = "production"
    }
}

occurs in an error when there is more than one EIP with that tag:

Error: multiple EC2 EIPs matched; use additional constraints to reduce matches to a single EC2 EIP

However, this is exactly what I want. Is there a way to do this using the data block or maybe another way?

Turns out there’s another data source: aws_eip*s*. Using that solved my issue.

Thanks for sharing your solution, @oxcug .

There are broadly two use-cases for data sources that each require different behavior, and so providers typically model them differently:

  1. This module depends on a single object that was defined elsewhere. In this case the data block is representing an assertion that this other object must exist along with returning the data about it so that the rest of the module can use it.
  2. This module needs to declare a set of objects that each correlate with one existing object in the remote system but we don’t know statically how many already exists. In this case the data block represents a query for the full set of matching objects, and so the result is a collection of values.

Both of these are important in different situations. The convention is to use a singular name (which often matches the name of a similar managed resource type) for the first situation and a plural name for the second, so that it’s clearer to a reader of the module what sort of result and guarantee the data block is representing.