I'm creating a map object by looping through a list of elements. How do I enforce unique keys in the map such that there are no duplicates?
For example, I have something like below but I get the error: │ Local value local.uri_bucket_map cannot use its own result as part of its expression.
locals {
inference = [
{ "data_uri" : "s3://my_bucket/model.tar.gz",
"model_uri": "account_num.dkr.ecr.us-west-2.amazonaws.com/my_container:latest"
},
{ "data_uri" : "s3://my_bucket/model.tar.gz",
"model_uri": "account_num.dkr.ecr.us-west-2.amazonaws.com/my_container2:latest"
},
{ "data_uri" : "s3://my_bucket_3/model.tar.gz" ,
"model_uri": "account_num.dkr.ecr.us-west-2.amazonaws.com/my_container3:latest"
}
]
uri_bucket_map = {
for x in local.inference : x.data_uri =>
split("/", split("//", x.data_uri)[1])[0]
}
}
Expected Map:
{
"s3://my_bucket/model.tar.gz" -> "my_bucket"
"s3://my_bucket_3/model.tar.gz" -> "my_bucket_3"
}
CodePudding user response:
Without much changes to you original code, you can obtain what you want as follows:
uri_bucket_map = {
for x in distinct(local.inference[*].data_uri) : x =>
split("/", split("//", x)[1])[0]
}