{"id":7641,"date":"2018-06-25T00:05:05","date_gmt":"2018-06-24T16:05:05","guid":{"rendered":"http:\/\/rmohan.com\/?p=7641"},"modified":"2018-06-25T00:05:05","modified_gmt":"2018-06-24T16:05:05","slug":"ssh-tunnel-for-rds-aws","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7641","title":{"rendered":"ssh tunnel for RDS AWS"},"content":{"rendered":"<h1 id=\"f6a1\" class=\"graf graf--h3 graf--leading graf--title\">ssh tunnel for RDS via bastion\u00a0host<\/h1>\n<p id=\"dd4b\" class=\"graf graf--p graf-after--h3\">Our RDS db is hosted on Amazon. Our Bastion(Jumphost) can connect to the db. Connections to the db are not allowed outside of the internet.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"a413\" class=\"graf graf--h3 graf-after--figure\">Run ssh tunnel\u00a0locally:<\/h3>\n<p id=\"41b2\" class=\"graf graf--p graf-after--h3\">This creates a tunnel from my local machine to the Bastion:<\/p>\n<pre id=\"8c62\" class=\"graf graf--pre graf-after--p\"><code class=\"markup--code markup--pre-code\">ssh -N -L 3307:my-rds-db.us-east-1.rds.amazonaws.com:3306 ec2-my-bastion-server.compute-1.amazonaws.com<\/code><\/pre>\n<p id=\"2157\" class=\"graf graf--p graf-after--pre\">This will forward port\u00a0<code class=\"markup--code markup--p-code\">3307<\/code>\u00a0from your local desktop to the remote MySQL rds server through your Public facing bastion EC2 instance.<\/p>\n<p id=\"e117\" class=\"graf graf--p graf-after--p\">You can easily set up this tunnel every time you log into your remote EC2 instance and log into it with whatever name you prefer:<\/p>\n<h3 id=\"ab03\" class=\"graf graf--h3 graf-after--p\">Add this to\u00a0.ssh\/config:<\/h3>\n<pre id=\"7836\" class=\"graf graf--pre graf-after--h3\"><code class=\"markup--code markup--pre-code\">Host my_instance\r\n  Hostname bastion-ip\r\n  Localforward 3307 my-rds-db.us-east-1.rds.amazonaws.com:3306<\/code><\/pre>\n<p id=\"6737\" class=\"graf graf--p graf-after--pre\">Then, just:<\/p>\n<pre id=\"eb35\" class=\"graf graf--pre graf-after--p\"><code class=\"markup--code markup--pre-code\">ssh my_instance<\/code><\/pre>\n<h3 id=\"0139\" class=\"graf graf--h3 graf-after--pre\">Connect to db using your favorite db interface.<\/h3>\n<p id=\"6f11\" class=\"graf graf--p graf-after--h3\">An example using\u00a0<code class=\"markup--code markup--p-code\">mysql<\/code>:<\/p>\n<pre id=\"ea57\" class=\"graf graf--pre graf-after--p\"><code class=\"markup--code markup--pre-code\">$ mysql -uusername -h 127.0.0.1 -P 3307 -p<\/code><\/pre>\n<p id=\"039f\" class=\"graf graf--p graf-after--pre\">For more info\u00a0<code class=\"markup--code markup--p-code\">man ssh<\/code>:<\/p>\n<pre id=\"83ee\" class=\"graf graf--pre graf-after--p graf--trailing\"><code class=\"markup--code markup--pre-code\">-L [bind_address:]port:host:hostport\r\n Specifies that the given port on the local (client) host is to be forwarded to the given \r\n host and port on the remote side.  This works by allocating a socket to listen to port on \r\n the local side, optionally bound to the specified bind_address.  Whenever a connection is \r\n made to this port, the connection is forwarded over the secure channel, and a connection \r\n is made to host port hostport from the remote machine.  Port forwardings can also be \r\n specified in the configuration file.  IPv6 addresses can be specified by enclosing the \r\n address in square brackets.  Only the superuser can forward privileged ports.  By default, \r\n the local port is bound in accordance with the GatewayPorts setting.  However, an explicit \r\n bind_address may be used to bind the connection to a specific address.  The bind_address of \r\n ``localhost'' indicates that the listening port be bound for local use only, while an empty \r\n  address or `*' indicates that the port should be available from all interfaces.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ssh tunnel for RDS via bastion host <\/p>\n<p id=\"dd4b\" class=\"graf graf--p graf-after--h3\">Our RDS db is hosted on Amazon. Our Bastion(Jumphost) can connect to the db. Connections to the db are not allowed outside of the internet.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p> Run ssh tunnel locally: <\/p>\n<p id=\"41b2\" class=\"graf graf--p graf-after--h3\">This creates a tunnel from my local machine to [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7641"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7641"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7641\/revisions"}],"predecessor-version":[{"id":7642,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7641\/revisions\/7642"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}