Understanding How Code Reaches Resources Inside and Outside a VPC
This lesson explains how code reaches resources inside and outside a VPC, and how to diagnose it when it cannot. Network design is outside the exam's scope.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how route tables, internet gateways and NAT gateways decide whether a subnet reaches the internet.
- Distinguish security groups from network ACLs.
- Distinguish gateway endpoints from interface endpoints.
- Use VPC Flow Logs to find where a connection is rejected.
Subnets and gateways
A VPC is a private network in one Region, divided into subnets, each of which lies in a single Availability Zone. Whether a subnet can reach the internet is decided by its route table, not by anything on the resources inside it.
A public subnet has a route to an internet gateway, which connects the VPC to the internet. Resources in it that have public IP addresses, such as a load balancer or a bastion host, can be reached from the internet and can reach it directly. A private subnet has no route to the internet gateway, so nothing on the internet can connect to its resources, which is where application servers, databases and VPC Lambda functions belong. To let them make outbound connections, for example to a third-party API, the private subnet's route table sends internet traffic to a NAT gateway. The NAT gateway sits in a public subnet, forwards outbound connections and their responses, and blocks connections initiated from outside. One NAT gateway per Availability Zone avoids a single point of failure. The following table summarises the components.
| Component | Purpose |
|---|---|
| Public subnet | Has a route to an internet gateway |
| Private subnet | Reaches the internet only through a NAT gateway |
| NAT gateway | Lets private resources make outbound internet connections |
Security groups vs network ACLs
Security groups and network ACLs both filter traffic in a VPC, but they work at different levels.
A security group acts as a firewall for individual resources, such as an EC2 instance, a Lambda function in a VPC or an RDS database. Its rules only allow traffic; anything not allowed is denied. It is stateful: if a request is allowed in, the response is allowed out automatically, and vice versa. A rule can name another security group as its source, so a database can accept connections from every resource in the application's security group, whatever their IP addresses. Security groups are the main control that developers configure.
A network ACL filters traffic entering and leaving a whole subnet. Its numbered rules are evaluated in order and can allow or deny, which makes it possible to block a specific IP range. It is stateless: responses are evaluated separately, so the ACL needs rules that allow return traffic on the ephemeral ports clients use, typically 1024 to 65535. A missing ephemeral port rule is a common reason for connections that time out. The default network ACL allows all traffic. The following table summarises the differences.
Example. The database security group allows port 5432 from the application's security group ID rather than IP ranges, so any instance or function with that group is allowed in, whatever its IP address.
The following diagram shows where each filter applies on the path from a client to a database.

Security group or network ACL?
VPC endpoints
AWS service APIs, such as those of S3, SQS and Secrets Manager, are public endpoints. A resource in a private subnet can reach them through a NAT gateway, but a VPC endpoint provides a private path instead, so traffic never leaves the AWS network and no internet access is needed.
A gateway endpoint is available only for S3 and DynamoDB. It adds a route to the service in the chosen route tables and costs nothing, so it is the first thing to add when private resources use either service. An interface endpoint, powered by AWS PrivateLink, is available for most other services. It creates a network interface with a private IP address in each chosen subnet, protected by a security group, and with private DNS enabled, the service's normal endpoint name resolves to that address, so applications need no change. Interface endpoints are charged per hour and per gigabyte. Both types support endpoint policies that limit which actions and resources can be used through them, and bucket policies can require that requests arrive through a particular endpoint. The following table summarises the two types.
| Endpoint type | Services | Cost |
|---|---|---|
| Gateway | S3 and DynamoDB | Free |
| Interface (AWS PrivateLink) | Most other services | Hourly and per GB |
Lambda functions in a VPC should use private subnets in several Availability Zones. They reach public APIs through a NAT gateway, and AWS services through a NAT gateway or VPC endpoints.
Example. A function in private subnets reads from S3. Instead of paying for a NAT gateway, add a free gateway endpoint to the route tables:
aws ec2 create-vpc-endpoint --vpc-id vpc-0abc123 --vpc-endpoint-type Gateway \
--service-name com.amazonaws.eu-west-1.s3 --route-table-ids rtb-0priv1 rtb-0priv2Gateway or interface endpoint?
Troubleshooting connections
When a connection fails or times out, check the path in order:
-
Security groups
Check both ends: outbound rules at the client and inbound rules at the target.
-
Route tables
Check that the subnet has a route to a NAT gateway or a VPC endpoint.
-
Network ACLs
Check the rules in both directions, including return traffic on ephemeral ports.
-
DNS resolution
Check that the endpoint's name resolves to the expected address.
VPC Flow Logs record the traffic and show where it was rejected.
Trace a failed connection
A Lambda function that cannot reach anything
After a function is attached to a VPC to reach a private RDS database, the database works but calls to S3, Secrets Manager and a public payment API all time out.
| Destination | Why it fails | Fix |
|---|---|---|
| Amazon S3 | No route to the internet from private subnets | S3 gateway endpoint (free) |
| Secrets Manager | Same | Secrets Manager interface endpoint |
| Public payment API | Same | NAT gateway in a public subnet, with a route from the private subnets |
Lambda functions in a VPC never receive public IP addresses, so putting them in public subnets does not help. The security group on the database must also allow the function's security group on the database port.
A function in a private subnet must read from S3 without internet access. What is the cheapest option?
Show answer
A gateway VPC endpoint for S3, which adds a route to S3 in the route table at no extra charge. Interface endpoints and NAT gateways are charged per hour and per GB.
Exam Tips
Remember for the exam
- For private resources calling S3 or DynamoDB without internet access, use a gateway VPC endpoint.
- To let the application reach the database, allow the application's security group in the database's security group.
- A Lambda function in a VPC that cannot reach a third-party API needs a route through a NAT gateway.
- When a connection times out, check security groups, route tables and network ACLs on both sides.