## ------ Creating security group for VPC1 ------
resource "aws_security_group" "sg1" {
name = "SG1"
vpc_id =
aws_vpc.vpc1.id ingress {
description = "Allow for SG2"
from_port = 0
to_port = 0
protocol = "all"
security_groups = [
aws_security_group.sg2.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["
0.0.0.0/0"]
}
tags = {
Name = "SG1"
}
}
######## Allow access from SG1 to SG2 ########
resource "aws_security_group_rule" "extra_rule_sg2" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "all"
security_group_id = "${
aws_security_group.sg2.id}"
source_security_group_id = "${
aws_security_group.sg1.id}"
depends_on = [aws_security_group.sg1]
}
## ------ Creating security group for VPC2 ------
resource "aws_security_group" "sg2" {
name = "SG2"
vpc_id =
aws_vpc.vpc2.id ingress {
description = "Allow for SG1"
from_port = 0
to_port = 0
protocol = "all"
security_groups = [
aws_security_group.sg1.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["
0.0.0.0/0"]
}
tags = {
Name = "SG2"
}
}
######## Allow access from SG1 to SG2 ########
resource "aws_security_group_rule" "extra_rule_sg1" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "all"
security_group_id = "${
aws_security_group.sg1.id}"
source_security_group_id = "${
aws_security_group.sg2.id}"
depends_on = [aws_security_group.sg2]
}