【问题标题】:Add a Security Group to the Inbound Rule of another Security Group as a Source with Terraform (AWS)使用 Terraform (AWS) 将安全组添加到另一个安全组的入站规则作为源
【发布时间】:2021-08-16 02:56:44
【问题描述】:

我无法将安全组“sg0”添加到另一个安全组“sg1”的入站规则作为 Terraform 的源。 (我用Terraform v0.15.4

这是我试过的代码:

resource "aws_security_group" "sg0" {
    ..........
}

resource "aws_security_group" "sg1" {
    ..........

    ingress {
      from_port        = 5432
      to_port          = 5432
      security_groups  = [aws_security_group.sg0]
      protocol         = "tcp"
    }
    ..........
}

但我收到以下错误:

Error: Incorrect attribute value type
│ 
│   on main.tf line 235, in resource "aws_security_group" "sg1":
│  235:       security_groups  = [aws_security_group.sg0]
│     ├────────────────
│     │ aws_security_group.sg0 is object with 13 attributes
│ 
│ Inappropriate value for attribute "security_groups": element 0: string required.

我想获得与以下我在没有 Terraform 的情况下手动完成的结果相同的结果。我该怎么做?

【问题讨论】:

    标签: amazon-web-services terraform terraform-provider-aws aws-security-group inbound-security-rule


    【解决方案1】:

    您需要将“sg0”的security group id作为源添加到“sg1”的入站规则中。因此,您只需在aws_security_group.sg0 之后添加.id,如下所示。

    resource "aws_security_group" "sg0" {
        ..........
    }
    
    resource "aws_security_group" "sg1" {
        ..........
    
        ingress {
          from_port        = 5432
          to_port          = 5432
          security_groups  = [aws_security_group.sg0.id] # Add .id here!!
          protocol         = "tcp"
        }
        ..........
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 2015-04-28
      • 1970-01-01
      • 2021-08-01
      • 2021-08-08
      相关资源
      最近更新 更多