16
12

More than 5 years have passed since last update.

aws_security_groupのCycleエラー

Last updated at Posted at 2015-05-20

TerraformでAWSのsecurity groupを設定した時にハマったのでメモ。

Security Groupの設定はaws_security_groupで行うが、2つのaws_security_groupのingress, egressのsecurity_groupsのidがお互い参照しあっていると、Cycleエラーが発生する。

aws.tf
provider "aws" {
    ...
}

resource "aws_security_group" "sg1" {
    name = "sg1"

    ingress {
        from_port = 80
        to_port = 80
        protocol = "tcp"
        security_groups = ["${aws_security_group.sg2.id}"] # お互いに参照しあっている
    }
}

resource "aws_security_group" "sg2" {
    name = "sg2"

    egress {
        from_port = 80
        to_port = 80
        protocol = "tcp"
        security_groups = ["${aws_security_group.sg1.id}"] # お互いに参照しあっている
    }
}
terminal
$ terraform
There are warnings and/or errors related to your configuration. Please
fix these before continuing.

Errors:

  * 1 error(s) occurred:

* Cycle: aws_security_group.sg1, aws_security_group.sg2

こういう場合は、片方のingress、egressをaws_security_group_ruleにすると、回避できる。

aws.tf
resource "aws_security_group" "sg1" {
    name = "sg1"

    ingress {
        from_port = 80
        to_port = 80
        protocol = "tcp"
        security_groups = ["${aws_security_group.sg2.id}"]
    }
}

resource "aws_security_group" "sg2" {
    name = "sg2"

#    egress {
#        from_port = 80
#        to_port = 80
#        protocol = "tcp"
#        security_groups = ["${aws_security_group.sg1.id}"] # お互いに参照しあっている
#    }
}

# sg2のegress設定をaws_security_group_ruleにする
resource "aws_security_group_rule" "sg2_egress_http_80" {
    security_group_id = "${aws_security_group.sg2.id}"
    type = "egress"
    from_port = 80
    to_port = 80
    protocol = "tcp"
    source_security_group_id = "${aws_security_group.sg1.id}"
}

量が増えるとつらそうなので、他に良い書き方がありましたら教えてくださいー

16
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
12