12/31/2023 • VirtueCloud

Traditionally, accessing AWS RDS instances in private subnets required a bastion host in a public subnet and SSH keys for secure access. With AWS Systems Manager (SSM) and AWS CLI, you can simplify this process and securely access RDS instances without the need for a bastion host or SSH keys.
To follow this guide, ensure you have:
SSM Agent: Installed and running on the EC2 instance.Note: The SSM Agent is pre-installed on most Amazon Machine Images (AMIs). For custom AMIs, manual installation may be necessary.
On Windows: Download and install the AWS CLI from the official AWS CLI download page.
On macOS:
brew install awscliOn Linux:
sudo apt-get update
sudo apt-get install
awscli -y Verify the Installation: Verify the Installation: Run the following command to confirm the AWS CLI version is installed correctly.
aws --version
Configure AWS CLI
To set up the AWS CLI, you'll need your AWS Access Key ID, Secret Access Key, default region, and output format.
Run the following command:
aws configure
When prompted, enter the following details:

Install Session Manager Plugin
session-manager-plugin --version Create an IAM Role in AWS Console




Launch an EC2 Instance and Attach the IAM Role



Note: Ensure your VPC has a NAT Gateway if you're selecting a private subnet for AWS Systems Manager (SSM) connectivity. Without a NAT Gateway, opt for a public subnet to ensure internet access for the EC2 instance.

To begin an SSM port forwarding session, use the command below:

Note : Substitute instance-id and rds-endpoint with your EC2 instance ID and the endpoint of your RDS database respectively.
Example Commands:






Note: Even though this setup connects directly to the AWS RDS database without needing a bastion host, an EC2 instance (or other compute resources like ECS or EKS) within the same VPC as the RDS instance is still required for connectivity. However, you don't need to create a separate EC2 instance just for database access. Any EC2 instance, ECS worker, or EKS node within the VPC can be used to connect to the RDS database, ensuring a streamlined and efficient setup.
In this guide, we explored a secure and streamlined approach to accessing AWS RDS instances in private subnets using AWS Systems Manager (SSM) and the AWS CLI. By eliminating the need for traditional bastion hosts and SSH keys, this method simplifies infrastructure management while enhancing security.
With SSM port forwarding, you can securely connect to your RDS database without exposing sensitive credentials or managing intermediary hosts. Additionally, leveraging IAM roles ensures proper access control and follows AWS best practices for least privilege.
As organizations continue to prioritize cloud security and operational efficiency, adopting tools like AWS Systems Manager becomes essential. Whether you're managing databases in production or accessing instances in development environments, this method offers a scalable and secure solution.
Related articles you might find interesting

Why platform teams are re-routing north-south traffic through the Kubernetes Gateway API, what HTTPRoute changes on the ground, and how to migrate without a big-bang rewrite.


Hassle-Free ECS: Terraform Automation + CI/CD Pipeline