Sub topic 9
Your duties for network and resource configuration
This chapter assumes you know what a VPC, a subnet, a security group, and an Availability Zone are. It also assumes you can read a short AWS CLI command. AWS secures the physical network and the virtualization layer. You decide how your resources are connected, who can reach them, and how they are configured. Most real-world incidents on AWS come from this second part.
Where the line falls for networking
AWS operates the data centers, the cables, the routers, and the hypervisor that keeps one customer's network traffic separate from another's. You cannot change any of that, and you do not need to.
Inside your VPC, every choice is yours:
- The CIDR ranges and the subnet layout.
- Which subnets have a route to an internet gateway.
- Which resources receive public IP addresses.
- Which security group and network ACL rules allow traffic.
- Whether traffic to AWS services stays on the AWS network (VPC endpoints) or crosses the public internet.
- Whether traffic is encrypted in transit.
AWS gives you the controls and sensible defaults. It does not know which of your workloads are meant to be public, so it does not decide for you.
The layers you configure
Routing and subnet design
A subnet is "public" only because its route table has a route to an internet gateway. A subnet without that route is private. This is a configuration fact, not a label, so check the route table instead of trusting the subnet name.
A common design places load balancers in public subnets and application servers and databases in private subnets. Private resources that need outbound access use a NAT gateway. This limits what an attacker can reach even if one rule is wrong.
Security groups
A security group is a stateful firewall attached to a network interface. Key properties:
- It only has allow rules. Anything not allowed is denied.
- It is stateful: a response to an allowed request is allowed automatically.
- A rule can reference another security group as its source. This is better than a CIDR range for internal traffic, because it keeps working when IP addresses change.
- A new security group allows all outbound traffic by default. Tightening outbound rules is your decision.
The rule that most often causes harm is a source of 0.0.0.0/0 on an administrative port such as 22 (SSH) or 3389 (RDP), or on a database port. This example finds security groups that allow SSH from anywhere:
aws ec2 describe-security-groups \
--filters Name=ip-permission.from-port,Values=22 \
Name=ip-permission.to-port,Values=22 \
Name=ip-permission.cidr,Values=0.0.0.0/0 \
--query "SecurityGroups[].{Id:GroupId,Name:GroupName,VpcId:VpcId}" \
--output tableTo remove the offending rule from a group:
aws ec2 revoke-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 22 \
--cidr 0.0.0.0/0Replace the group ID with one returned by the first command. For administrative access, prefer AWS Systems Manager Session Manager. It needs no inbound port at all.
Network ACLs
A network ACL is a stateless firewall applied to a whole subnet. Because it is stateless, you must allow return traffic explicitly, including ephemeral ports. Rules are evaluated in number order, and the first match wins. Network ACLs include explicit deny rules, which security groups lack. Many teams leave the default (allow all) and rely on security groups. That is a valid choice, but it is a choice you own. Use network ACLs when you need a subnet-wide deny, such as blocking a known bad address range.
| Property | Security group | Network ACL |
|---|---|---|
| Attached to | Network interface | Subnet |
| State | Stateful | Stateless |
| Rule types | Allow only | Allow and deny |
| Evaluation | All rules together | In number order, first match wins |
| Default for new resources | Deny inbound, allow outbound | Default ACL allows all |
Private connectivity to AWS services
By default, a call from an EC2 instance to S3 or DynamoDB goes to a public service endpoint. In a private subnet with a NAT gateway, this works but crosses the internet path. A VPC endpoint keeps the traffic on the AWS network and lets you attach a policy to the endpoint.
aws ec2 create-vpc-endpoint \
--vpc-id vpc-0123456789abcdef0 \
--vpc-endpoint-type Gateway \
--service-name com.amazonaws.us-east-1.s3 \
--route-table-ids rtb-0123456789abcdef0A gateway endpoint for S3 adds a route to the route tables you name. It has no hourly charge. Interface endpoints, which use PrivateLink, cost money per hour and per gigabyte, so factor that into your design.
Encryption in transit
AWS encrypts traffic between its own facilities. It does not encrypt the traffic between your clients and your application, or between your own tiers, unless you configure it. You own TLS certificates (AWS Certificate Manager can issue and renew them), the TLS policy on load balancers, and whether a service rejects unencrypted requests. For S3, a bucket policy can deny any request where aws:SecureTransport is false.
Resource configuration beyond the network
Configuration duties extend to every resource you create. The details differ by service category, which earlier chapters covered.
- EC2 (infrastructure service): you patch the guest operating system, harden the image, and require IMDSv2 so a server-side request forgery bug cannot easily read instance credentials.
- RDS (container service): AWS patches the database engine on your maintenance schedule, but you set the subnet group, the security group, public accessibility, and the parameter group (for example, forcing SSL).
- S3 (abstracted service): there is no network to configure in the usual sense, but you own Block Public Access, bucket policies, and versioning.
This command turns on all four Block Public Access settings for one bucket:
aws s3api put-public-access-block \
--bucket amzn-s3-demo-bucket \
--public-access-block-configuration \
BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=trueS3 is operated by AWS, but whether a bucket is public is a configuration choice that belongs to you.
To require IMDSv2 on an existing instance:
aws ec2 modify-instance-metadata-options \
--instance-id i-0123456789abcdef0 \
--http-tokens required \
--http-endpoint enabledPreventing drift
Configuration drifts. Someone opens a port for a test and forgets it. A template is edited by hand in the console. Three practices keep your side of the model stable:
- Define infrastructure as code. Use CloudFormation, the AWS CDK, or a similar tool, so network rules are reviewed like application code.
- Detect changes. AWS Config records configuration changes and evaluates them against rules.
- Prevent classes of mistakes. Service control policies in AWS Organizations, and account-level settings such as default EBS encryption, stop the error before it happens.
You can check which Config rules report problems:
aws configservice describe-compliance-by-config-rule \
--compliance-types NON_COMPLIANTManaged rules cover many network checks, for example restricted-ssh (security groups that allow unrestricted SSH), vpc-default-security-group-closed, s3-bucket-public-read-prohibited, and rds-instance-public-access-check. You must enable AWS Config and the rules in each Region and account where you want coverage. They are not on by default.
Common errors
- Assuming the default VPC is a safe design. Its subnets are public and it exists for quick starts. Production workloads usually need a custom VPC.
- Treating "private subnet" as "unreachable." A private resource can still be reached from other VPCs, peered networks, VPN links, or anything inside the VPC with a permissive security group.
- Forgetting the default security group. Resources launched without a group choice join it. Its default rule allows all traffic from members of the same group, so close it and do not use it.
- Relying on a network ACL alone. Because it is stateless, a mistake in return rules produces confusing timeouts instead of clear errors.
- Opening a port to fix a connection problem and leaving it open. Troubleshoot with VPC Flow Logs and Reachability Analyzer, then restore the rule.
- Assuming a managed service is private. An RDS instance with
PubliclyAccessibleset to true and a permissive security group is reachable from the internet, even though AWS runs the database engine. - Enabling a check in one Region only. Resources in other Regions go unchecked.
A checklist for your side
- Is every subnet's public or private status intentional and verified in its route table?
- Does any security group allow
0.0.0.0/0on a non-web port? - Are database and cache resources in private subnets with no public access?
- Do your AWS service calls use VPC endpoints where traffic must not leave the AWS network?
- Is TLS required for data in transit, and who renews the certificates?
- Are VPC Flow Logs enabled for the VPCs that matter?
- Do AWS Config rules cover these checks in every Region you use?
- Is the intended configuration stored as code, with changes reviewed?
Exercise
A team runs a PostgreSQL database on Amazon RDS in a private subnet. A developer sets PubliclyAccessible to true and adds a security group rule allowing port 5432 from 0.0.0.0/0 to test from a laptop. A colleague says this is fine because "AWS manages RDS." Which party is responsible for this exposure, and which two controls would detect or prevent it?
Show answer
The customer is responsible. AWS operates the database engine and host, but the subnet placement, public accessibility setting, and security group rules are customer configuration. The AWS Config managed rules rds-instance-public-access-check and restricted-common-ports (or a custom rule on port 5432) would detect it. A service control policy or infrastructure-as-code review would prevent it. A safer way to test from a laptop is Session Manager port forwarding or a VPN.
Exercise
A security group and a network ACL both apply to an EC2 instance in a subnet. Traffic to port 443 is allowed by the security group and by the inbound network ACL rule, but clients still time out. What is the most likely cause in the network ACL, and why does the security group not have the same problem?
Show answer
The network ACL is stateless, so it needs an outbound rule that allows the response traffic on the ephemeral port range (1024-65535 for most clients). Without it, responses are dropped. The security group is stateful, so it automatically allows replies to traffic it permitted.