0

I have tried a couple of ways to iterate through a map in yaml but each results i keep getting errors ex: local.settings is object with # attributes .

to start with here is my yaml and locals setup

config:
    params:
        server:
            host: 127.0.0.1
            port: 8080
        media:
            name: foobar
            type: html
        s3:
            bucket: foobarbucket
            uploadFolder: folderfoobar
            type:
              public: false
        email:
            to: noreply@foobar.com
        
            

locals {
    settings = yamldecode(file("test.yaml"))
}

module "store_write" {
  source  = "cloudposse/ssm-parameter-store/aws"
  for_each = local.settings.variables

  parameter_write = [
    {
      name        = "/settings/${each.key}(i need each param key name ex: server)/${each.key.NEXTKET}(then the following key name after ex:host)"
      value       = "${each.key.value}(that keys value, ex: 127.0.0.1)"
      type        = "String"
      overwrite   = "true"
      description = "Terraform deployed param: ${local.app_name} ${each.key}"
    }
  ]

}

now of course i get an output of whats decoded from the yaml file, but every for loop i try wont work. at this point i cant even find 1 that is clean enough to paste here. i dont think i can achieve what i need IF sometimes the key names ex: s3 or email change over time right?

also notice sometimes i might get 3 levels,

  config:
    params:
         s3:
         bucket: foobarbucket
         uploadFolder: folderfoobar
         type:
           public: false

I got a bit further in trying the following and had to edit the yaml file a bit. BUT now im getting duplicates

locals {
  params = yamldecode(file("./test.yaml"))["app"]
  folder_project_apis = flatten([
    for fk, param in local.params : [
      for pk, config in param.params : [
        for lk, name in config  : [
            for vk, value in config : {
            key1  = fk
            key2 = pk
            key3 = lk
            value = value
          }
        ]
      ]
    ]
  ])
}

using new yaml structure

app:
  testing: #fk
    display_name: "Folder A"
    parent: 
    params: 
      server: #pk
        host:
          - 127.0.0.1
        port:
          - 3000
      s3:
        layout:
          - "uploads"
        image:
          - "total"

outputs

> local.folder_project_apis
[
  {
    "key1" = "testing"
    "key2" = "s3"
    "key3" = "image"
    "value" = [
      "total",
    ]
  },
  {
    "key1" = "testing"
    "key2" = "s3"
    "key3" = "image"
    "value" = [
      "uploads",
    ]
  },
  {
    "key1" = "testing"
    "key2" = "s3"
    "key3" = "layout"
    "value" = [
      "total",
    ]
  },
  {
    "key1" = "testing"
    "key2" = "s3"
    "key3" = "layout"
    "value" = [
      "uploads",
    ]
  },
  {
    "key1" = "testing"
    "key2" = "server"
    "key3" = "host"
    "value" = [
      "127.0.0.1",
    ]
  },
  {
    "key1" = "testing"
    "key2" = "server"
    "key3" = "host"
    "value" = [
      3000,
    ]
  },
  {
    "key1" = "testing"
    "key2" = "server"
    "key3" = "port"
    "value" = [
      "127.0.0.1",
    ]
  },
  {
    "key1" = "testing"
    "key2" = "server"
    "key3" = "port"
    "value" = [
      3000,
    ]
  },
]
Luis
  • 83
  • 2
  • 10

1 Answers1

1

If i understand correctly, you want to iterate through a map in the file 'test.yaml' and access objects ? You can do below.

outputs.tf

output host{
   value = local.settings.params.server.host
}

output email {
   value = local.settings.params.email
}

terraform output

email = {
  "to" = "noreply@foobar.com"
}
host = "127.0.0.1"

And you have concern that if key value is changed then you cannot access above parsing of yaml objects , yes, that is correct.

For example, if I change 's3' to 's4' in the given file and terraform apply.

  • entire key-value of s3 is removed and inserted of s4
      ~ params = {
          - s3     = {
              - bucket       = "foobarbucket"
              - uploadFolder = "folderfoobar"
            } -> null
          + s4     = {
              + bucket       = "foobarbucket"
              + uploadFolder = "folderfoobar"
            }
            # (3 unchanged elements hidden)
        }
    }
Marcin
  • 215,873
  • 14
  • 235
  • 294
My IT GURU
  • 144
  • 1
  • 7
  • see updated post , i got a bit closer but the indexes is where i am having a bit of an issue – Luis Feb 24 '22 at 15:47