Build list of maps with multiple levels

Viewed 128

This snippet shows almost exactly what I'm trying to build. Except in my case I'm not setting an aws_lb's list of subnet_mapping, but rather an aws_cloudfront_distribution's list of origin. Specifically, note that origin contains custom_origin_config so it's a list of maps, where the map's value is a key to another map.

I tried doing this:

resource "aws_cloudfront_distribution" "foo" { 
    ...
    origin = "${null_resource.origins.*.triggers}"    
    ...
}

resource "null_resource" "origins" {
  count = "${length(var.origin_ids)}"
  triggers {
    domain_name = "${element(var.origin_domains, count.index)}"
    origin_id   = "${element(var.origin_ids, count.index)}"

    custom_origin_config {
      http_port              = "${local.http_port}"
      https_port             = "${local.https_port}"
      origin_protocol_policy = "http-only"
      origin_ssl_protocols   = "${local.origin_ssl_protocols}"
    }
}

but I'm getting errors with:

... got unconvertible type '[]map[string]interface {}'

1 Answers

The null_resource triggers only excepts arbitrary strings so a map is being rejected. Seems like a null_resource is overkill since your custom_origin_config isn't dependent on the count.index.

Is there any reason you can't simply do this?

resource "aws_cloudfront_distribution" "foo" {
  count = length(local.origin_ids)

  origin {
    domain_name = var.origin_domains[count.index]
    origin_id   = var.origin_ids[count.index]

    custom_origin_config {
      http_port  = local.http_port
      https_port = local.https_port

      origin_protocol_policy = "http-only"
      origin_ssl_protocols   = local.origin_ssl_protocols
    }
  }
}
Related