【问题标题】:How to append or delete the ingress/egress rule for a security group using Terraform?如何使用 Terraform 附加或删除安全组的入口/出口规则?
【发布时间】:2021-05-14 00:24:38
【问题描述】:

有没有办法在 Terraform 中管理 AWS 安全组以编辑现有 SG 的规则?

例如:如果我提供一个新实例,现有 SG 的入口规则会更新以允许新提供的实例。当实例终止时,SG 也需要更新。

如果 Terraform 不直接支持,请随意提出其他常见做法。

【问题讨论】:

  • “实例终止”是指通过创建它的同一个 TF 脚本终止它,使用 terraform destroy?
  • @Marcin 是的 terraform destroy

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


【解决方案1】:

是的,您可以向现有安全组 (SG) 添加和删除单个规则。这可以分两步完成:

  1. 使用 aws_security_group 获取现有 SG 的数据源:
data "aws_security_group" "selected" {
  id = <group-id-of-existing-sg>
}
  1. 创建aws_security_group_rule 资源以将新规则从步骤 1 添加到 SG:
resource "aws_security_group_rule" "example" {
  type              = "ingress"
  from_port         = 0
  to_port           = 65535
  protocol          = "tcp"
  cidr_blocks       = ["0.0.0.0/0"]
  security_group_id = data.aws_security_group.selected.id
}

如果您的实例是在与 SG 规则相同的 TF 文件中创建的,则在 terraform destroy 时,实例和规则都将被销毁。

【讨论】:

    猜你喜欢
    • 2021-03-06
    • 2020-03-27
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多