Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow ingress from one security group to another using AWS CDK

How can I connect two security groups together using the AWS CDK?

This is an example of allow IPv4 traffic ingress via port 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):

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. Typescript says

Constructor of class 'Peer' is protected and only accessible within the class declaration.

If I try:

const p = new Peer()
like image 951
comfytoday Avatar asked Dec 12 '19 08:12

comfytoday


People also ask

What is ingress rule in security group?

AWS::EC2::SecurityGroup Ingress. Adds an inbound rule to a security group. An inbound rule permits instances to receive traffic from the specified IPv4 or IPv6 CIDR address range, or from the instances associated with the specified security group.

What is ingress and egress in security group?

Egress in the world of networking implies traffic that exits an entity or a network boundary, while Ingress is traffic that enters the boundary of a network.

Can multiple security groups be applied to a single VPC?

Think of it as applying firewall settings to individual instances (or rather, virtual NICs within an instance). Another thing that you need to know about VPC security groups is that you can apply multiple security groups to a single network adapter.

What is Cidrip?

That system is known as CIDR notation. CIDR IP addresses consist of two groups of numbers, which are also referred to as groups of bits. The most important of these groups is the network address, and it is used to identify a network or a sub-network (subnet). The lesser of the bit groups is the host identifier.


1 Answers

This can be done by accessing the 'connections' on SecurityGroups or other Constructs directly

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

Or from an EC2 Instance object directly to another EC2 instance:

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.

like image 96
comfytoday Avatar answered Sep 19 '22 20:09

comfytoday