amazon-web-servicesterraform

How to output all the resources of one type in Terraform?


I have a bunch of aws_ecr_repositories defined in my Terraform code:

resource "aws_ecr_repository" "nginx_images" {
  name = "nginx-test"
}

resource "aws_ecr_repository" "oracle_images" {
  name = "oracle-test"
}

I want to be able to have an output that can list all the aws_ecr_repository resources into one output. This is what I tried:

output "ecr_repository_urls" {
  value = "[${aws_ecr_repository.*.repository_url}]"
}

This does not work because Terraform does not seem to allow wildcards on the resource names. Is it possible to have an output like this? My current solution is to just list outputs for every resource defined.


Solution

  • Terraform's splat syntax is for keeping track of each thing created by a resource using the count meta parameter.

    If you want to be able to get at all of the respoitory URLs you could have a single aws_ecr_repository resource and use the count meta parameter with something like this:

    variable "images" {
      default = [
        "nginx-test",
        "oracle-test",
      ]
    }
    
    resource "aws_ecr_repository" "images" {
      count = length(var.images)
      name  = var.images[count.index]
    }
    
    output "ecr_repository_urls" {
      value = "[${aws_ecr_repository.images.*.repository_url}]"
    }