title: Dynamic data source for fetching AMI using Terraform
body: <p>So the following data source will fetch the latest AMI with <code>component:web</code> tagged. Let's say I have a handful of components with their own AMI. Instead of creating this same block 5 times, is there a way to make this more dynamic where I can pass in the value of <code>component</code>? I can't seem to think of a unique value I can pass in. Do I need to refactor my code a bit?</p>
  
  <pre><code>data "aws_ami" "web" {
    filter {
      name   = "state"
      values = ["available"]
    }
  
    filter {
      name   = "tag:component"
      values = ["web"]
    }
  
    most_recent = true
  }
  </code></pre>
  
  <p>I have a <code>defaults</code> module that acts as a metadata lookup where it fetches and outputs basic things like AMI ID and VPC IDs.</p>
  
  <h1>Default Module</h1>
  
  <pre><code># defaults/main.tf
  
  data "aws_ami" "web" {
    filter {
      name   = "state"
      values = ["available"]
    }
  
    filter {
      name   = "tag:component"
      values = ["web"]
    }
  
    most_recent = true
  }
  
  output "web_ami" {
    value = "${data.aws_ami.web.id}"
  }
  </code></pre>
  
  <h1>Main code</h1>
  
  <pre><code># service_name/main.tf
  
  module "defaults" {
    source      = "../defaults"
    region      = "${var.region}"
    environment = "${var.environment}"
  }
  
  module "ftpserver" {
    source .    = "../ec2_instance"
    ami_id      = "${module.defaults.web_ami}"
    ...
  }
  </code></pre>