Sub topic 4
Placing RDS in your VPC
This chapter assumes you know what a VPC, a subnet, and a security group are, and that you can run AWS CLI commands. It covers the three networking pieces RDS needs before you can create an instance: a DB subnet group, a security group, and the public accessibility setting.
Why networking comes first
An RDS instance runs on infrastructure AWS manages, but its network interface lives in your VPC. You choose where it sits and who can reach it. You must make these choices before provisioning, because RDS asks for them in the create call.
DB subnet groups
A DB subnet group is a named collection of subnets in one VPC. RDS picks a subnet from this group to place your instance. The group must include subnets in at least two Availability Zones, even for a single-AZ instance. This lets RDS move the instance, or place a Multi-AZ standby, in another zone later.
For a private database, use private subnets. These are subnets whose route table has no route to an internet gateway.
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-0fedcba9876543210The subnet IDs above are placeholders. Replace them with subnets from your own VPC, in different Availability Zones.
Security groups
A security group is a stateful firewall attached to the instance's network interface. For RDS, the key rule is an inbound rule on the engine's port:
| Engine | Default port |
|---|---|
| PostgreSQL | 5432 |
| MySQL, MariaDB | 3306 |
| SQL Server | 1433 |
| Oracle | 1521 |
Prefer a rule whose source is another security group, such as the one on your application servers, rather than a CIDR range. The rule then follows your application as instances come and go, and you never open the database to an address range wider than needed.
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 5432 \
--source-group sg-0aaaabbbbccccddddHere sg-0123456789abcdef0 is the database's group and sg-0aaaabbbbccccdddd is the application's group.
Public accessibility
The --publicly-accessible setting controls whether the instance's DNS name resolves to a public IP address. With --no-publicly-accessible, the name resolves to a private IP, and only resources inside the VPC (or connected networks) can reach it.
Both conditions must hold for public access: the setting is on, and the subnet routes to an internet gateway. Even then, the security group still decides who gets in. For most workloads, keep the instance private. Reach it from your laptop through a VPN, AWS Direct Connect, or a bastion host.
Common errors
- "DB subnet group doesn't meet availability zone coverage requirement": the group's subnets sit in fewer than two zones. Add a subnet in another zone.
- Connection timeout from your application: usually a missing inbound rule, or the application is in a different VPC with no peering or routing.
- Subnet group and security group in different VPCs: RDS rejects the create call. Both must belong to the same VPC.
Exercise
You created a DB subnet group with two private subnets and an instance with --no-publicly-accessible. Your application server in the same VPC times out connecting to port 5432. What is the most likely cause, and how do you fix it?
Show answer
The database's security group probably has no inbound rule allowing TCP 5432 from the application. Add an inbound rule on the database's security group with the application's security group as the source.