--Note on how to connect to RDS from the Docker environment on your local PC --In Docker development, it seems that you often create a container image of DB, but for some reason you want to use AWS RDS directly.
--Build a VPC environment and create an EC2 instance and RDS for connecting Rails applications to the Private subnet. --Service by Nat gateway or Endpoint "com.amazonaws.us-east-1.ssm Can be connected to --Ssm-agent is installed on your EC2 instance --Connect to EC2 instance with SSM without using a bastion server etc.
--Add the settings for SSM connection to the ssh config file
config
Host EC2 server instance ID]
User [EC instance connection user]
Port 22
ServerAliveInterval 300
IdentityFile ~/.ssh/id_rsa
ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"
--ssm Check ssh connection with ssm
$ ssh [EC2 server instance ID]
--aws profile settings --Required when creating a tunnel by port forwarding
$ aws configure --profile=ssm-test
--Set the following in ssh-config
# SSH over Session Manager
host i-* mi-*
ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p' --profile=ssm-test"
--Creating a tunnel
ssh -i ~/.ssh/id_rsa [EC instance connection user]@[EC2 server instance ID] -L 13306:[RDS endpoint]:3306
--Perform connection check to rds
mysql -u [DB connection user] -h 127.0.0.1 -P 13306 -p
--Specify the port number (13306) when port forwarding to RDS. --Specify "docker.for.mac.localhost" for host
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: [RDS connection user]
password: [RDS connection password]
host: docker.for.mac.localhost
port: 13306
With the above settings, you can connect to RDS from a Rails application started in a Docker container. When using for SSM connection and port forwarding, it is not necessary to release SG and NACL port numbers (22, 13306).
Recommended Posts