Skip to content

Commit f10494c

Browse files
authored
feat(ec2): well-known port aliases (#29793)
### Issue # (if applicable) None as far as I can tell ### Reason for this change The web console lists commonly used ports when adding a rule to a security group, this aims to reproduce this simple quality of life shortcut. It can also help with code readability, and might save people from a typo ### Description of changes * Add well-known static `Port` instances * Intersection of the AWS web console listed ports and the [IANA Service Name and Transport Protocol Port Number Registry](https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt) ### Description of how you validated changes Compared the AWS web console values to the IANA list, and added a unit test to make sure the alias behaved properly ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 8198884 commit f10494c

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

packages/aws-cdk-lib/aws-ec2/README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ const provider = ec2.NatProvider.instanceV2({
215215
new ec2.Vpc(this, 'TheVPC', {
216216
natGatewayProvider: provider,
217217
});
218-
provider.connections.allowFrom(ec2.Peer.ipv4('1.2.3.4/8'), ec2.Port.tcp(80));
218+
provider.connections.allowFrom(ec2.Peer.ipv4('1.2.3.4/8'), ec2.Port.HTTP);
219219
```
220220

221221
You can also customize the characteristics of your NAT instances, including their security group,
@@ -266,7 +266,7 @@ const provider = ec2.NatProvider.instance({
266266
new ec2.Vpc(this, 'TheVPC', {
267267
natGatewayProvider: provider,
268268
});
269-
provider.connections.allowFrom(ec2.Peer.ipv4('1.2.3.4/8'), ec2.Port.tcp(80));
269+
provider.connections.allowFrom(ec2.Peer.ipv4('1.2.3.4/8'), ec2.Port.HTTP);
270270
```
271271

272272
### Ip Address Management
@@ -724,13 +724,13 @@ declare const appFleet: autoscaling.AutoScalingGroup;
724724
declare const dbFleet: autoscaling.AutoScalingGroup;
725725

726726
// Allow connections from anywhere
727-
loadBalancer.connections.allowFromAnyIpv4(ec2.Port.tcp(443), 'Allow inbound HTTPS');
727+
loadBalancer.connections.allowFromAnyIpv4(ec2.Port.HTTPS, 'Allow inbound HTTPS');
728728

729729
// The same, but an explicit IP address
730-
loadBalancer.connections.allowFrom(ec2.Peer.ipv4('1.2.3.4/32'), ec2.Port.tcp(443), 'Allow inbound HTTPS');
730+
loadBalancer.connections.allowFrom(ec2.Peer.ipv4('1.2.3.4/32'), ec2.Port.HTTPS, 'Allow inbound HTTPS');
731731

732732
// Allow connection between AutoScalingGroups
733-
appFleet.connections.allowTo(dbFleet, ec2.Port.tcp(443), 'App can call database');
733+
appFleet.connections.allowTo(dbFleet, ec2.Port.HTTPS, 'App can call database');
734734
```
735735

736736
### Connection Peers
@@ -747,7 +747,7 @@ peer = ec2.Peer.anyIpv4();
747747
peer = ec2.Peer.ipv6('::0/0');
748748
peer = ec2.Peer.anyIpv6();
749749
peer = ec2.Peer.prefixList('pl-12345');
750-
appFleet.connections.allowTo(peer, ec2.Port.tcp(443), 'Allow outbound HTTPS');
750+
appFleet.connections.allowTo(peer, ec2.Port.HTTPS, 'Allow outbound HTTPS');
751751
```
752752

753753
Any object that has a security group can itself be used as a connection peer:
@@ -758,9 +758,9 @@ declare const fleet2: autoscaling.AutoScalingGroup;
758758
declare const appFleet: autoscaling.AutoScalingGroup;
759759

760760
// These automatically create appropriate ingress and egress rules in both security groups
761-
fleet1.connections.allowTo(fleet2, ec2.Port.tcp(80), 'Allow between fleets');
761+
fleet1.connections.allowTo(fleet2, ec2.Port.HTTP, 'Allow between fleets');
762762

763-
appFleet.connections.allowFromAnyIpv4(ec2.Port.tcp(80), 'Allow from load balancer');
763+
appFleet.connections.allowFromAnyIpv4(ec2.Port.HTTP, 'Allow from load balancer');
764764
```
765765

766766
### Port Ranges
@@ -770,6 +770,7 @@ the connection specifier:
770770

771771
```ts
772772
ec2.Port.tcp(80)
773+
ec2.Port.HTTPS
773774
ec2.Port.tcpRange(60000, 65535)
774775
ec2.Port.allTcp()
775776
ec2.Port.allIcmp()
@@ -823,7 +824,7 @@ const mySecurityGroupWithoutInlineRules = new ec2.SecurityGroup(this, 'SecurityG
823824
disableInlineRules: true
824825
});
825826
//This will add the rule as an external cloud formation construct
826-
mySecurityGroupWithoutInlineRules.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(22), 'allow ssh access from the world');
827+
mySecurityGroupWithoutInlineRules.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.SSH, 'allow ssh access from the world');
827828
```
828829

829830
### Importing an existing security group

packages/aws-cdk-lib/aws-ec2/lib/port.ts

+35
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,41 @@ export interface PortProps {
188188
* Interface for classes that provide the connection-specification parts of a security group rule
189189
*/
190190
export class Port {
191+
/** Well-known SSH port (TCP 22) */
192+
public static readonly SSH = Port.tcp(22);
193+
/** Well-known SMTP port (TCP 25) */
194+
public static readonly SMTP = Port.tcp(25);
195+
/** Well-known DNS port (UDP 53) */
196+
public static readonly DNS_UDP = Port.udp(53);
197+
/** Well-known DNS port (TCP 53) */
198+
public static readonly DNS_TCP = Port.tcp(53);
199+
/** Well-known HTTP port (TCP 80) */
200+
public static readonly HTTP = Port.tcp(80);
201+
/** Well-known POP3 port (TCP 110) */
202+
public static readonly POP3 = Port.tcp(110);
203+
/** Well-known IMAP port (TCP 143) */
204+
public static readonly IMAP = Port.tcp(143);
205+
/** Well-known LDAP port (TCP 389) */
206+
public static readonly LDAP = Port.tcp(389);
207+
/** Well-known HTTPS port (TCP 443) */
208+
public static readonly HTTPS = Port.tcp(443);
209+
/** Well-known SMB port (TCP 445) */
210+
public static readonly SMB = Port.tcp(445);
211+
/** Well-known IMAPS port (TCP 993) */
212+
public static readonly IMAPS = Port.tcp(993);
213+
/** Well-known POP3S port (TCP 995) */
214+
public static readonly POP3S = Port.tcp(995);
215+
/** Well-known Microsoft SQL Server port (TCP 1433) */
216+
public static readonly MSSQL = Port.tcp(1433);
217+
/** Well-known NFS port (TCP 2049) */
218+
public static readonly NFS = Port.tcp(2049);
219+
/** Well-known MySQL and Aurora port (TCP 3306) */
220+
public static readonly MYSQL_AURORA = Port.tcp(3306);
221+
/** Well-known Microsoft Remote Desktop Protocol port (TCP 3389) */
222+
public static readonly RDP = Port.tcp(3389);
223+
/** Well-known PostgreSQL port (TCP 5432) */
224+
public static readonly POSTGRES = Port.tcp(5432);
225+
191226
/**
192227
* A single TCP port
193228
*/

packages/aws-cdk-lib/aws-ec2/test/security-group.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,12 @@ describe('security group', () => {
503503
}],
504504
});
505505
});
506+
507+
test('Static well-known ports are well-defined', () => {
508+
// THEN
509+
expect(Port.SSH).toEqual(Port.tcp(22));
510+
expect(Port.DNS_UDP).toEqual(Port.udp(53));
511+
});
506512
});
507513
});
508514

0 commit comments

Comments
 (0)