Pass variable names when calling data sources

Hi

Is there a way to call a variable when referencing a data source?

Eg:

data "aws_ami" "win2019" {
  most_recent = true
  owners      = ["1234"]

  filter {
    name   = "name"
    values = ["win2019-desktop*"]
  }
}

data "aws_ami" "win2016" {
  most_recent = true
  owners      = ["1234"]

  filter {
    name   = "name"
    values = ["win2016-desktop*"]
  }
}
resource "aws_instance" "ec2" {
  ami           = data.aws_ami."${var.ami_version}".id

.tfvars file:

ami_version = "win2016"

Running this gives:

│ Error: Invalid attribute name
│ 
│   on server.tf line 3, in resource "aws_instance" "ec2":
│    3:   ami           = data.aws_ami."${var.ami_version}".id

│ An attribute name is required after a dot.

Resource addresses cannot use interpolation, but you can index into an expanded resource (one using count or for_each). For example (untested):

variable "ami_name_filters" {
  type = map(set(string))
  default = {
    "win2019" = ["win2019-desktop*"],
    "win2016" = ["win2016-desktop*"],
  }
}

data "aws_ami" "amis" {
  for_each = var.ami_name_filters
  most_recent = true
  owners      = ["1234"]

  filter {
    name   = "name"
    values = each.value
  }
}
resource "aws_instance" "ec2" {
  ami           = data.aws_ami.amis[var.ami_version].id