YAML file iteration with Terraform

I have a yaml file like below -

nervecenter-kafka-apps:

  • id: AA00003191
    region: east1
    email: shp@example.com
    clientType: java
    orgname: cg-tso
    repoName: kafka-async-api
    targetBranchName: main
    userInitials: SHP
  • id: BB00000015
    region: east1
    email: shp@exampledotcom
  • id: BB00009902
    region: east1
    email: mao@exampledotcom
  • id: BB00009903
    region: east1
    email: mao@exampledotcom

I am trying to write tf code to get “id” which have “clientType” as non java. If the field 'clientType" is not there , it should take that as non-java app. “clientType” can have comma separated value as well.

locals {

consolidated_app_ids    = yamldecode(textdecodebase64(var.consolidated_app_id_file_content, "UTF-8"))

[
atm_id = [for atmid in (local.consolidated_app_ids[“nervecenter-kafka-apps”]):[
for key in atmid:
ct == atmid.clientType
]
]
}

output “non_java_atmid” {
value = local.atm_id
}

I tried with above code.
Please help on how to fix this issue. I am fairly new to terraform

with below code -

locals {

consolidated_app_ids    = yamldecode(textdecodebase64(var.consolidated_app_id_file_content, "UTF-8")

atm_id = [for atmid in (local.consolidated_app_ids[“nervecenter-kafka-apps”]): [
for clnt in atmid:[
clnt
]
]

]

}

output “non_java_atmid” {
value = local.atm_id
}

I get below output -

  • non_java_atmid = [

+ [

+ “java”,

+ " shp@example.com",

+ “AA00003191”,

+ “cg-tso”,

+ “east1”,

+ “kafka-async-api”,

+ “main”,

+ “SHP”,

],

+ [

+ “shp@exampledotcom”,

+ “BB00000015”,

+ “east1”,

],

+ [

+ “mao@exampledotcom”,

+ “BB00009902”,

+ “east1”,

],

+ [

+ “mao@exampledotcom”,

+ “BB00009903”,

+ “east1”,

],

]

Fixed the issue with below code. Explanation as below-

Issue-:

iterate over above given yaml file and output those atmid which are not java based

solution-:

loaded the base64 encoded file with yamldecode. This gives list of map as output. Loop over list of map and used lookup function to find “clientType” key and compared it to “java”.

code-:

locals {

consolidated_app_ids    = yamldecode(textdecodebase64(var.consolidated_app_id_file_content, "UTF-8"))

atm_id = flatten([for atmid in (local.consolidated_app_ids["nervecenter-kafka-apps"]): [

    lookup(atmid,"clientType",null) != "java" ? atmid : null
]])

}

output “non_java_atmid” {
value = local.atm_id
}

Line -:

atm_id = [for atmid in (local.consolidated_app_ids[“nervecenter-kafka-apps”]):

gives below output-:

  • non_java_atmid = [

+ {

+ clientType = “java”

+ email = “shp@example.com

+ id = “AA00003191”

+ orgname = “cg-tso”

+ region = “east1”

+ repoName = “kafka-async-api”

+ targetBranchName = “main”

+ userInitials = “SHP”

},

+ {

+ email = “shp@example.com

+ id = “BB00000015”

+ region = “east1”

},

+ {

+ email = “mao@capgroupdotcom”

+ id = “BB00009902”

+ region = “east1”

},

+ {

+ email = “mao@capgroupdotcom”

+ id = “BB00009903”

+ region = “east1”

},

]

lookup function searches for “clientType” key from above map and compares it “java”.

Further , I am using below code to get service account id number for all “id” coming in above output-

data “confluent_service_account” “get_atmid_sa_id” {
for_each = {for s in local.atm_id: s.id => s if s != null}
display_name = each.value.id

}

output “sa_id” {
value = data.confluent_service_account.get_atmid_sa_id
}

Hope this helps for someone