简体   繁体   English

AWS 和 Terraform - 安全组中的默认出口规则

[英]AWS and Terraform - Default egress rule in security group

There is a repeatable configuration that I see in many Terraform projects where the provider is AWS: The configuration of an outbound (egress) rule to allow ALL outbound traffic.我在提供者是 AWS 的许多 Terraform 项目中看到了一个可重复的配置:出站(出口)规则的配置以允许所有出站流量。

As far as I understand, this is the default behavior in AWS as mentioned in the AWS user guide :据我了解,这是AWS 用户指南中提到的AWS 中的默认行为:

By default, a security group includes an outbound rule that allows all outbound traffic.默认情况下,安全组包含允许所有出站流量的出站规则。 You can remove the rule and add outbound rules that allow specific outbound traffic only.您可以删除规则并添加仅允许特定出站流量的出站规则。 If your security group has no outbound rules, no outbound traffic originating from your instance is allowed.如果您的安全组没有出站规则,则不允许来自您的实例的出站流量。

An example for a common Terraform setup for security group - The focus of my question is the egress block:安全组的常见 Terraform 设置示例 - 我的问题的重点是出口块:

 resource "aws_security_group" "my_sg" {
       name        = "my_sg"
       description = "Some description"
       vpc_id      = "${aws_vpc.my_vpc.id}"
       tags {
         Name = "my_sg_tag"
       }

       #Not redundant - Because a new security group has no inbound rules.
       ingress {
         from_port   = "80"
         to_port     = "80"
         protocol    = "TCP"
         cidr_blocks = ["0.0.0.0/0"]
       }

       #Isn't this redundant?    
       egress {
         from_port   = 0
         to_port     = 0
         protocol    = "-1"
         cidr_blocks = ["0.0.0.0/0"]
       }
}

Is this configuration being made for documentation or does it have a technical reason?此配置是为文档而进行的还是有技术原因?

The documentation for the aws_security_group resource specifically states that they remove AWS' default egress rule intentionally by default and require users to specify it to limit surprises to users: aws_security_group资源文档特别指出,他们在默认情况下有意删除了 AWS 的默认出口规则,并要求用户指定它以限制对用户的意外:

NOTE on Egress rules: By default, AWS creates an ALLOW ALL egress rule when creating a new Security Group inside of a VPC.关于出口规则的注意事项:默认情况下,AWS 在 VPC 内创建新安全组时会创建 ALLOW ALL 出口规则。 When creating a new Security Group inside a VPC, Terraform will remove this default rule, and require you specifically re-create it if you desire that rule.在 VPC 内创建新安全组时,Terraform 将删除此默认规则,如果您需要该规则,则要求您专门重新创建它。 We feel this leads to fewer surprises in terms of controlling your egress rules.我们认为这会减少在控制出口规则方面的意外情况。 If you desire this rule to be in place, you can use this egress block:如果您希望此规则到位,您可以使用此出口块:

egress {
  from_port   = 0
  to_port     = 0
  protocol    = "-1"
  cidr_blocks = ["0.0.0.0/0"]
}

There's also a technical/UX reason here in that it would be tricky to make Terraform understand whether it should keep the allow all egress rule when making changes to the security group.这里还有一个技术/用户体验原因,因为在对安全组进行更改时,让 Terraform 了解它是否应该保留允许所有出口规则会很棘手。 Should it always provide the allow all egress rule unless another egress rule is specified and then if so remove the default?除非指定了另一个出口规则,否则它是否应该始终提供允许所有出口规则,然后删除默认值? How would that work with the combination of the aws_security_group_rule resource ?这将如何与aws_security_group_rule资源的组合aws_security_group_rule

AWS have made the decision that a default rule to allow all egress outbound is a nicer user experience than not having it (and confusing people as to why their instance is unable to communicate outbound) without too much of a security impact (compared to the equivalent for inbound). AWS已经做出了决定,一个默认规则,允许所有出口出站比没有它(和混乱的人,为什么它们的实例是无法沟通出站)没有太多的安全影响一个更好的用户体验(相比于等价入站)。 Even if they were to change their mind on the benefit of this now they would be unable to do this without massively breaking a lot of people's setups/workflows which AWS is very reluctant to do.即使他们现在改变主意对此的好处,他们也无法在不大规模破坏 AWS 非常不愿意做的很多人的设置/工作流程的情况下做到这一点。

Terraform, on the other hand, has made the decision the other way and that suits the tool better as well as slightly improving the security posture of the tool at the expense of making people define a repeated egress block in a lot of places.另一方面,Terraform 以另一种方式做出了决定,这更适合该工具,并略微改善了工具的安全状况,代价是让人们在很多地方定义重复的出口块。

If you particularly care about the repetition and you do always want to allow all egress traffic then you might find it useful to use a module instead that automatically includes an allow all egress rule.如果您特别关心重复并且您总是希望允许所有出口流量,那么您可能会发现使用自动包含允许所有出口规则的模块来代替很有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 append 或删除安全组的入口/出口规则 Terraform? - How to append or delete the ingress/egress rule for a security group using Terraform? 是否可以使用 Cloudformation 删除 aws 安全组默认出口允许所有规则? - Is it possible to delete the aws Security Group Default Egress allow all Rule with Cloudformation? AWS Cloudformation - 向安全组出口规则添加条件 - AWS Cloudformation - Add condition to security group egress rule AWS Cloudformation:允许所有出口的安全组规则 - AWS Cloudformation: Security Group Rule to allow all egress 带有 Terraform 的 AWS - 安全组规则中的安全组参数 - AWS with Terraform - security groups argument inside a security group rule 条件表达式在 aws_security_group 资源出口块 terraform 中不起作用 - Conditional Expression not working in aws_security_group resource egress block terraform AWS Workspace 安全组出口要求 - AWS Workspace Security Group Egress Requirments Terraform aws 安全组 revoke_rule_on_delete? - Terraform aws security group revoke_rule_on_delete? 有条件地使用 terraform 中的计数创建 aws_security_group_rule - Conditionally create aws_security_group_rule with count in terraform Terraform:成功创建资源(aws_security_group),但它采用来自所有给定安全组的入口/出口规则 - Terraform: create resource(aws_security_group) successfully but it takes ingress/egress rules from all given security groups
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM