【问题标题】:Terraform AWS Security group self referenceTerraform AWS 安全组自我参考
【发布时间】:2020-07-26 05:19:17
【问题描述】:

我正在使用 terraform 进行 AWS 资源配置。我需要自我引用“mySG”。从 Terraform 文档中我可以使用

 ingress {
          from_port = 0
          to_port = 0
          protocol = -1
          self = true
      }

但是不同的协议呢?使用控制台 有以下可用的历史入站规则:

      Type      Protocol         PortRange      Source
1. All TCP      TCP             0-65535         mySG 
2. All UDP       UDP              0-65535         mySG 
3. Custom TCP    TCP             1856            mySG

(需要第三个条目吗?考虑所有端口的第一个条目) 上述入口规则是否会处理所有 3 个条目?如果不是,那应该是什么 terraform 语法。

【问题讨论】:

  • 将协议设置为 -1 涵盖 TCP 和 UDP。安全组是第 3 层,因此它管理的只有这两个协议。
  • @jordanm stackoverflow.com/a/61192693/154527 是您问题的有效答案,您应该接受它。
  • Kamlendra 我的以下回答是否帮助您解决了这个问题?
  • 感谢 Datise,它对我有用。甚至针对不同环境(Dev、UAT、PROD)的建模器方法也有效。谢谢!

标签: amazon-web-services aws-cli terraform-provider-aws aws-security-group aws-cloudformation-custom-resource


【解决方案1】:

您可以通过分别使用资源 aws_security_group 和 aws_security_group_rule 将 sec 组从规则中拆分出来来实现自引用组。这样做,结合你现有的 3 条规则,大致看起来像这个 terraform:

resource "aws_security_group" "sec_group" {
  name   = "sec_group"
  vpc_id = "${local.vpc_id}"
}

resource "aws_security_group_rule" "sec_group_allow_tcp" {
  type              = "ingress"
  from_port         = 0 // first part of port range 
  to_port           = 65535 // second part of port range
  protocol          = "tcp" // Protocol, could be "tcp" "udp" etc. 
  security_group_id = "${aws_security_group.sec_group.id}" // Which group to attach it to
  source_security_group_id = "${aws_security_group.sec_group.id}" // Which group to specify as source
}

resource "aws_security_group_rule" "sec_group_allow_udp" {
  type              = "ingress"
  from_port         = 0 // first part of port range 
  to_port           = 65535 // second part of port range
  protocol          = "udp" // Protocol, could be "tcp" "udp" etc. 
  security_group_id = "${aws_security_group.sec_group.id}" // Which group to attach it to
  source_security_group_id = "${aws_security_group.sec_group.id}" // Which group to specify as source
}

resource "aws_security_group_rule" "sec_group_allow_1865" {
  type              = "ingress"
  from_port         = 1865 // first part of port range 
  to_port           = 1865 // second part of port range
  protocol          = "tcp" // Protocol, could be "tcp" "udp" etc. 
  security_group_id = "${aws_security_group.sec_group.id}" // Which group to attach it to
  source_security_group_id = "${aws_security_group.sec_group.id}" // Which group to specify as source
}

请注意,该规则采用协议类型,从端口/到端口(用于范围),以及用于指定的可选 source_security_group_id

【讨论】:

  • 谢谢@Datise,这对我有用。我想再为 icmp 添加一个所有端口的示例。资源 "aws_security_group_rule" "All_all_icmp_ports_for_self_SG_all" { count = tonumber(var.groupcount) type = "ingress" from_port = -1 to_port = -1 协议 = "icmp" security_group_id = aws_security_group.internal_security_group[count.index].id source_security_group_id = aws_security_group。 internal_security_group[count.index].id }
猜你喜欢
  • 2019-07-28
  • 2021-04-06
  • 1970-01-01
  • 2021-03-25
  • 2021-04-09
  • 2018-12-13
  • 2020-08-30
  • 2018-09-21
  • 2023-03-29
相关资源
最近更新 更多