Skip to content

Instantly share code, notes, and snippets.

@jadkik
Created January 9, 2019 13:20
Show Gist options
  • Save jadkik/d7b0805d4cdd876f614af20e87c4c62a to your computer and use it in GitHub Desktop.
Save jadkik/d7b0805d4cdd876f614af20e87c4c62a to your computer and use it in GitHub Desktop.
Connect to your RDS database that was created by Elastic Beanstalk
#!/bin/bash -ex
# ssh-add ~/path/to/aws-key-file.pem
LOCAL_PORT=${1:-15432}
EB_ENV=`mktemp`
trap 'rm -f $EB_ENV; exit' SIGINT SIGQUIT SIGKILL SIGTERM
# See https://serverfault.com/a/883673/373074
eb ssh --quiet -e 'ssh' -c 'cat /opt/python/current/env' > "$EB_ENV"
source "$EB_ENV"
rm -f "$EB_ENV"
# See https://gist.github.com/scy/6781836
eb ssh --quiet -e "ssh -L $LOCAL_PORT:$RDS_HOSTNAME:$RDS_PORT -o ExitOnForwardFailure=yes -T -f" -c 'sleep 10'
# If running psql on remote machine:
#. /opt/python/current/env
#export PGHOST="$RDS_HOSTNAME"
#export PGPORT="$RDS_PORT"
# If running psql here, on local machine:
export PGHOST="localhost"
export PGPORT="$LOCAL_PORT"
export PGPASSWORD="$RDS_PASSWORD"
export PGUSER="$RDS_USERNAME"
export PGDATABASE="$RDS_DB_NAME"
#export PGSSLMODE=disabled
# psql has to attempt connection in the 10 seconds after the ssh tunnel command returns (i.e. when the tunnel is established and the sleep command is still running)
exec psql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment