A firewall rule is a configuration statement that allows or denies network traffic according to its direction, protocol, port, and source or destination. Before traffic enters or leaves an EC2 instance, AWS inspects it against rules managed through security groups. section 5.4 section 5.4.1
Apply firewall rules narrowly to reduce exposure:
- Open only required ports: a public web server commonly needs inbound TCP ports 80 and 443, while unnecessary ports should remain closed. section 5.4
- Define traffic by direction, IP protocol, port, and IP address, IP range, or—within AWS—a source or destination security group. section 5.4.1
- Remember the defaults: security groups deny inbound traffic unless rules allow it, but allow all outbound traffic unless that rule is replaced. section 5.4
- Troubleshoot unexpected results with VPC Reachability Analyzer for simulated paths or VPC Flow Logs for aggregated rejected-connection messages. section 5.4
Allow HTTP to a web server
A CloudFormation security group rule permits inbound TCP traffic from 0.0.0.0/0 to port 80. The associated instance installs and starts Apache, allowing a browser to reach a basic test page. section 5.4.3
Listing 10.6 CloudFormation template extract: Firewall rules for an RDS database
DatabaseSecurityGroup: #1
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: 'awsinaction-db-sg'
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 3306 #2
ToPort: 3306
SourceSecurityGroupId: !Ref WebServerSecurityGroup #3
Inspect a CloudFormation extract that restricts MySQL traffic on port 3306 to instances attached to WebServerSecurityGroup. section 10.4.2

