【问题标题】:Recreate Same Security Group Rule Each time I execute "terrafrom apply/plan"每次执行“terraform apply/plan”时重新创建相同的安全组规则
【发布时间】:2017-01-13 11:32:12
【问题描述】:

在不对 main.tf 进行任何更改的情况下,Terraform 添加已经存在的安全组规则。 Terrafrom 将 main.tf 中的每个 SG 规则标识为新规则(但这些规则已在 aws 中作为先前执行的结果)并在我执行命令时尝试重新创建它们(terrafrom plan/apply)

这是 terraform apply 命令的输出

~ module.application_sg.aws_security_group.security_group
    ingress.#:                            "3" => "1"
    ingress.2358522502.cidr_blocks.#:     "1" => "0"
    ingress.2358522502.cidr_blocks.0:     "20.0.1.0/24" => ""
    ingress.2358522502.from_port:         "443" => "0"
    ingress.2358522502.protocol:          "tcp" => ""
    ingress.2358522502.security_groups.#: "0" => "0"
    ingress.2358522502.self:              "false" => "false"
    ingress.2358522502.to_port:           "443" => "0"
    ingress.3250959853.cidr_blocks.#:     "1" => "0"
    ingress.3250959853.cidr_blocks.0:     "20.0.1.0/24" => ""
    ingress.3250959853.from_port:         "8080" => "0"
    ingress.3250959853.protocol:          "tcp" => ""
    ingress.3250959853.security_groups.#: "0" => "0"
    ingress.3250959853.self:              "false" => "false"
    ingress.3250959853.to_port:           "8080" => "0"
    ingress.753360330.cidr_blocks.#:      "0" => "0"
    ingress.753360330.from_port:          "0" => "0"
    ingress.753360330.protocol:           "-1" => "-1"
    ingress.753360330.security_groups.#:  "0" => "0"
    ingress.753360330.self:               "true" => "true"
    ingress.753360330.to_port:            "0" => "0"

+ module.rule1.aws_security_group_rule.rule
    cidr_blocks.#:            "1"
    cidr_blocks.0:            "20.0.1.0/24"
    from_port:                "80"
    protocol:                 "tcp"
    security_group_id:        "sg-17c13770"
    self:                     "false"
    source_security_group_id: "<computed>"
    to_port:                  "80"
    type:                     "ingress"

此规则已经存在。请帮助避免这种情况,因为当我们要向环境添加新规则时,这很难理解。

【问题讨论】:

    标签: amazon-web-services terraform


    【解决方案1】:

    检查状态文件。这通常发生在 Terraform 已应用更改但尚未更新状态文件时。

    您可以使用以下命令列出该位置的状态文件中的内容:

    terraform state list
    

    如果状态文件中缺少它,您应该能够使用 Terraform 的导入命令将预先存在的资源导入状态文件,如下所示:

    terraform import aws_security_group.security_group sg-123456
    

    【讨论】:

    • 但是当我执行 terraform refresh 命令时,它没有检测到任何变化。这意味着 terraform 状态文件和真实的基础设施是相同的。但是当我执行 terraform apply 或 paln 时,它会尝试添加由于之前的 terraform 执行而已经存在的安全组规则。我想避免这种情况,因为当我真的想添加新规则时,我可能会被误解或很难找到什么是真正新增的规则
    • 如果您查看状态文件,它会显示吗? Refresh 仅更新 Terraform 对其正在管理的资源的视图(例如由 TF 构建但在 TF 之外更改的 ELB 的配置)。如果它根本不在状态文件中,那么刷新也无济于事。
    【解决方案2】:

    不要将入口/出口规则放在aws_security_group 对象中。使用aws_security_group_rule管理规则。

    例如。

    resource "aws_security_group" "my-secret-group" {
      name = "my-secret-group"
      vpc_id = "vpc-12345678"
    }
    
    resource "aws_security_group_rule" "ssh-external-to-node" {
      type = "ingress"
      security_group_id = "${aws_security_group.my-secret-group.id}"
      from_port = 22
      to_port = 22
      protocol = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
    }
    
    resource "aws_security_group_rule" "http-external-to-node" {
      type = "ingress"
      security_group_id = "${aws_security_group.my-secret-group.id}"
      from_port = 80
      to_port = 80
      protocol = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
    }
    

    【讨论】:

    • 这为我解决了上述问题,但了解原因会很有用
    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 2019-12-13
    • 2020-03-27
    • 1970-01-01
    • 2018-11-28
    • 2022-06-30
    相关资源
    最近更新 更多