首页 > 解决方案 > 如何使用 terraform 过滤 ec2 实例的关联安全组

问题描述

我正在尝试获取与 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}"

标签: terraformaws-security-group

解决方案


数据源的插值语法是data.TYPE.NAME.ATTRIBUTE. 看到这个

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

但是,正如文档所述 - “某些值并不总是设置并且可能无法用于插值。”


推荐阅读