Sub topic 4
Placing RDS in your VPC
This chapter covers the three network controls that decide who can reach a DB instance: the DB subnet group, security groups, and the public accessibility flag. It assumes you can read a VPC route table, know what a security group is, and can run AWS CLI commands in a region where you already have a VPC with private subnets.
How RDS uses your VPC
An RDS instance runs on an AWS-managed host. RDS attaches a network interface from your VPC to that host. The interface gets a private IP address from one of your subnets, and your security groups are applied to it. You cannot log in to the host, but you control its network position.
Three settings decide where the instance sits and who can reach it:
| Setting | What it controls | Where you set it |
|---|---|---|
| DB subnet group | Which VPC and which subnets (and so which Availability Zones) the instance may use | Created once, referenced at creation |
| VPC security group | Which sources may open connections to the database port | Attached to the instance, changeable at any time |
| Public accessibility | Whether the endpoint resolves to a public IP address from outside the VPC | Instance attribute, changeable |
The following diagram shows the usual private layout.
DB subnet groups
A DB subnet group is a named collection of subnets in one VPC. RDS requires subnets in at least two Availability Zones, even when you launch a Single-AZ instance. The requirement exists so that you can later enable Multi-AZ or restore into another zone without redoing the network setup.
The subnet group determines the VPC. An instance's VPC is fixed by the group you choose at creation. To move an instance to a different VPC, you restore a snapshot into a subnet group that belongs to the new VPC. Changing the subnet group to another one in the same VPC is possible through modify-db-instance, subject to engine and deployment constraints.
Create a group from private subnets in two zones:
aws rds create-db-subnet-group \
--db-subnet-group-name demo-subnets \
--db-subnet-group-description "Private subnets for the demo database" \
--subnet-ids subnet-0123456789abcdef0 subnet-0fedcba9876543210Use private subnets. A subnet is private when its route table has no route to an internet gateway. Placing the group's subnets in such a route table is the strongest guarantee that the database is not reachable from the internet, whatever the other settings say.
Sizing the subnets
RDS needs free IP addresses beyond the one it uses today. A Multi-AZ failover, a scale-compute operation, or a replacement host may need another address in the subnet. A subnet with no free addresses can make these operations fail. Give database subnets a comfortable range, such as a /24, and avoid sharing them with autoscaling fleets that can use up the addresses.
Security groups
A VPC security group is a stateful firewall attached to the instance's network interface. If an inbound connection is allowed, the reply traffic is allowed automatically. Security groups only have allow rules, and anything not allowed is dropped.
The database port depends on the engine, and you can override it at creation:
| Engine | Default port |
|---|---|
| PostgreSQL | 5432 |
| MySQL and MariaDB | 3306 |
| SQL Server | 1433 |
| Oracle | 1521 |
The best practice is to reference another security group as the source, not an IP range. The rule then follows your application as instances are replaced and their addresses change.
# Replace the VPC ID with your own
DB_SG=$(aws ec2 create-security-group \
--group-name demo-db-sg \
--description "Demo database access" \
--vpc-id vpc-0123456789abcdef0 \
--query GroupId --output text)
# Allow PostgreSQL only from members of the application security group
aws ec2 authorize-security-group-ingress \
--group-id "$DB_SG" \
--protocol tcp \
--port 5432 \
--source-group sg-0aaaabbbbccccddddHere sg-0aaaabbbbccccdddd is a placeholder for the group attached to your application servers. Pass the new $DB_SG value as --vpc-security-group-ids when you create the instance in the next chapter.
A new security group has an outbound rule that allows all traffic. A plain database usually needs no outbound access. Features that make the database call out, such as importing from S3 or invoking a Lambda function from SQL, need outbound paths that you design deliberately.
Public accessibility
The PubliclyAccessible attribute controls DNS and addressing. When it is true, the instance gets a public IP address, and the endpoint name resolves to that public address from outside the VPC. When it is false, the endpoint resolves only to the private address, and only from inside the VPC or from networks connected to it.
The flag alone does not open the database. For a connection from the internet to succeed, all of these must hold:
- The flag is
true. - The instance's subnets route to an internet gateway.
- A security group rule allows the client's address.
- Any network ACLs allow the traffic.
Because any one broken link closes the path, a private design should break several of them. Use --no-publicly-accessible, private subnets, and security group rules that name only your application group. You can inspect the result on a running instance:
aws rds describe-db-instances \
--db-instance-identifier demo-pg \
--query "DBInstances[0].[PubliclyAccessible,DBSubnetGroup.VpcId,Endpoint.Address]"To turn off public access on an existing instance:
aws rds modify-db-instance \
--db-instance-identifier demo-pg \
--no-publicly-accessible \
--apply-immediatelyReaching a private instance
A private endpoint is not reachable from your laptop. These are the common ways to connect for administration:
| Method | How it works | Notes |
|---|---|---|
| Application in the VPC | The app connects to the endpoint directly | The normal production path |
| Bastion host or SSM Session Manager port forwarding | You forward a local port to the endpoint through an EC2 instance | Needs no inbound SSH if you use Session Manager |
| VPN or Direct Connect | Your network is routed into the VPC | Good for teams that connect often |
| VPC peering or Transit Gateway | Another VPC or account reaches the endpoint | Security group rules must allow the peer's range or group, and routes must exist both ways |
With peering, a security group rule can reference a group in a peered VPC only in specific setups, so many teams use CIDR ranges there. Check the rule and the route tables before you suspect the database.
Common errors
- Connection times out. A timeout almost always means traffic is dropped, not refused. Check the security group source, the network ACL, the route tables, and whether you are outside the VPC while the instance is private.
- Connection refused. The packet arrived but nothing accepted it. Check that you are using the right port, and that the instance status is
available. - "Subnets must cover at least two Availability Zones." The subnet group has subnets from one zone only. Add a subnet from a second zone.
- Subnet group mismatch with the security group. A security group belongs to one VPC. Attaching a group from a different VPC than the subnet group fails at creation.
- Public endpoint works from some places only. Inside the VPC the name resolves to the private address, so a rule that allows the application's public IP address does not match. Allow the private source instead.
Exercise
A teammate sets PubliclyAccessible to true on an instance in private subnets (no internet gateway route), with a security group that allows 0.0.0.0/0 on port 5432. Can someone on the internet connect? Explain which condition decides it, and name two changes that would make the design safe.
Show answer
No. The endpoint would resolve to a public address, but the subnets have no route to an internet gateway, so internet traffic cannot reach the instance. Reachability needs the flag, a route, and a permissive security group (and network ACL) all at once, and here the route is missing. The design is still fragile, because one added route would expose the database to the whole internet. Make it safe by setting --no-publicly-accessible and by replacing the 0.0.0.0/0 rule with a rule whose source is the application's security group.
Exercise
You create a Single-AZ instance and the create-db-subnet-group call fails because both of your subnets are in us-east-1a. Why does RDS require two zones for a Single-AZ instance, and how do you fix it?
Show answer
RDS requires coverage of at least two Availability Zones so the instance can later be moved to Multi-AZ, restored, or replaced in another zone without a new subnet group. Fix it by creating a subnet in a second zone (for example us-east-1b) and including it in the subnet group.