Skip to content

Commit 1ca98df

Browse files
author
Ben Vilnis
committed
refactor digitalocean to use nginx reverse proxy
1 parent fc364e1 commit 1ca98df

File tree

7 files changed

+41
-78
lines changed

7 files changed

+41
-78
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ Each cloud platform will follow the same design pattern, that being:
3232

3333
**3.** Download and install the latest Code Server release via native package manager.
3434

35+
**4.** Import user's SSH keys from their GitHub account for easy SSH access.
36+
3537
## Future Features:
3638
The current ideas for future iterations are:
3739

38-
**1.** Implement more platforms. Possibly AWS next.
40+
**1.** Implement more platforms. GCP and Azure.
3941

4042
**2.** Add automation for installing dev tools (possibly Ansible).
4143

digitalocean/README.md

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Digital Ocean
22
You will need to export your [Digital Ocean API token](https://www.digitalocean.com/docs/api/create-personal-access-token/) as `DIGITALOCEAN_TOKEN` to authenticate Terraform.
33

4-
By default, this stack builds a [load balancer](https://www.digitalocean.com/docs/networking/load-balancers/) which accepts and passes HTTP traffic through to the Code Server port `:8080` on the droplet. For optimal security, I recommend using a TLS-certified domain and forcing HTTPS on the load balancer. An easy managed way to achieve this on Digital Ocean can be found [here](https://www.digitalocean.com/docs/networking/load-balancers/how-to/ssl-termination/).
4+
By default, this stack builds a Droplet with an NGINX reverse proxy which accepts and passes HTTP traffic through to the Code Server port `:8080` on the instance. For optimal security, I recommend using a TLS-certified domain against the instance public IP. This can be easily added into the existing NGINX webserver with the [following guide](https://www.scaleway.com/en/docs/how-to-configure-nginx-reverse-proxy/#-Adding-TLS-to-your-Nginx-Reverse-Proxy-using-Lets-Encrypt).
55

6-
_**FYI:** Digital Ocean user data can take a few minutes to execute sometimes. If the Code Server endpoint initially returns a 503, this just means the user data hasn't finished executing yet._
6+
_**FYI:** Digital Ocean user data can take a few minutes to execute sometimes. If the Code Server endpoint initially returns a 503 or you are unable to SSH in, this just means the user data hasn't finished executing yet._
77

88
## Digital Ocean parameters in [terraform.tvfars](terraform.tfvars):
99

@@ -43,32 +43,4 @@ _**FYI:** Digital Ocean user data can take a few minutes to execute sometimes. I
4343

4444
**storage_size:** The size *(in GB)* of the persistent disk that will be mounted to `/home`.
4545

46-
**ssh_key_id:** Your [Digital Ocean SSH key ID](https://developers.digitalocean.com/documentation/v2/#list-all-keys). These are 8-digit numbers that map to SSH keys linked on your Digital Ocean account and are required to authenticate connections to the droplet.
47-
48-
To find your SSH Key ID, run the following command, replacing `DO_API_TOKEN` with your Digital Ocean API token.
49-
50-
```
51-
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer DO_API_TOKEN" "https://api.digitalocean.com/v2/account/keys"
52-
````
53-
54-
The response body will look like this. Grab the 8-digit ID number.
55-
56-
```
57-
{
58-
"ssh_keys": [
59-
{
60-
"id": 512189,
61-
"fingerprint": "3b:16:bf:e4:8b:00:8b:b8:59:8c:a9:d3:f0:19:45:fa",
62-
"public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDDHr/jh2Jy4yALcK4JyWbVkPRaWmhck3IgCoeOO3z1e2dBowLh64QAM+Qb72pxekALga2oi4GvT+TlWNhzPH4V example",
63-
"name": "My SSH Public Key"
64-
}
65-
],
66-
"links": {
67-
},
68-
"meta": {
69-
"total": 1
70-
}
71-
}
72-
```
73-
74-
If you have not added your SSH key to your Digital Ocean account, instructions to do so can be found [here](https://www.digitalocean.com/docs/droplets/how-to/add-ssh-keys/to-account/).
46+
**github_username:** Your GitHub username. This will import the SSH keys associated with your GitHub account to the created user so you can SSH into the EC2 instance if needed.

digitalocean/main.tf

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ data "template_file" "init" {
2222
HOSTNAME = "${var.hostname}",
2323
USERNAME = "${var.username}",
2424
USERPASS = "${random_password.user.result}",
25-
CODERPASS = "${random_password.code-server.result}"
25+
CODERPASS = "${random_password.code-server.result}",
26+
GITHUB_USER = "${var.github_username}"
2627
}
2728
}
2829

@@ -35,7 +36,6 @@ resource "digitalocean_droplet" "droplet" {
3536
backups = true
3637
monitoring = true
3738
private_networking = "true"
38-
ssh_keys = [var.ssh_key_id]
3939
vpc_uuid = digitalocean_vpc.vpc.id
4040
user_data = data.template_file.init.rendered
4141
}
@@ -54,36 +54,6 @@ resource "digitalocean_volume_attachment" "disk-attachment" {
5454
volume_id = digitalocean_volume.disk.id
5555
}
5656

57-
# Loadbalancer
58-
resource "digitalocean_loadbalancer" "lb" {
59-
name = "${var.hostname}-loadbalancer"
60-
region = var.region
61-
vpc_uuid = digitalocean_vpc.vpc.id
62-
63-
forwarding_rule {
64-
entry_port = 22
65-
entry_protocol = "tcp"
66-
67-
target_port = 22
68-
target_protocol = "tcp"
69-
}
70-
71-
forwarding_rule {
72-
entry_port = 80
73-
entry_protocol = "http"
74-
75-
target_port = 8080
76-
target_protocol = "http"
77-
}
78-
79-
healthcheck {
80-
port = 22
81-
protocol = "tcp"
82-
}
83-
84-
droplet_ids = ["${digitalocean_droplet.droplet.id}"]
85-
}
86-
8757
# Firewall
8858
resource "digitalocean_firewall" "firewall" {
8959
name = "${var.hostname}-firewall"
@@ -93,18 +63,18 @@ resource "digitalocean_firewall" "firewall" {
9363
inbound_rule {
9464
protocol = "tcp"
9565
port_range = "22"
96-
source_load_balancer_uids = [digitalocean_loadbalancer.lb.id]
66+
source_addresses = ["0.0.0.0/0", "::/0"]
9767
}
9868

9969
inbound_rule {
10070
protocol = "tcp"
101-
port_range = "8080"
102-
source_load_balancer_uids = [digitalocean_loadbalancer.lb.id]
71+
port_range = "80"
72+
source_addresses = ["0.0.0.0/0", "::/0"]
10373
}
10474

10575
inbound_rule {
10676
protocol = "icmp"
107-
source_load_balancer_uids = [digitalocean_loadbalancer.lb.id]
77+
source_addresses = ["0.0.0.0/0", "::/0"]
10878
}
10979

11080
outbound_rule {
@@ -125,7 +95,6 @@ resource "digitalocean_project" "project" {
12595
name = var.hostname
12696
resources = [
12797
"do:droplet:${digitalocean_droplet.droplet.id}",
128-
"do:volume:${digitalocean_volume.disk.id}",
129-
"do:loadbalancer:${digitalocean_loadbalancer.lb.id}"
98+
"do:volume:${digitalocean_volume.disk.id}"
13099
]
131100
}

digitalocean/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
output "ip_address" {
2-
value = digitalocean_loadbalancer.lb.ip
2+
value = digitalocean_droplet.droplet.ipv4_address
33
}
44

55
output "sudo_password" {

digitalocean/terraform.tfvars

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ droplet_size = "s-2vcpu-2gb"
1313
# /home drive size
1414
storage_size = 20
1515

16-
# SSH key ID
17-
ssh_key_id = 01234567
16+
# GitHub username
17+
github_username = "github_username"

digitalocean/user_data.tpl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ mount_home_drive() {
1212
update_system() {
1313
apt update
1414
apt -y dist-upgrade
15-
apt install -y jq systemd-container
15+
apt install -y jq systemd-container nginx
1616
apt -y autoclean
1717
apt -y autoremove
1818
}
1919

2020
add_user() {
2121
useradd -m -G sudo -s /bin/bash ${USERNAME}
2222
echo -e "${USERPASS}\n${USERPASS}" | passwd ${USERNAME}
23-
cp -r /root/.ssh /home/${USERNAME} && \
23+
mkdir /home/${USERNAME}/.ssh && \
24+
curl https://github.com/${GITHUB_USER}.keys >> /home/${USERNAME}/.ssh/authorized_keys && \
2425
chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}/.ssh
2526
}
2627

@@ -50,6 +51,23 @@ enable_code_server() {
5051
machinectl shell --uid=${USERNAME} .host /usr/bin/systemctl --user enable --now code-server.service
5152
}
5253

54+
nginx_config() {
55+
unlink /etc/nginx/sites-enabled/default
56+
cat <<EOF > "/etc/nginx/sites-available/reverse-proxy.conf"
57+
server {
58+
listen 80;
59+
listen [::]:80;
60+
location / {
61+
proxy_pass http://localhost:8080/;
62+
proxy_set_header Upgrade \$http_upgrade;
63+
proxy_set_header Connection upgrade;
64+
}
65+
}
66+
EOF
67+
ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf
68+
systemctl restart nginx.service
69+
}
70+
5371
main () {
5472
mount_home_drive
5573

@@ -62,6 +80,8 @@ main () {
6280
code_server_config
6381

6482
enable_code_server
83+
84+
nginx_config
6585
}
6686

6787
# Exectution

digitalocean/variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ variable "storage_size" {
1818
type = number
1919
}
2020

21-
variable "ssh_key_id" {
22-
type = number
23-
}
21+
variable "github_username" {
22+
type = string
23+
}

0 commit comments

Comments
 (0)