Hetzner to AWS
After taking a look at some of my growing subscriptions, I ended up spending a month or so trying to reduce my cloud costs. Whilst I generally self-host as much as I can locally, I have various tools hosted in clouds. I’ve been using a mixture of Hetzner and OVH, but these costs have grown to £40-50/month - which is not small for a hobby, especially where nearly all of my lab exists outside of these costs.
Most of the migrations were a split between:
- Moving instances to cheaper alternatives
- Moving services to my local servers
The last thing that remained was Gitlab runners. I have a couple of these, a mixture of ARM and AMD64 and they ran in Hetzner. Between them, they cost about £15/month - the main rational for them not being self-hosted was that my homelab is quite old (10+ year Xeons - mostly there for the RAM capacity).
After considering and trialing various options, I did what I thought I’d never do and move to AWS.
The main reason for this is the billing frequency. I don’t run CI/CD pipelines all the time, so using a scalable insance, I’d be able to create the machines when the pipeline runs and tear them down afterwards, which could be substancially cheaper than running a machine 24/7 on a cheap, but more static provider (like Hetzner).
To do this migration, I faced 3 interesting issues that I’d like to cover in this post…
1. Always on manager
The way Gitlab fleet autoscaling works is that you have a machine (a runner of sorts) that is configured with the ability to scale instances. So rather than it running the job itself, it spins up an instance and delegates it. Before reading the documentation, I didn’t really expect this to be how it worked - I half thought you’d provide some cloud credentials within Gitlab and it would just “do” it. Of course, in a big company, having some noddy machine running a pseudo-runner binary wouldn’t be an issue - but for me, trying to save costs, running a dedicated machine in AWS to just create other machines wasn’t going to happy. So I moved to running this inside my homelab, running on Nomad and it would spawn instances in AWS.
As far as services, go.. it started, read secrets, connected and basically consumed no memory/cpu and worked first try.. I couldn’t really have asked for anything more.
However, whilst it did appear to work, it leached out issues that will come back in a later part of this post..
2. Connectivity nightmares
The current architecture looks like this:
flowchart LR
subgraph homelab["Homelab"]
subgraph vms["VMs"]
subgraph nomad["Nomad"]
gitlab["GitLab"]
end
end
end
subgraph cloud["Cloud Provider"]
edge["Edge VMs"]
end
vms == "VPN tunnel" ==> edge
The normal approach would be to implement a site-to-site - AWS offers this, yes I’ve implemented this many times in my professional career… but of course, this “cheap” service will immediately blow the budget. Any gateway or anything that contains a VPC endpoint in AWS immediately costs $~35/month - peanuts for 6-7-figure AWS budget, but higher than I want my entire cloud bill. The “dirty” other solution that people consider when the AWS built-in features cost too much generally gear towards rolling your own - i.e. hosting your own version on an EC2 instance. Unfortunately, the cheapest EC2 instance is about $5/month - this obviously ignores high-availability and would likely require 2-3 instances and become closer to $15/month. The $15/month is definitely out of my budget and $5 is pushing it (I’ll come back to budget later).
I chose to, instead, use a mesh VPN, which would run on each of the Gitlab runners, meaning that nother had to be running in AWS other than the runners themselves.
This decision had several impacts, which lead to a design of:
- Using the external edge servers as discovery nodes and a dedicated homelab machine to route traffic:
flowchart LR
internet["Internet"]
subgraph homelab["Homelab"]
firewall["Firewall"]
vpnhost["AWS VPN host"]
firewall -- "AWS routes" --> vpnhost
end
subgraph cloud["Cloud Provider"]
edge["Edge hosts<br/>(discovery nodes)"]
end
subgraph aws["AWS"]
runners["GitLab runners"]
end
firewall --> internet
vpnhost == "VPN" ==> edge
runners == "VPN" ==> edge
This left several issues unanswered:
- Gitlab runner supports either connecting to the public or the private IP of an instance, not some artibrary IP
- How would routing work for an unknown number of machines (at any given time there might be 1, or 2.. or 0)
Whilst the former could be solved with some harsh 1:1 NAT rules, I chose to go with iBGP. The result of which meant that the AWS VPN gateway blackholes the VPC CIDR and FRR allows the AWS runners to connect over the mesh and advertise their AWS VPC IP:
flowchart LR vpn["Homelab VPN host"] runner["GitLab runner (AWS)"] vpn ---|"VPN tunnel"| runner vpn -. "BGP: homelab CIDRs x.x.x.x/x" .-> runner runner -. "BGP: AWS VPC IP 172.31.10.5/32" .-> vpn
To have the VPN CIDRs be deterministic, I up-front generated VPN key-pairs and IP addressed, indexed on a per-VPC-CIDR basis, meaning that 1.1.1.1 VPC IP would have 1.1.2.1 VPN IP. This avoids any overlap, allows the FRR config generation at boot to be deterministic and avoid some “discovery” to determine which IP it should use.
Initially, I trialled WireGuard as the VPN solution. However, its peer-based routing model introduced some limitations for our intended topology.
In particular, each peer’s configuration determines which destination CIDRs are routed through it. This creates conflicts when multiple peers advertise overlapping networks, which becomes problematic when multiple edge nodes provide access to the same client networks.
For example, in a topology where clients A and B connect through a set of highly available edge nodes, each edge node would need to advertise the same client CIDRs. This isn’t supported cleanly by WireGuard’s routing model.
Tinc offered a better fit, as it separates establishing connectivity between peers from the routing layer for each clients.. This allows multiple edge nodes to provide connectivity without requiring each to independently advertise the same client networks.
I also considered using WireGuard with UDP relays, or introducing GRE tunnels to provide an additional encapsulation layer, with BGP running on top of it. However, these approaches added considerable complexity, particularly given the dynamic nature of the AWS instances and the need to provision an entire /24 subnet. Maintaining GRE tunnels for each VM, VPN connection and edge node would have resulted in a incredibly complicated setup.
In the end, Tinc provided the required connectivity and routing flexibility without introducing too much additional complexity.
3. Public URL assumptions are the mother of all…
After deploying an initial trial of the Gitlab runner, it seemed to work.. until it didn’t. The jobs ran fine in tests, but when I performed a build against a job that produces/consumes artifacts, it broke.. trying to access gitlab over http and port 8080. After searching through my Gitlab config I didn’t find anything (though, this instance has been running for 6+ years and I’d not seen this before, I was a bit doubtful).
The obvious answer was the right answer and I need to explain how Gitlab is hosted and how the runner is hosted…
flowchart LR
subgraph homelab["Homelab"]
irp["Internal reverse proxy"]
subgraph dc1["Nomad DC 1"]
traefik["Traefik"]
subgraph gitlabgroup["GitLab service"]
envoy["Envoy sidecar"]
gitlab["GitLab"]
envoy --> gitlab
end
traefik --> envoy
end
subgraph dc2["Nomad DC 2"]
subgraph fleetgroup["gitlab-fleet-manager service"]
fleet["gitlab-fleet-manager"]
envoy2["Envoy sidecar"]
fleet --> envoy2
end
end
end
subgraph cloud["Cloud"]
erp["External reverse proxy"]
end
erp --> irp
irp --> traefik
envoy2 -- "GitLab API" --> envoy
To allow gitlab fleet manager to communicate with Gitlab, it goes through an envoy side-car. Since I’m not yet using a transparent proxy for mesh communications in nomad, all communication happens through envoy sockets bound to localhost. Since this Gitlab instance is the omnibus version, it bundles the database and other services including nginx. Because of this, it’s finnicky and actually cares about the host header, meaning I can’t just send traffic at it with random host headers and hope it works.. Whilst I did have another task to update the consul service definition set that envoy would set the host header for a traffic (and thus allow gitlab traffic to behave more similarly to other services and use a standard alternative header that’s used to define the service in Traefik routing), I had decided to simply bodge it and shove in a hosts file entry in gitlab-fleet-manager, pointing the gitlab domain to the loopback interface… the result was unfortunatly that the “gitlab URL”, according to the fleet manger, is http://gitlab.dock.studio:8080.
The “fix” for this was to run haproxy on the gitlab runner AMI, listening on:
- 8080 and 80 - listening on http and forwarding to actual upstream over https
- 443 / 22 - TCP-based listener forwarding to upstream
This actually had a benefit because I actually forwarded to the “internal reverse proxy” (in above diagram) rather than the external edge servers - over the past month or two, as everyone knows, there’s been a wave of critical vulnerabilities found, including some that affect Gitlab and some other externally accessible services that I run. Given this isn’t my day job, I’ve brought things on/offline multiple times, whilst still making them internally accessible and this has meant that the Gitlab runners have still continued to function throughout this.
4. Automation, packer and testing
As with everything, the process of setting this up had to be completely automated, the result was a 4-phase deployment of:
- Initial AWS setup - IAM deployment roles, CIDR assignment, VPC, subnets and the like, injecting necessary credentials into Vault’s AWS auth engine
- Packer built (per architecture) with a resulting AMI
- Terraform testing phase
- Gitlab fleet manager deployment, including IAM roles, launch template and ASG
The interesting part of this was the testing, from my professional experience we don’t do a great deal of AMI testing - we have ansible tests and we have dev (or pre-dev) environments, but for this situation neither really helps:
- Will ansible tests check that the VPN comes up, connects and then FRR comes up and it can access Gitlab? Probably not, it will most likely result in finnicky checks that break each time a config is changed
- Will a dev environment help? Not really, because I’d need to run actual Gitlab pipelines and target a particular machine and also means it will only function once the Gitlab fleet manager is up and running and the AMIs are a pre-requisite to this.
What I settled for is… Terraform. The way I put it together looks like this:
- Terraform defines an AMI, a snapshot, an EC2 instance and a remote provisioner, which executes a test script that tests all of the aspects of the machine (services, VPN status, FRR status, some ping/DNS resolution checks and HTTP connectivity to required homelab services).
- A wrapper script sets variables for the AMI ID, snapshot ID and the architecture
- The terraform has
importblocks, which pull the packer-built AMI and snapshot into state. - The wrapper then performs a terraform apply, which creates the test machine and executes the script
- Terraform will success/fail based on whether the test script passes or fails
- If Terraform succeeds, the script performs a
terraform state rmremoving the AMI and snapshot from state. - A
terraform destroyis then performed
So, a single build will create the AMI, test it and if it fails, delete it again. If it passes, the test infrastructure is cleaned up, leaving just the built AMI.
5. AWS free-tier
After signing up for an account, not only did a get a completely different experience to what I was used to at work (they have really revamped the onboarding and new “project” based account management), I was:
- Not asked for my credit card(!!)
- Given $200 free credit for 180 days
This “free” credit, whilst great, lead to three major issues, which bit me all in succession. Whilst performing a packer build, I ran into multiple SCP denies - as with anyone who’s managed SCPs for more than 5 minutes has learnt, SCP errors are obtuse and providing information about what SCPs are in-place and what they’re doing is abundently important - does AWS themselves do this.. no. So I faced awkward trial-and-error bouncing from error to error to error.
Instance types
Actually going into the AWS console and testing helped get through several, including showing: most instance types are unavaiable. You could obviously imagine this - a free tier account would disable really expensive instance types (perhaps?) or at least disabling popular instance types.
Except… I could chose a t3.micro, or a t3.small.. but not a t3.large (yes, it is bigger).. but wait, I can use c7i-flex.large OR c7i-flex.xlarge (and some other beefier instance types)! Who knows! I’ll take what I’m given.. if AWS refuses to let me have the crappy t3-series, I’ll happily take c7i. (Keep in mind this is the Intel varient which is somewhat more costly and with AMX features, becoming more preferred.. who knows!).
Spot!
After getting a machine actually running in AWS without error now, I went back to the packer build, which still failed. After more back and forth, I realised spot requests are completely blocked in free accounts.
From advice from my PostReviewerPT, I’ve moved a slight rant to the bottom - you can see the appendix!
Broken ARM
Finally, after getting everything working and deployed for AMD, I turned to ARM. For an architecture that uses Amazon’s own CPUs, has been in abundance for quite some time and the latest instance types appear to only be supporting it (??), this is completely cripped in the free tier.. t3a.small or medium, take it or leave it. There’s no happy ending for this one, I’ve had to move a bunch of builds from ARM to AMD (thankfully they were only Golang builds ;)) and will look at moving back once the free Tier ends.
Conclusion and cost
I never thought I’d say that I’ve move part of my homelab to AWS… I definitely didn’t think I’d ever say I’d move parts of my homelab to AWS to save money. Realistically to run a machine 24x7, AWS is never going to be the cheapest option, period. However, for machines that run a subset of my CI builds, which equates to probably 10-15 hours per month, AWS base costs come to about $0.50, when combined with spot instance pricing, this drops to closer to $0.10-$0.20 per month. On top of this for 200 days, I have $200 credit, ultimately likely to save me $1.30 (though perhaps a little more during this setup period). When compared to running an AMD runner and an ARM runner in Hetzner, coming in at about $15/month, this is a great saving. It also means, if I really want it, I could spin up 5 machines to crunch through some builds and still come in much much cheaper. Honestly, cost per hour of effort put in, particularly with little free time, this probably wasn’t financially worth it, but in retrospect, I probably still would have done it.
Appendix Rant
If you phrase this as “spot instances are an advanced feature for price saving and isn’t available for free tier” you might not ask too many questions. But what if I phrase it as the reality..
- When you create a standard on-demand machine in AWS it uses capacity.
- People care for and fight for capacity and AWS would like to provide a frictionless service.
- Once an on-demand machine is created, AWS can’t do anything about it until you stop it.
- To combat this, AWS provides a service, which allows people to create machines that they don’t mind being ripped away from the drop of a hat, this allows:
- AWS to fulfill requests for people that request the power
- AWS to (presumably) fill more capacity as they can dynamically allocate it more freely by dropping some capacity at the drop of a hat
- To make this option attractive AWS provides a cost saving for those instances
So, imagine you have a bunch of users on a free tier, that have not given a credit card and they’re creating instances on your compute.. surely you’d LOVE them to be on this tier.. heck, you could even cancel the spot instance and claim there’s no capacity when you want to prioritise Netflix and just don’t want to give away free compute for a while. Let’s say it doesn’t matter, there’s a clause deep in the ToS that allows them to more freely turn off on-demand machines for free-tier users - they absolutely shouldn’t - that’s the worst user experience and presumably completely against the purpose of the free-tier, which is “Here AWS is great, here’s some free credit, please spent more money with us” - if they show you that by randomly turning off machines, it would be disasterous. So why oh why they don’t allow spot instances on free tier is BEYOND me.