# Connections

Manage the allowed network connections for constructs with Security Groups.

Security Groups can be thought of as a firewall for network-connected
devices. This class makes it easy to allow network connections to and
from security groups, and between security groups individually. When
establishing connectivity between security groups, it will automatically
add rules in both security groups

This object can manage one or more security groups.

Implements: [IConnectable](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.IConnectable.html)

- Source: [`aws-ec2/lib/connections.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-ec2/lib/connections.ts#L71)

## Example

TypeScript:

```ts
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import { aws_ec2 as ec2 } from 'aws-cdk-lib';

declare const peer: ec2.IPeer;
declare const port: ec2.Port;
declare const securityGroup: ec2.SecurityGroup;
const connections = new ec2.Connections(/* all optional props */ {
  defaultPort: port,
  peer: peer,
  securityGroups: [securityGroup],
});
```

Python:

```python
# The code below shows an example of how to instantiate this type.
# The values are placeholders you should change.
from aws_cdk import aws_ec2 as ec2

# peer: ec2.IPeer
# port: ec2.Port
# security_group: ec2.SecurityGroup

connections = ec2.Connections(
    default_port=port,
    peer=peer,
    security_groups=[security_group]
)
```

Java:

```java
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import software.amazon.awscdk.services.ec2.*;

IPeer peer;
Port port;
SecurityGroup securityGroup;

Connections connections = Connections.Builder.create()
        .defaultPort(port)
        .peer(peer)
        .securityGroups(List.of(securityGroup))
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
using Amazon.CDK.AWS.EC2;

IPeer peer;
Port port;
SecurityGroup securityGroup;

var connections = new Connections(new ConnectionsProps {
    DefaultPort = port,
    Peer = peer,
    SecurityGroups = new [] { securityGroup }
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import "github.com/aws/aws-cdk-go/awscdk"

var peer IPeer
var port Port
var securityGroup SecurityGroup

connections := awscdk.Aws_ec2.NewConnections(&ConnectionsProps{
	DefaultPort: port,
	Peer: peer,
	SecurityGroups: []ISecurityGroup{
		securityGroup,
	},
})
```

## Initializer

TypeScript:

```ts
new Connections(props?: ConnectionsProps)
```

Python:

```python
Connections(props: typing.Optional[ConnectionsProps] = None)
```

Java:

```java
new Connections(ConnectionsProps props)
```

C# / .NET:

```csharp
new Connections(ConnectionsProps? props = null)
```

Golang:

```go
NewConnections(Props ConnectionsProps)
```

## Properties

- `connections`: [Connections](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.Connections.html) — The network connections associated with this resource.

- `defaultPort`: [Port](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.Port.html) — The default port configured for this connection peer, if available.

- `securityGroups`: [ISecurityGroup](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.ISecurityGroup.html)[]

## Methods

- `addSecurityGroup(...securityGroups: ISecurityGroup[]): void` — Add a security group to the list of security groups managed by this object.

- `allowDefaultPortFrom(other: IConnectable, description?: string): void` — Allow connections from the peer on our default port.

  Even if the peer has a default port, we will always use our default port.

- `allowDefaultPortFromAnyIpv4(description?: string): void` — Allow default connections from all IPv4 ranges.

- `allowDefaultPortInternally(description?: string): void` — Allow hosts inside the security group to connect to each other.

- `allowDefaultPortTo(other: IConnectable, description?: string): void` — Allow connections from the peer on our default port.

  Even if the peer has a default port, we will always use our default port.

- `allowFrom(other: IConnectable, portRange: Port, description?: string): void` — Allow connections from the peer on the given port.

- `allowFromAnyIpv4(portRange: Port, description?: string): void` — Allow from any IPv4 ranges.

- `allowInternally(portRange: Port, description?: string): void` — Allow hosts inside the security group to connect to each other on the given port.

- `allowTo(other: IConnectable, portRange: Port, description?: string): void` — Allow connections to the peer on the given port.

- `allowToAnyIpv4(portRange: Port, description?: string): void` — Allow to all IPv4 ranges.

- `allowToDefaultPort(other: IConnectable, description?: string): void` — Allow connections to the security group on their default port.
