如何在 Terraform 中输出一种类型的所有资源? [英] How to output all the resources of one type in Terraform?

查看:8
本文介绍了如何在 Terraform 中输出一种类型的所有资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Terraform 代码中定义了一堆 aws_ecr_repositories:

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"
}

我希望能够有一个可以将所有 aws_ecr_repository 资源列出到一个输出中的输出.这是我尝试过的:

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}]"
}

这不起作用,因为 Terraform 似乎不允许在资源名称上使用通配符.有可能有这样的输出吗?我目前的解决方案是只列出定义的每个资源的输出.

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.

推荐答案

Terraform 的 splat 语法 用于使用 count 元参数.

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

如果您希望能够获取所有存储库 URL,您可以拥有一个 aws_ecr_repository 资源并使用 count 元参数,如下所示:

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}]"
}

这篇关于如何在 Terraform 中输出一种类型的所有资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆