How to map values by output values

Hello, I have a resource aws_kms_key which has been created through for_each expression, now I need to locate its arn to use with ecr repository, can you help me how to do that?

Here is the output it self:

  + test1 = {
      + KMS Key For - Cluster Loggroup   = "arn:aws:kms:eu-west-1:771664210945:key/044c6d77-df44-4b47-a51e-17b4c4561319"
      + KMS Key For - Container Registry = "arn:aws:kms:eu-west-1:771664210945:key/e0d919d7-137c-46ec-b99c-07e7ad07af3a"
      + KMS Key For - Flowlog Loggroup   = "arn:aws:kms:eu-west-1:771664210945:key/184fbca3-2845-4b72-bd64-b4d167610b1c"
    }

Or, I’ve also have this kind of output

  + test2 = {
      + alias/dev/cluster  = "arn:aws:kms:eu-west-1:771664210945:key/044c6d77-df44-4b47-a51e-17b4c4561319"
      + alias/dev/flowlogs = "arn:aws:kms:eu-west-1:771664210945:key/184fbca3-2845-4b72-bd64-b4d167610b1c"
      + alias/dev/registry = "arn:aws:kms:eu-west-1:771664210945:key/e0d919d7-137c-46ec-b99c-07e7ad07af3a"
    }

output "key"                         {
   description = "The 'Amazon Resource Name (ARN)' of key"
   value       = zipmap( 
       values(aws_kms_key.global)[*].description, values(aws_kms_key.global)[*].arn
   )
}

So how to grep for example the Container Registry, and make something like:

module.kms.arn.container

Hi @unity-unity,
as the data structure is a map, it should be possible to use the key and get the appropriate value.

1 Like

Hi, @tbugfinder can you show an example?

Hi @unity-unity ,
I hope I understood it properly:

locals {
  test1 = {
    "KMS Key For - Cluster Loggroup"   = "arn:aws:kms:eu-west-1:771664210945:key/044c6d77-df44-4b47-a51e-17b4c4561319"
    "KMS Key For - Container Registry" = "arn:aws:kms:eu-west-1:771664210945:key/e0d919d7-137c-46ec-b99c-07e7ad07af3a"
    "KMS Key For - Flowlog Loggroup"   = "arn:aws:kms:eu-west-1:771664210945:key/184fbca3-2845-4b72-bd64-b4d167610b1c"
  }
  test2 = {
      "alias/dev/cluster"  = "arn:aws:kms:eu-west-1:771664210945:key/044c6d77-df44-4b47-a51e-17b4c4561319"
      "alias/dev/flowlogs" = "arn:aws:kms:eu-west-1:771664210945:key/184fbca3-2845-4b72-bd64-b4d167610b1c"
      "alias/dev/registry" = "arn:aws:kms:eu-west-1:771664210945:key/e0d919d7-137c-46ec-b99c-07e7ad07af3a"
    }
}


output "containerregistry1" {
  value = local.test1["KMS Key For - Container Registry"]
}

output "containerregistry2" {
  value = local.test2["alias/dev/registry"]
}

Result:

Outputs:

containerregistry1 = "arn:aws:kms:eu-west-1:771664210945:key/e0d919d7-137c-46ec-b99c-07e7ad07af3a"
containerregistry2 = "arn:aws:kms:eu-west-1:771664210945:key/e0d919d7-137c-46ec-b99c-07e7ad07af3a"
1 Like

@tbugfinder Thank you, again perfectly done