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

时间:2019-12-12 08:07:54

标签: typescript aws-cdk

如何使用AWS CDK将两个安全组连接在一起?

这是允许通过端口443进入IPv4流量的示例

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

这来自文档:

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

这是我能想到的最好的方法(其中“ elbSecurityGroup”是另一个安全组):

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

但这并没有任何意义。必须有更好的初始化对等方的方法。打字稿说

  

“ Peer”类的构造函数受到保护,并且只能在内部访问   类声明。

如果我尝试:

const p = new Peer()

1 个答案:

答案 0 :(得分:1)

这可以通过直接访问SecurityGroups或其他结构上的“连接”来完成

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

或者从一个EC2实例对象直接到另一个EC2实例:

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

这将创建/更改由附加到EC2实例的CDK创建的安全组。