Connect to AWS RDS from your local PC Docker environment

Overview

--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.

Premise

--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.

Local PC environment

reference

SSM settings

--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]

Connecting to RDS by port forwarding

--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

Rails settings

database.yml settings

--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

Connect to AWS RDS from your local PC Docker environment
Connect to AWS RDS from your local PC Docker environment
Temporarily move Docker environment from Mac to AWS
How to migrate a web application created in a local docker environment to AWS
How to deploy to Heroku from a local docker image
List how to learn from Docker to AKS on AWS
Test processing with AWS KMS on your local PC
Build WordPress environment with Docker (Local) and AWS (Production)
Moving from AWS to PaizaCloud
Migrating from vargrant to docker
Connect from Java to PostgreSQL
[Docker context] ~ How to access docker in remote environment from VScode ~
Connect from local to remote host JMX via SSH port forwarding
PostgreSQL environment construction with Docker (from setup to just before development)
Build Couchbase local environment with Docker
To beginners launching Docker on AWS
Pass environment variables to docker container
Update MySQL from 5.7 to 8.0 with Docker
[Environment construction] Uninstall rails from local
docker + mysql can't connect / Can't connect to local MySQL server through socket'var / run / mysqld / mysqld.sock' (2)
How to apply Rails environment variables to Docker container (aws :: Sigv4 :: Errors solution)
[For beginners] Laravel Docker AWS (EC2) How to easily deploy Web application (PHP) from 0 (free) ②-Docker development environment construction-
Deployed using Docker + Rails + AWS (EC2 + RDS)
Investigate the replacement from Docker to Podman.
Migrate existing Rails 6 apps to Docker environment
Create a MySQL environment with Docker from 0-> 1
Build a WAS execution environment from Docker
Connect from Java to MySQL using Eclipse
[Docker] Copy files from docker container to host
[Note] Update to production environment (AWS EC2)
Build Cakephp environment from docker installation Ubuntu
Build an environment with Docker on AWS
Try the Docker environment on AWS ECS
How to build Rails 6 environment with Docker
How to connect to lcalhost from your smartphone and use the app under development
Connect with VS Code from a Windows client to Docker on another server
I was addicted to not being able to connect to AWS-S3 from the Docker container
How to build a Jenkins server with a Docker container on CentOS 7 of VirtualBox and access the Jenkins server from a local PC