title: Multiple availability zones with terraform on AWS
body: <p>The VPC I'm working on has 3 logical tiers: Web, App and DB. For each tier there is one subnet in each availability zone. Total of 6 subnets in the region I'm using.</p>
  
  <p>I'm trying to create EC2 instances using a module and the <code>count</code> parameter but I don't know how to tell terraform to use the two subnets of the App tier. An additional constraint I have is to use static IP addresses (or a way to have a deterministic private name)</p>
  
  <p>I'm playing around with the resource</p>
  
  <pre><code>resource "aws_instance" "app_server" {
    ...
    count = "${var.app_servers_count}"
  
    # Not all at the same time, though!
    availability_zone = ...
    subnet_id = ...
    private_ip = ...
  }
  </code></pre>
  
  <p>Things I've tried/thought so far:</p>
  
  <ul>
  <li>Use <code>data "aws_subnet" "all_app_subnets" {...}</code>, filter by name, get all the subnets that match and use them as a list. But <code>aws_subnet</code> cannot return a list;</li>
  <li>Use <code>data "aws_availability_zones" {...}</code> to find all the zones. But I still have the problem of assigning the correct subnet;</li>
  <li>Use <code>data "aws_subnet_ids" {...}</code> which looks like the best option. But apparently it doesn't have a filter option to match the networks namel</li>
  <li>Pass the subnets IDs as list of strings to the module. But I don't want to hard code the IDs, it's not automation;</li>
  <li>Hard code the subnets as <code>data "aws_subnet" "app_subnet_1" {...}</code>, <code>data "aws_subnet" "app_subnet_2" {...}</code> but then I have to use separate sets of variables for each subnet which I don't like;</li>
  <li>Get information for each subnet like in the point above but then create a <code>map</code> to access it as a list. But it's not possibile to use interpolation in variables definition;</li>
  <li>Not using modules and hard-code each instance for each environment. Mmmm... really?</li>
  </ul>
  
  <p>I really ran out of ideas. It seems that nobody has to deploy instances in specific subnetworks and keep a good degree of abstration. I see only examples where subnetworks are not specified or where people just use default values for everything. Is this really something so unusual?</p>
  
  <p>Thanks in advance to everyone.</p>