How to specify what block I want to use in Terraform

I have this Code in Terraform. And I have the outputs.tf. I have assigned variables like os_family in my terraform.tfvars but whenever I do apply it’s just dropping cause of Query returned no results I think that’s because it’s trying to query both things. How Can I specify to use the Ubuntu part instead of the RHEL part if I want to install Ubuntu and vice versa for the RHEL? Cause my way is definitely not working. Thank you.

Main.tf

data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-*-${var.os_version}*amd64*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["REDACTED"]
}

data "aws_ami" "rhel" {
  most_recent = true

  filter {
    name   = "name"
    values = ["RHEL-${var.os_version}*GA*x86_64*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["REDACTED"]
}

outputs.tf

output "image_id" {
  value = var.os_family == "rhel" ? data.aws_ami.rhel.id : data.aws_ami.ubuntu.id
}