Introduction

Databases are the backbone of modern applications. Whether an application uses MySQL, PostgreSQL, MongoDB, or another database platform, reliable and secure connectivity between the application and the database is essential.

In cloud environments, however, running a database is not just about installing the database engine, configuring users, and optimizing queries. The underlying network architecture plays an equally important role in database security, availability, and performance.

Amazon Web Services (AWS) provides several networking services that help organizations deploy databases securely. These include Virtual Private Clouds (VPCs), subnets, route tables, security groups, Network Access Control Lists (NACLs), and private connectivity options.

Understanding how these components work together helps database administrators (DBAs), DevOps engineers, and infrastructure teams prevent unauthorized access, troubleshoot connection failures, and build reliable production environments.

In this article, we will explore how AWS networking works for databases, examine a practical production architecture, and learn how to troubleshoot common database connectivity issues.

Why Is Networking Important for Databases?

Consider a typical application hosted on AWS.

Users access a website through their browsers. The application processes requests and communicates with a database to retrieve or update information.

For this communication to work, the application server must be able to reach the database server over the network.

At the same time, the database should not be unnecessarily exposed to the public internet.

This creates an important design requirement: the database must be accessible to authorized application components while remaining protected from unauthorized connections.

Poor network configuration can lead to several problems:

  • Applications cannot connect to the database.
  • Database ports are accidentally exposed to the internet.
  • Connections fail because of incorrect routing or firewall rules.
  • Database administration becomes difficult when access paths are not designed properly.
  • Applications experience connection timeouts caused by network or infrastructure issues.
  • High-availability deployments fail to provide the expected resilience.

A well-designed AWS network helps address these challenges by separating resources, controlling traffic, and providing private communication paths.

Understanding Amazon VPC for Database Infrastructure

An Amazon Virtual Private Cloud (VPC) is a logically isolated virtual network in AWS where you deploy resources such as EC2 instances, load balancers, and supported database services.

Think of a VPC as your organization’s private network within AWS.

When creating a VPC, you specify an IP address range using CIDR notation.

For example:

10.0.0.0/16

This address range provides 65,536 total IPv4 addresses before AWS subnet reservations and other allocations.

Within the VPC, you can create separate subnets for application servers, database servers, and public-facing components.

Why should databases be deployed inside a VPC?

A VPC provides the foundation for controlling database network access.

It allows you to:

  • Keep database servers on private IP addresses.
  • Control which application servers can connect to databases.
  • Separate application and database workloads into different subnets.
  • Connect to supported AWS services privately.
  • Establish connectivity to corporate networks or other VPCs when required.
  • Monitor network traffic for troubleshooting and security investigations.

A VPC does not automatically secure every resource. Security groups, routing, authentication, encryption, and appropriate database configuration are still necessary.

Public vs Private Subnets for Databases

Subnets divide a VPC into smaller IP address ranges. Each subnet belongs to one Availability Zone.

For database infrastructure, understanding the difference between public and private subnets is essential.

Public subnet

A public subnet typically has a route to an Internet Gateway for internet-bound IPv4 traffic.

Resources in a public subnet can potentially communicate with the internet if the required public addressing and security configuration are present.

Public subnets are commonly used for internet-facing load balancers and other components that need direct public connectivity.

Private subnet

A private subnet does not have a direct route to an Internet Gateway for general internet-bound IPv4 traffic.

Private subnets are commonly used for:

  • MySQL and MariaDB servers.
  • PostgreSQL database servers.
  • MongoDB instances.
  • Application servers.
  • Internal monitoring systems.
  • Background processing services.

For most production databases, a private subnet is the preferred deployment location.

Why should databases remain private?

Suppose a MongoDB server is assigned a public IP address and its database port is exposed to the internet. Attackers may be able to reach the service and attempt to exploit vulnerabilities, stolen credentials, or weak configurations.

Authentication alone should not be treated as a reason to expose a database publicly.

A safer design keeps the database on private addressing and permits connections only from trusted application servers or approved administration paths.

Important: A private subnet can still have outbound internet connectivity through a NAT Gateway, and private resources can be reachable from other networks through explicitly configured routes. Private does not necessarily mean completely isolated.

How Database Traffic Flows Inside AWS

Let’s consider an application that runs on EC2 and uses a MySQL database hosted on another EC2 instance.

The application server is deployed in an application subnet, while MySQL is deployed in a private database subnet.

When the application connects to MySQL, the communication typically follows this path:

  1. The application resolves the database hostname to an IP address.
  2. The application opens a TCP connection to the database’s private IP address on the configured MySQL port, usually TCP 3306.
  3. AWS evaluates the applicable network routing and security controls.
  4. The database security group checks whether the connection is permitted.
  5. The database host’s firewall and MySQL listener configuration must also allow the connection.
  6. MySQL authenticates the database user and processes the request.
  7. Response traffic returns through the established connection.

The VPC’s local route normally enables communication between resources in the same VPC, provided their addressing and other network configurations are correct.

This is an important distinction: resources in the same VPC do not normally need an Internet Gateway or NAT Gateway to communicate with each other.

The local route provides internal connectivity, while security groups and host-level controls determine whether the connection is allowed.

Understanding AWS Security Groups for Databases

A security group acts as a virtual firewall for supported AWS resources. For EC2-hosted databases, security groups are applied to network interfaces.

Security groups control inbound and outbound traffic based on rules that specify protocols, ports, and source or destination addresses.

Security groups are stateful. Return traffic for an allowed connection is automatically permitted, even if there is no matching reverse-direction rule.

Example: MySQL security group

Suppose your application connects to a MySQL server on TCP port 3306.

A suitable inbound rule could be:

SettingValue
TypeMySQL/Aurora
ProtocolTCP
Port3306
SourceApplication-Server-SG

Here, Application-Server-SG represents the security group attached to the application servers.

Instead of allowing connections from every IP address, the rule permits traffic from resources associated with the specified application security group, subject to the normal AWS security group rules.

Example: MongoDB security group

MongoDB commonly uses TCP port 27017 by default, although administrators may configure a custom port.

If your production MongoDB instance listens on TCP port 27028, the security group must allow that configured port.

For example:

SettingValue
ProtocolTCP
Port27028
SourceAuthorized application security group

This approach reduces unnecessary exposure and makes access control easier to maintain as application instances change.

Example: PostgreSQL security group

PostgreSQL commonly uses TCP port 5432.

A PostgreSQL security group can allow inbound TCP port 5432 only from the application security group or another explicitly approved source.

Security group best practices

  • Do not expose database ports to 0.0.0.0/0.
  • Prefer security group references over broad IP ranges when possible.
  • Permit only the database ports required by the application.
  • Review outbound rules and restrict them where appropriate.
  • Keep administrative access separate from application connectivity.
  • Regularly audit security groups for unused or overly permissive rules.

A database security group should express a clear policy: which systems are allowed to connect, on which ports, and for what purpose.

What Is the Role of Route Tables?

A route table determines where network traffic should be directed based on its destination IP address.

Every VPC has a local route for communication within its CIDR range. Additional routes can direct traffic to Internet Gateways, NAT Gateways, Transit Gateways, VPN connections, and other supported targets.

Example private database route table

DestinationTarget
10.0.0.0/16local

This route allows traffic to destinations within the VPC’s IPv4 address range to use the VPC’s internal routing.

A private database subnet may not need a default route to the internet at all.

If the database server needs to download operating system updates, the architecture could provide outbound IPv4 connectivity through a NAT Gateway, or use suitable private endpoints and internal repositories where supported.

For example:

DestinationTarget
10.0.0.0/16local
0.0.0.0/0NAT Gateway

The second route is optional and should be included only when required.

Why do route tables matter during troubleshooting?

A security group can permit a connection, but traffic can still fail if the network route is missing or incorrect.

This is particularly important when databases communicate across:

  • Different VPCs.
  • Multiple AWS accounts.
  • On-premises networks connected through VPN or Direct Connect.
  • Networks connected through AWS Transit Gateway.

For cross-network connections, verify that both the forward and return paths are configured correctly.

Internet Gateway vs. NAT Gateway for Database Servers

An Internet Gateway enables internet connectivity for a VPC when routes, addressing, and security controls permit it.

A NAT Gateway allows private resources to initiate outbound IPv4 connections to external destinations without making those resources directly reachable through the NAT Gateway from the public internet.

For database environments, the distinction is important.

Internet Gateway

Typically supports the internet connectivity path for public-facing components, such as an internet-facing load balancer.

A database instance in a private subnet does not need a public IP address just because the VPC has an Internet Gateway attached.

NAT Gateway

Can provide outbound internet access for private database or application resources when necessary.

For example, a database host might need to retrieve operating system updates from external repositories.

A public NAT Gateway is deployed in a public subnet and normally uses an Elastic IP address. The private subnet’s route table directs relevant outbound traffic to the NAT Gateway, which uses the Internet Gateway to reach the internet.

NAT Gateways do not provide unsolicited inbound internet connectivity to private database instances.

A better option: VPC endpoints

When private resources need to access supported AWS services, VPC endpoints may eliminate the need for NAT-based connectivity to those services.

For example, a database backup workflow that uploads files to Amazon S3 may be able to use an S3 gateway endpoint, depending on the architecture and routing configuration.

This can reduce unnecessary internet routing and NAT Gateway data processing costs.

Remember that an S3 endpoint does not automatically provide private access to every AWS service. Each service and endpoint type has its own configuration requirements.

Private Connectivity to AWS Managed Databases

AWS provides managed database services such as Amazon RDS and Amazon Aurora.

With managed databases, AWS handles many underlying infrastructure responsibilities, but network design remains important.

Amazon RDS

Amazon RDS supports database engines including MySQL, PostgreSQL, MariaDB, and others.

When configuring an RDS database, you typically choose:

  • A VPC.
  • A DB subnet group.
  • Security groups.
  • Public accessibility settings.
  • Database authentication and encryption options.

A DB subnet group identifies subnets that RDS can use for the database deployment. It normally includes subnets in at least two Availability Zones.

For a private production deployment, choose suitable private subnets and set the database to not be publicly accessible unless there is a specific, justified requirement.

The application security group can then be granted access to the database’s configured port.

Self-managed databases on EC2

Some organizations run their own database software on EC2 instances.

This provides more control over the operating system, database installation, configuration, patching, and operational tooling, but also increases the team’s responsibility for maintaining the environment.

The networking principles are similar: use private subnets, narrowly scoped security groups, suitable routing, and secure administration paths.

RDS vs. self-managed databases

AreaAmazon RDSSelf-managed database on EC2
Operating system managementAWS-managedCustomer-managed
Database engine administrationShared responsibility, depending on taskCustomer-managed
Network configurationCustomer configures VPC, subnet group, and security groupsCustomer configures VPC, subnets, routes, and security groups
OS-level accessGenerally not providedAvailable with appropriate access
Patching and maintenanceManaged capabilities and customer-selected settingsCustomer-managed
FlexibilityService-specific limitationsGreater operating system and installation control

The right choice depends on operational requirements, compliance, database engine capabilities, cost, and the level of control the organization needs.

How Can DBAs Connect Securely to Private Databases?

Keeping a database private introduces an operational question: how can administrators access it when troubleshooting, running maintenance, or investigating performance problems?

There are several options.

Option 1: AWS Systems Manager Session Manager

AWS Systems Manager Session Manager can provide managed access to supported EC2 instances without requiring an inbound SSH port open to the internet.

The instance must satisfy the relevant Systems Manager prerequisites, including the required agent, permissions, and network connectivity to the service.

DBAs can use a managed access path to reach an approved host and run database administration tools.

Option 2: Bastion host

A bastion host is an intermediary server used to access private infrastructure.

An administrator connects to the bastion through an approved access path and then connects to the private database.

A bastion host must be carefully secured, monitored, and maintained. It should not become a general-purpose server with unrestricted access to every resource.

Option 3: VPN or corporate network connectivity

Organizations may connect an office or corporate network to AWS through an appropriately configured VPN or AWS Direct Connect architecture.

Administrators can then reach private database IP addresses through the established network path, subject to routing and security policies.

Which option is best?

For many AWS environments, Session Manager is worth evaluating because it can reduce the need for publicly reachable SSH access.

The right solution depends on the organization’s security requirements, existing network architecture, operational workflows, and compliance obligations.

Regardless of the access method, database authentication and authorization must still be enforced. Network access alone should not grant unrestricted database privileges.

Troubleshooting Common Database Connectivity Issues

Database connection problems are common in production environments. A useful troubleshooting process checks each layer methodically instead of immediately changing firewall rules.

Let’s look at several common scenarios.

Scenario 1: MongoDB connection is refused

Suppose a MongoDB application reports:

Connection refused

This often indicates that the destination is reachable but no process is accepting the connection at that address and port, or that a firewall or intermediary is actively rejecting it.

Check the configured listener:

sudo ss -lntp | grep ':27017'

If MongoDB uses a custom port, replace 27017 with the configured value.

Review the MongoDB configuration, including the net.port and net.bindIp settings, and check the database service status.

Also verify that the application is connecting to the correct private IP address and port.

A MongoDB instance listening only on 127.0.0.1 will not accept connections from remote application servers through its private network interface.

Do not solve this by exposing MongoDB to the public internet. Configure the appropriate private listener and narrowly scoped network access instead.

Scenario 2: Database connections fail across VPCs

Suppose the application and database are deployed in separate VPCs.

Possible causes include:

  • No VPC peering connection or other connectivity mechanism.
  • Missing routes in one or both directions.
  • Overlapping VPC CIDR ranges.
  • Security groups that do not permit the connection.
  • Restrictive network ACLs.
  • Incorrect DNS configuration.

For VPC Peering, both VPCs need the appropriate routes to the peer’s CIDR range. VPC Peering does not support transitive routing.

For more complex environments, verify the Transit Gateway attachments and route tables if Transit Gateway is used.

Scenario 3: Database connections are slow

A slow database connection does not always indicate a database performance problem.

Investigate:

  • DNS resolution delays.
  • Packet loss or network latency.
  • Cross-Region or cross-network communication.
  • Connection pool exhaustion.
  • Database connection limits.
  • TLS negotiation and authentication overhead.
  • Server resource saturation.

Tools such as ping, traceroute, and mtr can be useful where permitted, but their results must be interpreted carefully. Network devices or security policies may block ICMP, and successful ping responses do not prove that a database TCP port is reachable.

Use database-specific monitoring and application logs alongside network diagnostics.

Scenario 4: Security group rules appear correct, but connections still fail

A security group is only one part of the network path.

Check the following:

  1. Is the correct security group attached to the intended network interface?
  2. Does the database listen on the correct address and port?
  3. Are the source and destination IP addresses correct?
  4. Are the route tables configured properly?
  5. Do NACL rules permit both request and return traffic?
  6. Is a host firewall blocking the connection?
  7. Is the database rejecting the connection because of authentication or TLS settings?

AWS VPC Flow Logs can help investigate network traffic associated with VPCs, subnets, and network interfaces. They capture information about IP traffic and can help identify accepted or rejected flows.

They are useful for diagnosing network access problems, although they do not capture every detail of an application-level failure.

Network ACLs and Database Connectivity

Network ACLs provide an additional subnet-level traffic filtering layer.

Unlike security groups, NACLs are stateless. Inbound and outbound traffic must be permitted independently.

For example, allowing inbound MySQL traffic on TCP port 3306 does not automatically permit the return traffic through a restrictive NACL.

The return traffic may use ephemeral client-side TCP ports, so the applicable outbound and inbound rules must account for the connection flow.

Security groups are often the primary resource-level control for database connectivity, while NACLs provide an additional subnet-level policy where needed.

When troubleshooting a connection, avoid adding broad NACL permissions without understanding the required traffic. Instead, identify the source, destination, protocol, destination port, and return path before changing rules.

High Availability and Database Networking Across Availability Zones

Production databases often require more than basic connectivity. They also need resilience against infrastructure failures.

AWS Availability Zones are isolated locations within a Region. Deploying appropriate components across multiple Availability Zones can improve availability.

For example, a production architecture might include:

  • Application servers in two Availability Zones.
  • A load balancer distributing traffic to healthy application instances.
  • A database deployment configured for the required availability and recovery objectives.
  • Monitoring and alerting for database health.
  • Backups and tested recovery procedures.

For Amazon RDS, Multi-AZ configurations can provide managed high availability, depending on the database engine and deployment type.

For self-managed MySQL, PostgreSQL, or MongoDB deployments on EC2, high availability requires an appropriate database replication and failover design in addition to network connectivity.

Networking must support communication between database nodes, monitoring systems, and application components. Those communication paths should be restricted to the necessary sources and ports.

A multi-AZ network alone does not guarantee database high availability. Replication, failover behavior, application reconnection, backup recovery, and operational testing are also essential.

Conclusion

AWS networking is a critical part of secure and reliable database operations.

VPCs provide the network foundation, subnets organize application and database resources, route tables determine traffic paths, and security groups control which resources can communicate. Private connectivity options help administrators manage databases without exposing them unnecessarily to the public internet.

For database support teams, these concepts are directly connected to daily operational tasks, including investigating connection timeouts, resolving firewall issues, securing database ports, supporting high availability, and maintaining production infrastructure.

The most important principle is simple: make database connectivity available to the systems that need it, and restrict it everywhere else.

By combining a well-planned AWS network with database security, monitoring, and operational best practices, organizations can build infrastructure that is easier to troubleshoot, more secure, and better prepared for production workloads.