title: Creating a variable number of methods and resources within an API Gateway resource
body: <p>I currently use a Lambda function that uses API Gateway as its trigger, and the methods and endpoints within API Gateway are created manually. I'm trying to move this over to create it automatically with Terraform. I'm pretty new to Terraform though and I'm having trouble figuring out how to create a looping structure since the number of methods and endpoints can be different for each lambda function.</p>
  
  <p>I've read up on templates and interpolation, but because what I need has 2 layers, I can't really piece together what the best solution would be</p>
  
  <p>This is an example of what I may go off of when manually creating the gateway resources. Assume the number of endpoints can vary and the number of methods within each endpoint can also vary.</p>
  
  <pre><code>{
    "api_name" : "branches",
    "api_description" : "Branches API",
    "endpoints" : [
      {
        "name" : "branch1",
        "path" : "branch1/Retail/GetData",
       "methods" : [
          {
            "type" : "get",
            "folder" : "branches",
            "handler" : "index.handler",
            "authenticator": false
          }
        ]
      },
      {
        "name" : "branch2",
        "path" : "branch2/Retail/GetData",
        "methods" : [
          {
            "type" : "get",
            "folder" : "branches",
            "handler" : "index.get_handler",
            "authenticator": false
          },
          {
            "type" : "post",
            "folder" : "branches",
            "handler" : "index.post_handler",
            "authenticator": false
          }
        ]
      }
    ]
  } 
  </code></pre>
  
  <p>I'd like the end result to produce something like this:</p>
  
  <pre><code>resource "aws_api_gateway_rest_api" "LambdaTrigger" {
    name        = "${var.API_NAME}"
    description = "${var.API_DESCRIPTION}"
  }
  
  resource "aws_api_gateway_resource" "Resource1" {
    rest_api_id = "${aws_api_gateway_rest_api.LambdaTrigger.id}"
    parent_id   = "${aws_api_gateway_rest_api.LambdaTrigger.root_resource_id}"
    path_part   = "GetData"
  }
  
  resource "aws_api_gateway_method" "Method1" {
    rest_api_id   = "${aws_api_gateway_rest_api.LambdaTrigger.id}"
    resource_id   = "${aws_api_gateway_resource.Resource1.id}"
    http_method   = "GET"
    authorization = "NONE"
  }
  
  resource "aws_api_gateway_resource" "Resource2" {
    rest_api_id = "${aws_api_gateway_rest_api.LambdaTrigger.id}"
    parent_id   = "${aws_api_gateway_rest_api.LambdaTrigger.root_resource_id}"
    path_part   = "GetData"
  }
  
  resource "aws_api_gateway_method" "Method2" {
    rest_api_id   = "${aws_api_gateway_rest_api.LambdaTrigger.id}"
    resource_id   = "${aws_api_gateway_resource.Resource2.id}"
    http_method   = "GET"
    authorization = "NONE"
  }
  resource "aws_api_gateway_method" "Method2" {
    rest_api_id   = "${aws_api_gateway_rest_api.LambdaTrigger.id}"
    resource_id   = "${aws_api_gateway_resource.Resource2.id}"
    http_method   = "POST"
    authorization = "NONE"
  }
  </code></pre>