Skip to content
>GLB_
Go back

ecure Database Access in AWS Using SSH Tunneling

Accessing databases located in private subnets within AWS Virtual Private Clouds (VPCs) is a common requirement in enterprise architectures. To ensure secure connectivity without exposing the database to the public internet, developers and operations engineers often employ SSH tunneling via a bastion host.

Background

Databases in a private subnet cannot be accessed directly from external networks for security reasons. However, there are situations where engineers need to connect from a local development environment to such a database instance for debugging, data verification, or administrative purposes. SSH tunneling offers a secure and controlled mechanism to achieve this.

Architecture

The typical setup includes:

SSH Tunnel Command

To create an SSH tunnel from the local machine to the private database through the bastion, the following command is used:

`ssh -i /path/to/key.pem -L local_port:db_internal_endpoint:db_port ec2-user@bastion_public_ip`

For example, to forward the local port 5432 to a PostgreSQL instance accessible to the bastion on port 5432:

`ssh -i ~/.ssh/my-key.pem -L 5432:db.internal.local:5432 ec2-user@13.45.67.89`

This command creates a tunnel from localhost:5432 on the developer’s machine to the internal address db.internal.local:5432, routed through the bastion host.

Usage

Once the tunnel is established, the database can be accessed as if it were local:

`psql -h localhost -p 5432 -U db_user -d db_name`

Any client software (such as DBeaver, pgAdmin, or CLI tools) can connect to localhost on the forwarded port.

Notes

`ssh -f -N -i ~/.ssh/my-key.pem -L 5432:db.internal.local:5432 ec2-user@13.45.67.89`

Security Considerations

Alternatives

While SSH tunneling is effective for ad hoc access, it is not recommended for production workloads. Alternative approaches include:

Conclusion

SSH tunneling remains a valuable technique for secure, temporary access to private infrastructure in AWS. It should be used judiciously, following best practices and supplemented by more robust networking solutions for long-term or automated access.


Share this post:

Previous Post
How Google Changed Big Data: The Story of GFS, MapReduce, and Bigtable
Next Post
Did Early Personal Computers Really Have a CPU? A Look at the von Neumann Architecture