Introduction
In the previous post, we discussed how to use the AWS VPN client to connect to private subnets in a VPC. We used Entra ID to perform federated user-based authentication. This technique is useful for enterprises using federated authentication, but there is another way to achieve the same result using AWS Systems Manager Session Manager (SSM) and port forwarding.
Architecture
The diagram above shows the main components used for the SSM port forwarding session setup. The diagram shows the following components:
- The bastion host EC2 instance that is located in the private subnet and that is used to initiate the port forwarding session with the attached IAM role
- The EC2 instance security group that allows inbound traffic only from the VPC CIDR range
- The RDS instance in the private subnet that will be used as the target for the port forwarding session
- The RDS security group that allows inbound traffic on the DB port (5432) only from the bastion host EC2 instance
- The SSM session manager document that will be used to initiate the port forwarding session
- The NAT gateway that will be used to route traffic from the private subnet to the internet
- The development local machine that will be used to connect to the RDS instance
RDS DB Connection with SSM Port Forwarding
The following parameters are required for the port forwarding session:
- AWS Region (e.g. eu-central-1)
- EC2 instance id (e.g. i-04e29d6b82ca52fbc)
- RDS host (e.g. mytestdb-instance-1.clzjs8sy98st.eu-central-1.rds.amazonaws.com)
- RDS port (e.g. 5432)
- RDS username (e.g. postgres)
- RDS password (e.g. arn:aws:secretsmanager:eu-central-1:123456789012:secret:rds!)
- RDS cluster identifier (e.g. cluster-506ac9d4-c6f0-5421-911f-85dec405f14a-A12ncL)
- RDS DB name (e.g. mytestdb)
Install the session manager aws cli plugin locally and start a new session. Keep the session running and execute further commands from a new terminal session:
| |
Export the correct AWS region and RDS parameters in a new terminal session to connect from a local client:
| |
The PGPASSWORD environment variable is automatically picked up by psql when connecting to a PostgreSQL server.
Connect using psql to localhost and port 5432 that match the forwarded session that was initiated:
| |
The connection was established, and the describe table command shows the proper output.
Conclusion
This post demonstrates how to use AWS Systems Manager Session Manager to establish a port forwarding session to a remote DB host. This is useful for scenarios where you need to connect to a remote DB instance from a local development machine for debugging or testing purposes. No ssh connection or public internet-facing endpoints are required for this scenario which makes it a practical solution with minimal overhead.
References: