Connect to a private database using Tunnel
BetaHyperdrive can securely connect to your private databases using Cloudflare Tunnel and Cloudflare Access.
When your database is isolated within a private network (such as a virtual private cloud ↗ or an on-premise network), you must enable a secure connection from your network to Cloudflare.
- Cloudflare Tunnel is used to establish the secure tunnel connection.
- Cloudflare Access is used to restrict access to your tunnel such that only specific Hyperdrive configurations can access it.
All of the tutorials assume you have already completed the Get started guide, which gets you set up with a Cloudflare Workers account, C3 ↗, and Wrangler.
- A database in your private network, configured to use TLS/SSL.
- A hostname on your Cloudflare account, which will be used to route requests to your database.
First, create a Cloudflare Tunnel in your private network to establish a secure connection between your network and Cloudflare. Your network must be configured such that the tunnel has permissions to egress to the Cloudflare network and access the database within your network.
-
Log in to Zero Trust ↗ and go to Networks > Tunnels.
-
Select Create a tunnel.
-
Choose Cloudflared for the connector type and select Next.
-
Enter a name for your tunnel. We suggest choosing a name that reflects the type of resources you want to connect through this tunnel (for example,
enterprise-VPC-01
). -
Select Save tunnel.
-
Next, you will need to install
cloudflared
and run it. To do so, check that the environment under Choose an environment reflects the operating system on your machine, then copy the command in the box below and paste it into a terminal window. Run the command. -
Once the command has finished running, your connector will appear in Zero Trust.
-
Select Next.
Your tunnel must be configured to use a public hostname so that Hyperdrive can route requests to it. If you don't have a hostname on Cloudflare yet, you will need to register a new hostname or add a zone to Cloudflare to proceed.
-
In the Public Hostnames tab, choose a Domain and specify any subdomain or path information. This will be used in your Hyperdrive configuration to route to this tunnel.
-
In the Service section, specify Type
TCP
and the URL and configured port of your database, such aslocalhost:5432
. This address will be used by the tunnel to route requests to your database. -
Select Save tunnel.
The service token will be used to restrict requests to the tunnel, and is needed for the next step.
-
In Zero Trust ↗, go to Access > Service auth > Service Tokens.
-
Select Create Service Token.
-
Name the service token. The name allows you to easily identify events related to the token in the logs and to revoke the token individually.
-
Set a Service Token Duration of
Non-expiring
. This prevents the service token from expiring, ensuring it can be used throughout the life of the Hyperdrive configuration. -
Select Generate token. You will see the generated Client ID and Client Secret for the service token, as well as their respective request headers.
-
Copy the Access Client ID and Access Client Secret. These will be used when creating the Hyperdrive configuration.
Cloudflare Access will be used to verify that requests to the tunnel originate from Hyperdrive using the service token created above.
-
In Zero Trust ↗, go to Access > Applications.
-
Select Add an application.
-
Select Self-hosted.
-
In Application Configuration > Application name, enter any name for the application.
-
In Application Configuration > Session Duration, select
No duration, expires immediately
. -
In Application Configuration > Application domain, enter the subdomain and domain that was previously set for the tunnel application.
-
In Application Appearance, disable the
Enable App in App Launcher
setting. -
In Identity providers, disable the
Accept all available identity providers
setting and selectDeselect all
identity providers. -
Select Next.
-
Enter a name in the Policy name and set the Action to
Service Auth
. -
In Configure rules, create an Include rule. Specify a Selector of
Service Token
and the Value of the service token you created in step 2. Create a service token. -
Select Next.
-
Select Add application to create the Access application.
To create a Hyperdrive configuration for your private database, you'll need to specify the Access application and Cloudflare Tunnel information upon creation.
This will create a Hyperdrive configuration using the usual database information (database name, database host, database user, and database password).
In addition, it will also set the Access Client ID and the Access Client Secret of the Service Token. When Hyperdrive makes requests to the tunnel, requests will be intercepted by Access and validated using the credentials of the Service Token.
To test your Hyperdrive configuration to the database using Cloudflare Tunnel and Access, use the Hyperdrive configuration ID in your Worker and deploy it.
You must create a binding for your Worker to connect to your Hyperdrive configuration. Bindings allow your Workers to access resources, like D1, on the Cloudflare developer platform. You create bindings by updating your wrangler.toml
file.
To bind your Hyperdrive configuration to your Worker, add the following to the end of your wrangler.toml
file:
Specifically:
- The value (string) you set for the
name
(binding name) will be used to reference this database in your Worker. In this tutorial, name your bindingHYPERDRIVE
. - The binding must be a valid JavaScript variable name ↗. For example,
binding = "hyperdrive"
orbinding = "productionDB"
would both be valid names for the binding. - Your binding is available in your Worker at
env.<BINDING_NAME>
.
Use Postgres.js to send a test query to validate that the connection has been successful.
Install the Postgres.js driver:
Create a new sql
instance and pass the Hyperdrive parameters:
Now, deploy your Worker:
If you successfully receive the list of pg_tables
from your database when you access your deployed Worker, your Hyperdrive has now been configured to securely connect to a private database using Cloudflare Tunnel and Cloudflare Access.
If you encounter issues when setting up your Hyperdrive configuration with tunnels to a private database, consider these common solutions, in addition to general troubleshooting steps for Hyperdrive:
- Ensure your database is configured to use TLS (SSL). Hyperdrive requires TLS (SSL) to connect.