简体   繁体   English

允许使用 AWS CDK 从一个安全组进入另一个安全组

[英]Allow ingress from one security group to another using AWS CDK

How can I connect two security groups together using the AWS CDK?如何使用 AWS CDK 将两个安全组连接在一起?

This is an example of allow IPv4 traffic ingress via port 443这是允许 IPv4 流量通过端口 443 进入的示例

ec2SecurityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(443), 'Test rule', false)

This from the documentation:这来自文档:

public addIngressRule(peer: IPeer, connection: Port, description?: string, remoteRule?: boolean): void

This is the best I could come up with (where 'elbSecurityGroup' is another security group):这是我能想到的最好的方法(其中 'elbSecurityGroup' 是另一个安全组):

const p = Peer.anyIpv4()
p.connections.allowFrom(elbSecurityGroup.connections, Port.tcp(443))
ec2SecurityGroup.addIngressRule(p, Port.tcp(443), 'Test rule', false)

But that doesn't really make any sense.但这真的没有任何意义。 There must be a better way of Initializing the Peer.必须有更好的方法来初始化 Peer。 Typescript says打字稿说

Constructor of class 'Peer' is protected and only accessible within the class declaration.类“Peer”的构造函数受保护并且只能在类声明中访问。

If I try:如果我尝试:

const p = new Peer()

This can be done by accessing the 'connections' on SecurityGroups or other Constructs directly这可以通过直接访问 SecurityGroups 或其他构造上的“连接”来完成

ec2SecurityGroup.connections.allowFrom(elbSecurityGroup, Port.tcp(443), 'Application Load Balancer')

Or from an EC2 Instance object directly to another EC2 instance:或者直接从一个 EC2 实例对象到另一个 EC2 实例:

ec2Instance1.connections.allowFrom(ec2Instance2, Port.tcp(4321), 'Inbound')
ec2Instance2.connections.allowTo(ec2Instance1, Port.tcp(4321), 'Outbound')

This will create/alter a SecurityGroup created by CDK that is attached to the EC2 instance.这将创建/更改由附加到 EC2 实例的 CDK 创建的 SecurityGroup。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM