rds-setsuzoku-chan (RDS接続ちゃん) is a utility script to establish a AWS Systems Manager - Session Manager port-forwarding connection for Amazon RDS via a bastion host. Since it uses Session Manager to connect to a bastion server, there is no need to manage SSH key pairs.
- AWS CLI is installed and configured with valid credentials
- Session Manager plugin for AWS CLI is installed
- AWS CLI output format is set to JSON (default is JSON, so you might be OK)
- A database client corresponding to the target RDS is installed
- A bash script is executable in the terminal
- jq is installed
- The bastion server is configured to be connectable via Session Manager
- RDS connection information is registered in AWS Secret Manager, and as the secret values, it must contain connection information as the following key / value pairs:
host
: RDS connection endpointport
: RDS connection portusername
: RDS connection usernamepassword
: RDS connection user's password- (optional)
engine
: RDS database engine. It is only used to display the example of the client CLI command to connect (currently only MySQL, Aurora MySQL, PostgreSQL and Aurora PostgreSQL are supported) - (optional)
database
: RDS default database name. It is only used to display the example of the client CLI command to connect too
Note that the above format of the connection information data in Secret Manager is compatible with the one set when configuring RDS with AWS CDK constructs.
- "EC2 instance name" or "EC2 instance ID" of the bastion server
- "Secret name" in Secrets Manager where RDS connection information is registered
Execute the script with:
- the specified secret name (
-s
or--secretname
) - either the bastion host instance name (
-n
or--bastionname
) or instance ID (-i
or--bastionid
) to start a session.
Example of specifying an EC2 instance name:
./rds-setsuzoku-chan -s <secret name> -n <bastion name>
Example of specifying an EC2 instance ID:
./rds-setsuzoku-chan -s <secret name> -i <bastion ID>
You will see kind of the following output:
-----------------------------------------------------------------------
RDS information
-----------------------------------------------------------------------
host: xxxxxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com
port: 3306
engine: mysql
dbname: db_name
-----------------------------------------------------------------------
Connection information
-----------------------------------------------------------------------
host: 127.0.0.1 | localhost
port: 43306
username: db_admin
password: *************************
-----------------------------------------------------------------------
You can connect to the database via MySQL client with the following command once session is started:
mysql -h 127.0.0.1 -P 43306 -u admin -p db_name
-----------------------------------------------------------------------
Establishing session...
Starting session with SessionId: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Now you can use your preferred database client to connect to the RDS via the port-forwarding connection.
By default, the database password is masked and not displayed in the output.
If you want to display the password in the output, use the --showpassword=yes
option.
There is --getinfo
option allows you to list registered EC2 instances and secrets in your configured AWS account / region.
./rds-setsuzoku-chan --getinfo
-s <Secret Name>
/--secretname=<Secret Name>
- Secret Manager secret name for database connection
-n <Bastion Instance Name>
/--bastionname=<Bastion Instance Name>
- Bastion EC2 instance name (either
-n
/--bastionname
or-i
/--instancename
must be specified)
- Bastion EC2 instance name (either
-i <Bastion Instance ID>
/bastionid=<Bastion Instance ID>
- Bastion EC2 instance ID (either
-n
/--bastionname
or-i
/--instancename
must be specified)
- Bastion EC2 instance ID (either
-p <Local Port>
/--port=<Local Port>
- (Optional) Local port number for port forwarding (Default to 43306)
--showpassword=yes
- Display the password in the output (Default to no)
--getinfo
- Show information of secrets and instances for the current configured AWS account / region
-h
/--help
- Show help
When using the built-in RDS Secret Manager integration feature, secrets are automatically registered and their values are managed by RDS. Unfortunately, the automatically configured secrets do not include information such as host endpoints or ports, so it is not possible to automatically start a session using the retrieved values from the secret.
However, since this script can retrieve information such as the username,
it is possible to execute ssm start-session
directly from the AWS CLI by
supplementing the missing information yourself.
aws ssm start-session \
--target <your bastion host instance id> \
--document-name AWS-StartPortForwardingSessionToRemoteHost \
--parameters host=<your RDS endpoint>,portNumber=<your RDS port>,localPortNumber=<local port you provided>
Since there is a possibility that the network is unstable and there is no guarantee that the content of the Gist will not be changed maliciously, this method is not recommended in general.
It is also possible to execute the script directly by specifying the script URL as follows:
curl -sSL https://gist.githubusercontent.com/5t111111/0d596d980be2a76ea20fe91eea6ca4ad/raw/rds-setsuzoku-chan | bash /dev/stdin -s <secret name> -n <bastion server instance name>
Of course, you can also use --getinfo
without downloading:
curl -sSL https://gist.githubusercontent.com/5t111111/0d596d980be2a76ea20fe91eea6ca4ad/raw/rds-setsuzoku-chan | bash /dev/stdin --getinfo