我如何使用 Cloudformation 从其他 VPC EC2 实例访问 RDS

How I can access RDS from other VPC EC2 instance using Cloudformation

我是 AWS 新手。我有 VPC-A 和 VPC-B。我想从 VPC-B EC2 实例访问 VPC-A 中的 RDS。我创建了 VPC 对等连接但无法访问 RDS。当我将 VPC-B EC2 实例 ID 添加到 VPC-A RDS 安全组时,它正在工作。但我需要从 Cloudformation 进行。我可以从 CloudFormation 更新现有安全组吗?感谢提前 :)

几件事。您可能已经创建了 VPC 对等互连,但您需要转到与 RDS 和 EC2 实例的子网关联的路由 tables 并确保它们可以通信。

即如果您的 EC2 实例在子网 10.0.X.XX 中,则需要在 RDS 子网的路由 table 中为 10.0.X.XX 使用 VPC 对等互连(可能显示为 pcx-xxxx , pc for 'peering connection') 和 EC2 实例将需要使用 EC2 的 VPC 对等的类似规则。然后您的安全组将需要允许您提到的流量。

至于通过 CloudFormation 更新现有安全组,是的,这是可能的。您将根据需要使用 SecurityGroupIngress(入站)或 SecurityGroupEgress(出站)资源,例如:

"MySecurityGroupIngressRule": {
"Type": "AWS::EC2::SecurityGroupIngress",
"Properties" : {   
    "GroupId": "sg-123456789",
    "IpProtocol": "tcp",
    "FromPort": "1234",
    "ToPort": "1234",
    "CidrIp": "1.2.3.4/0"
}

}

GroupId 是您根据 SG ID 指定现有组的方式。