AWS DocumentDB SSH

default discord avatar
jonnyf.27last year

In order to connect to an AWS DocumentDB during development, you need to SSH into an EC2 instance as DocumentDB connections are only allowed on the same VPC. Is there a way to set up an SSH tunnel with Payload?



For anyone else needing to answer this:



- You first need to set up an EC2 instance on AWS to provide your SSH Tunnel. Make sure this is one the same VPC as your AWS DocumentDB and correct security group settings applied



- You then need to set up SSH port forwarding on your local machine with the following:


ssh -i ssh.pem -L localhost:<LOCAL_PORT>:<AWS_DOCUMENT_DB_CONNECTION_STRING> -p 22 <SSH_USERNAME>@<SSH_INSTANCE_DNS>


- The pem file will be your own private key generated from AWS



- You then need to update your MongoDB connection string. Its important to include

directConnection

otherwise it will error:


mongodb://localhost:27000?directConnection=true


- Include your auth for AWS DocumentDB in the Payload

mongoOptions

:


payload.init({
  secret: process.env.PAYLOAD_SECRET,
  mongoURL: isProduction
    ? process.env.MONGODB_URI_PROD
    : process.env.MONGODB_URI_DEV,
  mongoOptions: {
    user: process.env.MONGODB_USER,
    pass: process.env.MONGODB_PASSWORD,
    retryWrites: false, // AWS DocumentDB doesn't support retryWrites
  }
  ...
  })
    Star on GitHub

    Star

    Chat on Discord

    Discord

    online

    Can't find what you're looking for?

    Get help straight from the Payload team with an Enterprise License.