Skip to content

Instantly share code, notes, and snippets.

@rossedman
Last active September 8, 2024 22:40
Show Gist options
  • Save rossedman/87abb57aafb53030e881b410ea66ba4b to your computer and use it in GitHub Desktop.
Save rossedman/87abb57aafb53030e881b410ea66ba4b to your computer and use it in GitHub Desktop.
Scale homelab into cloud with Tailscale, Terraform and cloud-init
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
}
}
}
provider "digitalocean" {
}
variable "tailscale_key" {
type = "string"
}
resource "digitalocean_vpc" "homelab" {
name = "homelab"
region = "nyc3"
ip_range = "10.10.10.0/24"
}
resource "digitalocean_ssh_key" "default" {
name = "Labscale - Terraform"
public_key = file("~/.ssh/id_rsa.pub")
}
resource "digitalocean_droplet" "server" {
count = 3
name = "server-${count.index}"
size = "s-1vcpu-1gb"
image = "ubuntu-20-04-x64"
region = digitalocean_vpc.homelab.region
vpc_uuid = digitalocean_vpc.homelab.id
ssh_keys = [digitalocean_ssh_key.default.fingerprint]
user_data = templatefile("${path.module}/userdata.tpl", {
tailscale_key = var.tailscale_key
})
}
resource "digitalocean_project" "labscale" {
name = "labscale"
description = "A project for scaling homelab with tailscale"
resources = digitalocean_droplet.server.*.urn
}
output "public_ip" {
value = digitalocean_droplet.server.*.ipv4_address
}
output "private_ip" {
value = digitalocean_droplet.server.*.ipv4_address_private
}
#cloud-config
# this variant installs docker as well as tailscale
# using these together you could create a really simple
# container homelab for quick learning and low cost
---
ssh_pwauth: false
apt:
sources:
tailscale.list:
source: deb https://pkgs.tailscale.com/stable/ubuntu focal main
keyid: 2596A99EAAB33821893C0A79458CA832957F5868
docker.list:
source: deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
keyid: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88
packages:
- docker-ce
- docker-ce-cli
- tailscale
runcmd:
- tailscale up -authkey ${tailscale_key}
#cloud-config
---
apt:
sources:
tailscale.list:
source: deb https://pkgs.tailscale.com/stable/ubuntu focal main
keyid: 2596A99EAAB33821893C0A79458CA832957F5868
packages:
- tailscale
runcmd:
- [tailscale, up, -authkey, ${tailscale_key}]
@rossedman
Copy link
Author

To run this:

export DIGITALOCEAN_TOKEN=<token>
export TF_VAR_tailscale_key=<token>
terraform plan -out tailscale.out
terraform apply tailscale.out

@okmechak
Copy link

okmechak commented Mar 7, 2024

@rossedman Thank you for sharing these configs especially with TailScale setup, but this part:

runcmd:
  - [tailscale, up, -authkey, ${tailscale_key}]

unfortunately does not work in my case.

I have posted on Ask Ubuntu two questions(one, two) about this. Maybe you know the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment