2つのVPCセキュリティグループを作成したいとします。Terraformを使用してAWS VPCセキュリティグループを作成するときのサイクルエラー
1つはVPCのBastionホスト用で、もう1つはプライベートサブネット用です。
# BASTION #
resource "aws_security_group" "VPC-BastionSG" {
name = "VPC-BastionSG"
description = "The sec group for the Bastion instance"
vpc_id = "aws_vpc.VPC.id"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["my.super.ip/32"]
}
egress {
# Access to the Private subnet from the bastion host[ssh]
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = ["${aws_security_group.VPC-PrivateSG.id}"]
}
egress {
# Access to the Private subnet from the bastion host[jenkins]
from_port = 8686
to_port = 8686
protocol = "tcp"
security_groups = ["${aws_security_group.VPC-PrivateSG.id}"]
}
tags = {
Name = "VPC-BastionSG"
}
}
# PRIVATE #
resource "aws_security_group" "VPC-PrivateSG" {
name = "VPC-PrivateSG"
description = "The sec group for the private subnet"
vpc_id = "aws_vpc.VPC.id"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = ["${aws_security_group.VPC-BastionSG.id}"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
security_groups = ["${aws_security_group.VPC-PublicSG.id}"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
security_groups = ["${aws_security_group.VPC-PublicSG.id}"]
}
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
security_groups = ["${aws_security_group.VPC-PublicSG.id}"]
}
ingress {
from_port = 8686
to_port = 8686
protocol = "tcp"
security_groups = ["${aws_security_group.VPC-BastionSG.id}"]
}
ingress {
# ALL TRAFFIC from the same subnet
from_port = 0
to_port = 0
protocol = "-1"
self = true
}
egress {
# ALL TRAFFIC to outside world
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "VPC-PrivateSG"
}
}
私はそれをterraform plan
すると、このエラーが返されます。
**`Error configuring: 1 error(s) occurred:
* Cycle: aws_security_group.VPC-BastionSG, aws_security_group.VPC-PrivateSG`**
私はPrivateSGからBastionSGのための進入ルールをコメントアウトした場合の計画はうまく実行されます。
また、私がBastionSGからPrivateSGの出力ルールをコメントアウトすると、それも正常に実行されます。
AWS Scenario 2 for building a VPC with Public/Private subnets and Bastion hostは、設定しようとしているアーキテクチャについて説明しています。
AWSコンソールで設定されている設定とまったく同じで、正常に再生されます。
なぜTerraformはそれを受け入れていないのですか? Bastionセキュリティグループをプライベートセキュリティグループに接続する別の方法はありますか?
EDIT
私は何とかAWSでそれが有効であるにもかかわらず、壊す必要がある2秒のグループの間の循環参照があることを理解したよう。
私は、Bastion secグループからのすべての発信トラフィック(0.0.0.0/0)を許可し、それを個々のセキュリティグループに指定しないことを考えました。
セキュリティに悪影響はありますか?
terraform GitHubには、お互いに依存してセキュリティグループを記述する問題があります。スレッドの最後にお勧めのソリューションがあなたのケースで機能しますか? https://github.com/hashicorp/terraform/issues/539 – jbird
@jbirdを指摘していただきありがとうございます。私はそれをCIDRブロックに置き換え、それはもはや不平を言っていない。しかし、私は明確で記述的なコードを持ちたいので、ydaetskcoRの答えを好むだろう。 –