'How to loop through nested maps in Terraform?

I am creating hierarchical endpoints in AWS api gateway and want to loop through my local variable to create endpoints in api-gateway. My local variable looks like this:

{
  "api" = {
    "v1" = [
      {
        "method" = "GET"
        "name" = "route_1"
        "uri" = "https://test-api.example.com/api/v1/route_1"
      },
      {
        "method" = "GET"
        "name" = "route_2"
        "uri" = "https://test-api.example.com/api/v1/route_2"
      },
    ]
  }
}

What I want to achieve is to have a nice tree like this in api-gateway.

enter image description here

I am new to terraform. Is there a simple way to acheive this? Here's my example Terraform code.

resource "aws_api_gateway_rest_api" "api_test" {
  name = "qa-api"

  endpoint_configuration {
    types = [ "REGIONAL" ]
  }
}

resource "aws_api_gateway_resource" "api" {
  parent_id   = aws_api_gateway_rest_api.api_test.root_resource_id
  path_part   = "api"
  rest_api_id = aws_api_gateway_rest_api.api_test.id
}

resource "aws_api_gateway_method" "MyDemoMethod" {
  rest_api_id   = aws_api_gateway_rest_api.api_test.id
  resource_id   = aws_api_gateway_resource.api.id
  http_method   = "GET"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "MyDemoIntegration" {
  rest_api_id          = aws_api_gateway_rest_api.api_test.id
  resource_id          = aws_api_gateway_resource.api.id
  http_method          = aws_api_gateway_method.MyDemoMethod.http_method
  type                 = "HTTP"
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source