Filter AWS AMI based on a map of tag names and values

I’m trying to create an aws_ami data source that fetches the latest AMI based on a few tags. The catch is that I want to do it with a map of tags and their values, not by defining filters for each specific tag in the data source

Example:

module-vars.tf

variable "filter-tags" {
  type = "map"
  default = {
    "java_vendor" = "oracle"
  }
}

module.tf

data "aws_ami" "aws-ami" {
  most_recent = true
  owners = ["self"]

  // Filter code here
  // e.g. FICTIONAL CODE, DON'T USE
  filter {
    name = "tags:${var.filter-tags}"
  }
}

So obviously this filter-tags variable should be able to change and the filtered AMI should have all the tags matching.

Any ideas?

Found a way

data "aws_ami" "aws-ami" {
  most_recent = true
  owners = ["self"]

  dynamic "filter" {
    for_each = var.filter-tags
    iterator = tag
    content {
      name = "tag:${tag.key}"
      values = ["${tag.value}"]
    }
  }
}