analytics pipeline
The tunnel that starts on our side

Somebody wants a revenue dashboard. The revenue lives in a document database inside a VPC, reachable through a private endpoint, with no public listener and no route in from anywhere. The tool that is going to draw the dashboard is somebody else's SaaS, and so is the thing that will load the data into the warehouse it reads from.
There are two ways to do it. Either the vendor comes in, which means a bastion, an inbound rule and an allow-list of their egress addresses that they change when they feel like it, or we go out.
We went out, and I still think that was the right call, but the reasons go beyond "outbound is safer" and the bill is bigger than it looks.
Why a connector at all
The obvious objection first. The database already emits change streams, and the platform already consumes them for product features, so why pay someone to read a database we can read ourselves?
Because a warehouse feed is a different job from a product feature. A product feature cares about what changed in the last few seconds and is allowed to be lossy at the edges. A warehouse feed has to do a full historical backfill without knocking the cluster over, resume from a definite position after an outage, notice that a field started appearing in documents halfway through last year, and re-sync a collection from scratch when somebody decides the transformation upstream was wrong. Every one of those is a week, and the last one is a week that keeps coming back.
For a platform team that isn't a data platform team, buying that is the cheaper answer. What we still own is everything below, getting the vendor to the database at all.
Reverse, not forward
A managed ingest connector will normally offer to reach a private database over SSH. The default reading of that is forward. They hold a key, they connect to a bastion we expose, and they hop from there to the database.
That is a door. It's an inbound security-group rule, a public listener that exists whether or not a sync is running, and an authentication decision made by our host about a connection that someone else's host initiated. Any of those can be got wrong, and the allow-list is maintained by the party on the other side.
The other direction works like a phone call. We run ssh -R outwards to a
host the vendor operates, using an account they issue us, and publish the
database's port on their side of the connection:
our VPC vendor
┌──────────────────────┐ ┌──────────────────┐
│ tunnel host ─────────┼── ssh ──►│ :27018 │
│ │ │ -R │ ▲ │
│ ▼ │ │ │ │
│ mongod :27017 │ │ connector reads │
└──────────────────────┘ └──────────────────┘
Nothing on our side listens for them. There's no inbound rule, no public database endpoint, no allow-list to keep in sync. If the relationship ends we stop the container and the access ends with it, and we don't have to coordinate with anyone, open a ticket or wait for someone else to remove a key.
What we hold instead is a private key that logs into their host. That's a credential worth protecting, but the most it gives away is an SSH account on a vendor machine. It is not a foothold in our network.
The tunnel is a container
The mechanism is just autossh in a container, written out by Terraform
through a remote-exec provisioner:
services:
reverse-tunnel:
image: jnovack/autossh
environment:
- SSH_REMOTE_USER=<vendor account>
- SSH_REMOTE_HOST=<vendor host>
- SSH_REMOTE_PORT=22
- SSH_TUNNEL_PORT=27018 # the port they connect to
- SSH_TARGET_HOST=<resolved mongod host>
- SSH_TARGET_PORT=27017
restart: always
volumes:
- ./auto-ssh.key:/id_rsa
dns: [8.8.8.8, 1.1.1.1]
Two details in there need explaining.
The tunnel runs under autossh instead of plain ssh under a restart policy.
restart: always handles a process that exits. It does nothing for the failure
you get in practice, a TCP connection that is dead without being closed (a NAT
table entry expiring, a middlebox silently dropping an idle flow). The SSH
process is still running and still happy, and nothing arrives. autossh exists
to detect that. It runs its own traffic through the tunnel and restarts when
the echo stops coming back.
The public resolvers are pinned so the container can resolve the vendor's hostname, and they are a small wart. The container doesn't use the VPC resolver (that was intended), so the one name it can't resolve is anything private. That's fine only because the target host is resolved elsewhere, and that is the next problem.
The target is an SRV record, and we take the first one
Behind a private endpoint the database is an SRV record, not a hostname, and the host it points at is the vendor's business. They rotate it, they replace members during maintenance, and the port isn't necessarily the obvious one.
So the module resolves the record at plan time and pulls the target and port out of it:
data "dns_srv_record_set" "service" {
count = var.target_srv_record == null ? 0 : 1
service = "_mongodb._tcp.${var.target_srv_record}"
}
locals {
target_address = substr(
data.dns_srv_record_set.service[0].srv[0].target,
0,
length(data.dns_srv_record_set.service[0].srv[0].target) - 1,
)
target_port = data.dns_srv_record_set.service[0].srv[0].port
}
The substr is trimming the trailing dot off the fully-qualified name. It's
the sort of line that tells you how it was discovered.
Look at the index, though. srv[0]. A replica set publishes one SRV record per
member, and this picks one of them and pins the tunnel to it for as long as the
container runs.
So the tunnel points at a single node. When that node is replaced (and managed clusters replace nodes on their own schedule) the tunnel keeps dialling a name that no longer serves, and the fix is to run Terraform again. That is a manual maintenance chore and it doesn't announce itself. The sync just stops.
Plan-time resolution also puts DNS into the diff. Any plan, for any reason, can come back non-empty because the cluster reshuffled overnight. Half the time that's the system telling you something useful. The rest of the time it is noise in a plan you were reading for something else.
Neither is fatal, and I would still resolve the record before I'd hardcode a host. But having Terraform resolve DNS does couple every plan to the current state of the cluster.
Three clusters, three ports, and a directory collision
There is more than one cluster to feed, and the remote port namespace belongs to the SSH account and not to us, so the ports are an allocation more than a configuration:
module "reverse-tunnel" {
port_on_remote = 27018
target_srv_record = "<analytics cluster>"
# ...
}
module "reverse-tunnel-hybrid" {
port_on_remote = 27118
target_srv_record = "<second cluster>"
# ...
}
The first version of the module wrote its compose file to one fixed path. The
second tunnel overwrote the first's, and docker-compose up -d --force-recreate
in that directory then cheerfully replaced the running service with the new one.
Two tunnels were declared and only one ran, with no error anywhere, and the
feed that stopped was simply the one whose Terraform ran first. Keying the
project directory by the remote port fixed it. The module took plenty of
variables but wrote everything to one place on disk.
The other one that cost an afternoon was the key file. The file provisioner
wrote it with no trailing newline, and ssh rejected it outright. The fix is
echo '' >> the key. It lives in the code with a comment, because without one
it looks like a mistake.
Where the tunnel lives
On the VPN instance. It already existed, it had a public address and a foot in
the VPC, it ran Docker, and it is a t3a.nano doing almost nothing. Adding a
container to it cost nothing and shipped that week.
The cost is that one instance is now both the path every engineer uses to reach the environment and a working part of the data pipeline. Reboot it for the first reason and you break the second. We wrote that down and did not fix it.
The VPN underneath
Since the same box carries both, the VPN configuration matters here too. There are two choices in it that I'd repeat and two I wouldn't.
It's a split tunnel, and we meant it to be. The client profile generator strips the line that would send all traffic over the VPN and appends an explicit route for the VPC:
echo "route $(cidrhost $vpc_cidr 0) $(cidrnetmask $vpc_cidr)" >> $user.ovpn
sed -i '/redirect-gateway def1/d' $user.ovpn
An engineer's entire internet shouldn't go through a nano instance in one region. And with a full tunnel the VPN becomes a dependency for work that has nothing to do with the VPC.
It pushes the VPC resolver, read from the instance's own resolv.conf. Without
it the client gets a route and no names. Private endpoints resolve to nothing,
and what you see is a connection that hangs, with no error to explain it. I
think this is the most common way a split tunnel gets shipped broken.
The access check is a name whitelist evaluated at TLS time. A tls-verify
script greps the certificate's common name against a file:
/bin/grep -q "^`expr match "$3" ".*CN=\([^,]*\)"`$" "$1" && exit 0
Revocation is what a CRL is for, and a whitelist is a cruder instrument. It's a
second list that has to agree with the first, and the day they disagree the
answer is ambiguous. It is also immediate and easy to read, and that's why it
is there. Enabling it needs script-security 2. The config sets 3, which
additionally permits passing passwords to scripts through the environment. We
didn't need that, and it survived because it worked.
The CA key has no passphrase (ovpn_initpki nopass), because provisioning is
automated and a passphrase would mean a human in the loop of every apply. That
is a real trade, and it should be written down next to the decision and not
left for whoever reads the volume later to discover.
The OpenVPN image this box runs is a community one that hasn't seen a release in a long while. It works. It's also a dependency with no maintainer sitting on the path into the environment, and "it works" is the argument that will keep it there for another year.
Handing out access without handing out a shell
The most recent addition is the least clever one and has made the most difference. We don't log into the VPN host to mint a profile any more. A workflow does it through Systems Manager:
aws ssm send-command \
--instance-ids "$INSTANCE_ID" \
--document-name "AWS-RunShellScript" \
--comment "Create VPN user" \
--parameters commands="cd ~/ovpn && ../scripts/new_vpn_client.sh $VPN_NAME"
and the resulting profile is delivered as a Slack direct message, with the
recipient resolved from their email address. The profile file is the
credential, so this isn't as tidy as it sounds. It now lives in a chat history
we do not control, and what we have for control is rotation, not secrecy.
Profiles are named <user>_<env>_<ddmmyyyy> so that an old one is visibly old.
That's about the minimum you can do when the credential is a file somebody
downloaded once.
The piece I like most only reads. A second script lists the whitelist, lists the issued certificates, intersects them, and emits the result as YAML:
listCNs="sudo cat /var/lib/docker/volumes/openvpn-data-default/_data/CN_whitelist"
listCRTs="sudo ls -1 .../pki/issued/*.crt | xargs -n 1 basename"
Access becomes a file. You can diff it between weeks and review it in a pull request, and it answers "who can reach production" without anyone logging into anything.
The two-source problem from earlier bites here too. An entry in one list and not the other silently disappears from the output, and an empty whitelist produces an empty file that looks just like a failed run.
What the complexity bought
No analytics requirement ever turned into a firewall exception. The vendor never
had a route into the network, the database never acquired a public endpoint,
and revoking all of it is docker stop.
The costs are a tunnel pinned to a node that can be replaced underneath it, a nano instance doing two unrelated jobs, and an access-control story with one list too many in it. I'd pay those again in the same order. But I would write them into the module's README on the day they were introduced instead of into a blog post two years later.


