Skip to content

RDS security: best practices for databases on AWS

The database holds the data that matters most. See how to lock down networking, encryption, backups and access in RDS.

Reading time
9 min
Published

The database usually holds the organization's most sensitive data, which makes it a high-value target. In RDS, security has several layers: where in the network the database sits, who can reach it, how data is protected at rest and in transit, how access is authenticated and how backups are stored. A failure in any one of them can expose everything.

For RDS security, the topics follow the path of someone trying to reach the data. First come network isolation and exposure (where the database sits and who can reach it), then encryption and authentication, next what each connection can do once inside and, finally, auditing, detection of anomalous access and backups. The idea is to shrink the attack surface and keep every access to the database controlled and traceable.

Private networking and exposure

The database should sit in private subnets, with no direct access from the internet. Publicly accessible databases are among the most serious findings in any review, because they are exposed to scanning and attacks all the time. Security groups should allow connections only from the workloads that actually need them, preferably by referencing the application's group instead of IP ranges.

Also, do not expose the database to make administration easier. When you need administrative access, use a controlled bastion host or temporary tunnels and keep the database port closed to the internet.

Reference the application's group, not IP ranges

A security group that opens the database port to a broad range lets more hosts reach the database than necessary. The tighter pattern is to have the inbound rule point to the application's security group as the source. Only members of that group can connect, and the rule stays correct even when the workloads' IPs change.

Inbound rule that only accepts connections from the application's group
aws ec2 authorize-security-group-ingress \  --group-id sg-db \  --protocol tcp --port 5432 \  --source-group sg-app

Encryption, authentication and logs

Enable encryption at rest, which protects the data and the snapshots, and require encrypted connections in transit. In most cases, encryption at rest has to be set when the database is created, so plan for it from the start. IAM authentication, in turn, uses temporary credentials and, in the scenarios where it applies, eliminates long-lived database passwords.

  • Keep the database in a private subnet, with no public access
  • Allow only the workloads that need to connect in the security group
  • Enable encryption at rest and require TLS in transit
  • Consider IAM authentication to have fewer fixed passwords
  • Turn on database logs for auditing and investigation

Enable the logs the database engine offers, such as error and audit logs, and export them for analysis. They are what help you detect anomalous access and investigate incidents that involve data.

IAM authentication in practice

With IAM authentication enabled on the database, the application generates a temporary token and connects with it, without a fixed password. Permission to connect is granted in IAM and restricted to the corresponding database user. Control becomes centralized, and the application no longer carries a long-lived password.

IAM connect permission restricted to one database user
{  "Effect": "Allow",  "Action": "rds-db:connect",  "Resource": "arn:aws:rds-db:REGION:ACCOUNT:dbuser:DB-ID/app_read"}

Least privilege inside the database

The network and authentication control who reaches the database, but not what each user does once connected. Inside the database, apply least privilege by creating users per function, each with only the privileges its workload uses. An application that only reads and writes rows does not need privileges to alter the schema, create users or act as a superuser.

Application user with minimal privileges (PostgreSQL example)
-- Application role limited to reading and writing dataCREATE ROLE app_rw LOGIN;GRANT CONNECT ON DATABASE shop TO app_rw;GRANT USAGE ON SCHEMA public TO app_rw;GRANT SELECT, INSERT, UPDATE, DELETE  ON ALL TABLES IN SCHEMA public TO app_rw;-- No DDL, no role creation, no superuser

Also separate administrative roles from application roles. The schema migration credential, which needs DDL, should not be the same one the application uses at runtime. If someone compromises the production workload, they do not also gain the power to change the database structure.

Detecting anomalous access

Preventive controls reduce the chance of unauthorized access, but you still need to see what is happening. Export the engine logs to CloudWatch Logs and create alerts for signs of abuse, such as a sequence of authentication failures or connections from an unexpected source. GuardDuty has an RDS protection plan that watches for anomalous login attempts on managed databases, and it works well as an extra detection layer.

Also keep an eye on unusual queries, such as bulk reads of sensitive tables, which may indicate exfiltration. This kind of signal tends to go unnoticed when no audit log is exported and nobody is looking. So give detection in the database the same care you give the network and identity.

Backups and snapshots

Automated backups and snapshots are as much a part of security as of availability. Set an appropriate retention period, keep backups encrypted and protect them against deletion. Be careful with snapshot sharing: a snapshot shared broadly, or made public, hands over a complete copy of the database. And test restores from time to time, because until a backup has been restored, you are only assuming it works.

Practical checklist

  • Place the database in a private subnet, with no public access
  • Restrict connections in the security group to the workloads that need them
  • Do not expose the database for direct administration
  • Enable encryption at rest at creation time
  • Require TLS for connections in transit
  • Use the application's group as the source in the security group, not IP ranges
  • Evaluate IAM authentication with temporary tokens
  • Apply least privilege inside the database, with users per function
  • Separate administrative credentials from application credentials
  • Enable and export the database engine logs
  • Alert on login failures and unexpected sources
  • Set backup retention and keep backups encrypted
  • Prevent snapshots from being shared broadly or publicly
  • Test backup restores periodically

Best practices

  • As a rule, isolate the database in a private network
  • Give network access only to the workloads that need it
  • Reference the application's group instead of IP ranges
  • Plan encryption at rest from the moment you create the database
  • Replace fixed passwords with IAM authentication when you can
  • Apply least privilege inside the database, not just on the network
  • Protect and test backups, not just configure them
  • Audit database access with exported logs

Common mistakes

Publicly accessible database
Sensitive data is exposed to scans and attacks from the internet all the time.
Security group that allows broad ranges
More hosts than necessary can reach the database.
Application connecting as superuser
Whoever compromises the workload gains unlimited power over the schema and the data.
Database without encryption at rest
Data and snapshots are unprotected, and turning on encryption later takes work.
Snapshot shared broadly or made public
A complete copy of the database is within reach of people who should not have it.
Backups never tested
The restore fails right when the incident hits.

Authorship and review

Cesar Gargiulo

CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.

Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.

Frequently asked questions

Can I leave RDS publicly accessible?
Except in very specific, well-justified cases, no. A public database is exposed to constant scans and attacks from the internet, and it is among the most serious findings in security reviews. The recommendation is to keep RDS in a private subnet, reachable only by the workloads that need it, with a bastion host or a tunnel for administration.
Can I enable encryption on a database that already exists?
In most cases, encryption at rest has to be set at creation time. For an existing database without encryption, the usual path is to create an encrypted version from a snapshot and migrate, which takes planning and a maintenance window. That is why it is best to enable encryption from the start and avoid that work later.
What is IAM authentication in RDS?
It is a mechanism that lets you authenticate to the database with temporary credentials generated by IAM, in supported scenarios, instead of long-lived passwords. This lowers the risk of a fixed password leaking, and access control becomes centralized. It does not replace every use of passwords, but where it applies, it is a layer worth having.
Do snapshots need security attention?
Yes. A snapshot is a complete copy of the database, so it should be encrypted and protected against deletion and improper sharing. Sharing a snapshot broadly, or making it public, exposes all the data. Treat snapshots with the same rigor as the original database, and that includes retention and access control.
Is protecting the network enough, or do I need least privilege inside the database?
You need both. The network controls who reaches the database, and internal privileges control what each connection can do after authenticating. If the application connects as a superuser, whoever compromises the workload gains full power over the schema and the data. Create users per function, each with only the privileges its workload uses, and separate administrative credentials from application credentials.
How do I restrict the RDS security group correctly?
Instead of opening the database port to an IP range, have the inbound rule point to the application's security group as the source. That way, only workloads in that group can connect, and the rule stays valid even when addresses change. Broad ranges usually let more hosts reach the database than necessary, which widens the attack surface for no gain at all.
How do I detect anomalous access to the database?
Export the engine logs, such as error and audit logs, to CloudWatch Logs and create alerts for signs of abuse: repeated authentication failures, connections from unexpected sources and bulk reads of sensitive tables. GuardDuty offers an RDS protection plan that watches for anomalous login attempts on managed databases. It is an extra layer of detection, and it requires no configuration in the database itself.

When to bring in specialist help

Reviewing exposure, encryption and access on critical databases is part of GUARDIASEC's AWS and Cloud Security service. We assess the network, the controls and the backups, and deliver the fixes in order of priority, starting with the most relevant risks.