How to specify all ports in Security group - CloudFormation
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In AWS CloudFormation, you specify "all ports" in a security group by setting IpProtocol to "-1", which means all protocols and implicitly all ports. For TCP or UDP specifically, set FromPort: 0 and ToPort: 65535 to cover the full port range. The "-1" protocol is the simplest approach and is equivalent to selecting "All traffic" in the AWS Console.
All Traffic (All Protocols, All Ports)
When IpProtocol is "-1", you must NOT specify FromPort or ToPort. AWS treats this as all protocols (TCP, UDP, ICMP, and others) on all ports.
All TCP Ports
This allows all TCP traffic (ports 0-65535) from the specified CIDR range.
All UDP Ports
ICMP (All Types)
For ICMP, FromPort represents the ICMP type and ToPort the ICMP code. Setting both to -1 means all ICMP types and codes.
Complete Security Group Example
Using Security Group References
IPv6 Support
Port Range Syntax
CDK Equivalent (TypeScript)
Common Pitfalls
- Specifying FromPort/ToPort with IpProtocol "-1": When using
IpProtocol: "-1"(all traffic), do NOT includeFromPortorToPort. CloudFormation will throw a validation error. The all-protocol rule implicitly covers all ports. - Using 0.0.0.0/0 for ingress in production: Allowing all traffic from
0.0.0.0/0on ingress opens your instances to the entire internet. Restrict CIDR ranges to known IP ranges, VPN addresses, or use security group references to limit access to other AWS resources. - Forgetting egress rules: Security groups have a default egress rule allowing all outbound traffic. If you explicitly define any egress rule in CloudFormation, the default is removed. If you only add one egress rule, all other outbound traffic is blocked.
- String vs number for IpProtocol:
IpProtocolmust be a string. Use"-1"(quoted) not-1(number). Using an unquoted-1may cause YAML parsing issues, though most parsers handle it. For safety, always quote protocol values. - Security group rule limits: Each security group can have up to 60 inbound and 60 outbound rules by default (adjustable via AWS support). Using "all ports" rules (
IpProtocol: "-1") counts as one rule, while listing individual ports uses one rule per entry.
Summary
- Use
IpProtocol: "-1"for all protocols and all ports (equivalent to "All traffic" in the Console) - Use
FromPort: 0, ToPort: 65535withtcporudpfor all ports of a specific protocol - Do NOT specify
FromPort/ToPortwhenIpProtocolis"-1" - Use
SourceSecurityGroupIdto allow traffic from other security groups instead of CIDR ranges - Restrict
CidrIpto specific networks in production — avoid0.0.0.0/0for ingress rules
Related reading
- How to specify AWS Access Key ID and Secret Access Key as part of a amazon s3n URL
- How to specify AWS credentials in C .NET core console program
- How to specify credentials when connecting to boto3 S3?
- How to specify credentials when connecting to boto3 S3?
- How to specify api docs url for swagger ui in spring boot open api v3?
- How to specify Proxy Pass in kubernetes
- How to stop all external traffic and allow only inter pod network call within namespace using network policy?
- How to store user information with DynamoDB and Cognito using Facebook authentication with iOS SDK

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.