【问题标题】:how to filter the associated security groups of an ec2 instance using terraform如何使用 terraform 过滤 ec2 实例的关联安全组
【发布时间】:2018-11-28 16:55:18
【问题描述】:

我正在尝试获取与 ID 为“i-0abcdefgh1234”的实例关联的安全组,但输出没有给出任何结果。

terraform.tf

data "aws_instance" "ec2" {
     instance_id = "i-0abcdefgh1234"

     filter {
        name = "tag:Name"
        values = ["name-of-the-server"]
      }
    }

    output "sg" {
       value = "${data.aws_instance.ec2.*.security_groups}"
    }

输出

data.aws_instance.ec2: Refreshing state...

------------------------------------------------------------------------

No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.

在下面带和不带 * 的尝试

value = "${data.aws_instance.ec2.*.security_groups}"

【问题讨论】:

  • 只是一个旁注:指定实例 ID 和其他过滤器是多余的,因为实例 ID 是唯一的。

标签: terraform aws-security-group


【解决方案1】:

数据源的插值语法是data.TYPE.NAME.ATTRIBUTE。见this

在您的情况下,它将是 ${data.aws_instance.ec2.security_groups}

但是,正如documentation 所述 - “某些值并非始终设置,并且可能无法用于插值。”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2021-04-06
    • 2023-03-29
    • 2020-11-10
    • 2016-04-25
    相关资源
    最近更新 更多