Terraform - 授权 VPC 对等连接之间的安全组
Terraform - Authorizing Security Groups Between VPC Peering Connection
我 运行 在 AWS 上,我有 VPC-A 和 VPC-B
我在两个 VPC 之间有一个 VPC 对等
我想允许从 VPC-B 中的 SecurityGroupB 到 VPC-A 中的 SecurityGroupA 的流量
到目前为止,我使用 ruby 客户端进行了以下调用
security_group_a.authorize_ingress(
ip_permissions: [
{
from_port: "-1",
ip_protocol: "-1",
to_port: "-1",
user_id_group_pairs: [
{
description: "Accept all traffic from SecurityGroupB",
group_id: security_group_b.id,
vpc_id: vpc_b.id,
vpc_peering_connection_id: peering_connection_id,
},
],
},
]
)
我查看了 terraform 的 aws_security_group_rule,但找不到与上述设置等效的内容。
当我尝试将安全组 B 放入安全组 A 的入口时,出现以下错误:
Error: Error authorizing security group rule type ingress: InvalidGroup.NotFound: You have specified two resources that belong to different networks
我在这里做错了什么?假设我已设置对等连接,如何创建规则以允许流量从 VPC B 上的安全组进入 VPC A 上的安全组?
如果满足以下条件,安全组规则可以引用对等 VPC 中的安全组:
- VPC必须在同一地域
- 对等连接必须处于活动状态
- 如果对等 VPC 在另一个账户中,则引用必须包含账户号码作为前缀。例如,
123456789012/sg-1a2b3c4d
。
只要满足这些条件,应该没有问题。
下面是 Terraform 资源的外观示例:
resource "aws_security_group_rule" "example" {
type = "ingress"
from_port = 0
to_port = 65535
protocol = "tcp"
security_group_id = "sg-123456"
source_security_group_id = "sg-789012"
}
所以@Ben Whaley 的回答在入口规则中指定来自另一个 VPC 的安全组方面是正确的。
关于我收到的错误
Error: Error authorizing security group rule type ingress: InvalidGroup.NotFound: You have specified two resources that belong to different networks
原因是在将安全组添加到入口规则之前,我必须添加对 vpc 对等连接的依赖:
resource "aws_security_group_rule" "vpc_a_to_vpc_b" {
security_group_id = var.vpc_a_security_group_id
description = "Allow vpc_b to communicate with vpc_a"
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
source_security_group_id = aws_security_group.vpc_b.id
depends_on = [aws_vpc_peering_connection.vpc_a_to_vpc_b]
}
depends_on
是与众不同的地方
我 运行 在 AWS 上,我有 VPC-A 和 VPC-B 我在两个 VPC 之间有一个 VPC 对等
我想允许从 VPC-B 中的 SecurityGroupB 到 VPC-A 中的 SecurityGroupA 的流量
到目前为止,我使用 ruby 客户端进行了以下调用
security_group_a.authorize_ingress(
ip_permissions: [
{
from_port: "-1",
ip_protocol: "-1",
to_port: "-1",
user_id_group_pairs: [
{
description: "Accept all traffic from SecurityGroupB",
group_id: security_group_b.id,
vpc_id: vpc_b.id,
vpc_peering_connection_id: peering_connection_id,
},
],
},
]
)
我查看了 terraform 的 aws_security_group_rule,但找不到与上述设置等效的内容。
当我尝试将安全组 B 放入安全组 A 的入口时,出现以下错误:
Error: Error authorizing security group rule type ingress: InvalidGroup.NotFound: You have specified two resources that belong to different networks
我在这里做错了什么?假设我已设置对等连接,如何创建规则以允许流量从 VPC B 上的安全组进入 VPC A 上的安全组?
如果满足以下条件,安全组规则可以引用对等 VPC 中的安全组:
- VPC必须在同一地域
- 对等连接必须处于活动状态
- 如果对等 VPC 在另一个账户中,则引用必须包含账户号码作为前缀。例如,
123456789012/sg-1a2b3c4d
。
只要满足这些条件,应该没有问题。
下面是 Terraform 资源的外观示例:
resource "aws_security_group_rule" "example" {
type = "ingress"
from_port = 0
to_port = 65535
protocol = "tcp"
security_group_id = "sg-123456"
source_security_group_id = "sg-789012"
}
所以@Ben Whaley 的回答在入口规则中指定来自另一个 VPC 的安全组方面是正确的。
关于我收到的错误
Error: Error authorizing security group rule type ingress: InvalidGroup.NotFound: You have specified two resources that belong to different networks
原因是在将安全组添加到入口规则之前,我必须添加对 vpc 对等连接的依赖:
resource "aws_security_group_rule" "vpc_a_to_vpc_b" {
security_group_id = var.vpc_a_security_group_id
description = "Allow vpc_b to communicate with vpc_a"
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
source_security_group_id = aws_security_group.vpc_b.id
depends_on = [aws_vpc_peering_connection.vpc_a_to_vpc_b]
}
depends_on
是与众不同的地方