ALB 和 EC2 实例之间的 AWS 安全组

AWS security group between ALB and EC2 instances

我正在使用 Terraform 在 AWS 上配置一个 ALB,目标组由 EC2 实例组成。我尝试使用 Terraform 创建以下安全组:

1) sg-alb (SG associated to the ALB):
   ----------------------------------
   Inbound:
      HTTP with source 0.0.0.0/0
      HTTPS with source 0.0.0.0/0

   Outbound: 
      All traffic with destination 0.0.0.0/0 

  2) sg-http-alb (SG associated to the EC2 instances and should only receive traffic from the ALB):
     ----------------------------------------------------------------------------------------------
   Inbound:
      HTTP with source sg-alb
      HTTPS with source sg-alb

   Outbound: 
      All traffic with destination 0.0.0.0/0 

我读到 here 最好的做法是将出站流量限制到侦听器端口上的 instance security group 目的地。

所以我改了配置如下:

1) sg-alb (SG associated to the ALB):
   ----------------------------------
   Inbound:
      HTTP with source 0.0.0.0/0
      HTTPS with source 0.0.0.0/0

   Outbound: 
      HTTP with destination sg-http-alb (<---- this line changed)

  2) sg-http-alb (SG associated to the EC2 instances and should only receive traffic from the ALB):
     ----------------------------------------------------------------------------------------------
   Inbound:
      HTTP with source sg-alb
      HTTPS with source sg-alb

   Outbound: 
      All traffic with destination 0.0.0.0/0 

当我在 Terraform 中配置它时,我得到 Error: Cycle:,这似乎表明存在循环。事实上,我指定从安全组 sg-alb 到实例 sg-http-alb 和安全组 sg-http-alb 我正在使用安全组 sg-alb 作为来源。 EC2 和 ALB 都在同一个 public 子网中(这是有原因的)。

但是使用控制台是允许的。此外,当我将我的 EC2 实例的内部 IP 地址(使用 /32)指定为出站目的地时,它可以工作但不确定这是否是正确的方法。

如果您想避免循环引用,您需要将安全组规则拆分为 separate terraform resources,而不是在 security_group 资源中内联定义它们。