Skip to content

Commit 67f95d0

Browse files
author
kamil cybulski
committed
Initial commit
0 parents  commit 67f95d0

File tree

16 files changed

+1145
-0
lines changed

16 files changed

+1145
-0
lines changed

.github/workflows/test.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
11+
test:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v2
18+
with:
19+
go-version: 1.18
20+
21+
- name: Run tests
22+
working-directory: ./test
23+
run: go test -v
24+
env:
25+
UPCLOUD_USERNAME: ${{ secrets.UPCLOUD_USERNAME }}
26+
UPCLOUD_PASSWORD: ${{ secrets.UPCLOUD_PASSWORD }}
27+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
28+
TF_VAR_cloudflare_zone_id: ${{ secrets. CLOUDFLARE_ZONE_ID }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.terraform/
2+
.terraform.lock.hcl
3+
terraform.tfstate
4+
terraform.tfstate.backup

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 UpCloud Ltd
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# UpCloud Basic Loadbalancer
2+
3+
Terraform module which creates a basic load balancer setup with with automatic SSL certificate.
4+
5+
## Usage
6+
```hcl
7+
module "load_balancer" {
8+
source = "UpCloudLtd/basic-loadbalancer/upcloud"
9+
10+
name = "my_loadbalancer"
11+
zone = "pl-waw1"
12+
network = upcloud_network.my_network.id
13+
backend_servers = ["10.0.1.3", "10.0.1.4", "10.0.1.5"]
14+
backend_server_port = 3000
15+
domains = ["my.domain.net"]
16+
}
17+
```

examples/basic/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Basic example
2+
3+
A simple example with 3 servers and a load balancer that distributes the traffic between them. The example assumes that each of the servers has some application listening on port 3000.
4+
5+
Once you deploy it, go to your domain settings and add a CNAME record that points to the `lb_url` output variable. After the change gets propagated, all the traffic to your domain (`my.domain.com` in this example) will be distributed among the 3 servers.

examples/basic/main.tf

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
terraform {
2+
required_providers {
3+
upcloud = {
4+
source = "UpCloudLtd/upcloud"
5+
version = "~> 2.4"
6+
}
7+
}
8+
}
9+
10+
provider "upcloud" {}
11+
12+
resource "upcloud_network" "network" {
13+
name = "my_network"
14+
zone = "pl-waw1"
15+
16+
ip_network {
17+
family = "IPv4"
18+
address = "10.0.1.0/24"
19+
dhcp = true
20+
}
21+
}
22+
23+
resource "upcloud_server" "webservers" {
24+
count = 3
25+
hostname = "webserver${count.index}"
26+
title = "webserver_${count.index}"
27+
zone = "pl-waw1"
28+
plan = "1xCPU-2GB"
29+
30+
template {
31+
storage = "Ubuntu Server 20.04 LTS (Focal Fossa)"
32+
size = 25
33+
}
34+
35+
network_interface {
36+
type = "utility"
37+
}
38+
39+
network_interface {
40+
type = "private"
41+
network = upcloud_network.network.id
42+
}
43+
}
44+
45+
module "load_balancer" {
46+
source = "UpCloudLtd/basic-loadbalancer/upcloud"
47+
48+
name = "my_loadbalancer"
49+
zone = "pl-waw1"
50+
network = upcloud_network.network.id
51+
backend_servers = [for v in upcloud_server.webservers : v.network_interface.1.ip_address]
52+
backend_server_port = 3000
53+
domains = ["my.domain.net"]
54+
}
55+
56+
output "lb_url" {
57+
value = module.load_balancer.dns_name
58+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Basic example with additional rules
2+
3+
This example shows how to attach additional frontend rules to load balancer. The module has a `frontend_id` output variable, which allows you to easily add rules to it.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Rest of the configuration omitted for brevity
2+
3+
module "load_balancer" {
4+
source = "UpCloudLtd/basic-loadbalancer/upcloud"
5+
6+
name = "my_loadbalancer"
7+
zone = "pl-waw1"
8+
network = upcloud_network.my_network.id
9+
backend_servers = ["10.0.1.3", "10.0.1.4", "10.0.1.5"]
10+
domains = ["my.domain.net"]
11+
}
12+
13+
resource "upcloud_loadbalancer_frontend_rule" "some_rule" {
14+
frontend = module.load_balancer.frontend_id
15+
name = "some_rule"
16+
priority = 10
17+
18+
matchers {
19+
url_param {
20+
method = "exact"
21+
name = "secret"
22+
value = "1"
23+
}
24+
}
25+
26+
actions {
27+
http_redirect {
28+
location = "/super-secret-place"
29+
}
30+
}
31+
}

main.tf

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
terraform {
2+
required_providers {
3+
upcloud = {
4+
source = "UpCloudLtd/upcloud"
5+
version = "~> 2.4"
6+
}
7+
}
8+
}
9+
10+
resource "upcloud_loadbalancer" "main" {
11+
configured_status = "started"
12+
name = var.name
13+
plan = var.plan
14+
zone = var.zone
15+
network = var.network
16+
}
17+
18+
resource "upcloud_loadbalancer_backend" "main" {
19+
loadbalancer = upcloud_loadbalancer.main.id
20+
name = "main"
21+
}
22+
23+
resource "upcloud_loadbalancer_static_backend_member" "main" {
24+
count = length(var.backend_servers)
25+
backend = upcloud_loadbalancer_backend.main.id
26+
name = "member_${count.index}"
27+
ip = var.backend_servers[count.index]
28+
port = var.backend_server_port
29+
max_sessions = var.max_server_sessions
30+
enabled = true
31+
weight = 100
32+
}
33+
34+
resource "upcloud_loadbalancer_frontend" "main" {
35+
loadbalancer = upcloud_loadbalancer.main.id
36+
name = "main"
37+
mode = "http"
38+
port = var.frontend_port
39+
default_backend_name = upcloud_loadbalancer_backend.main.name
40+
}
41+
42+
resource "upcloud_loadbalancer_dynamic_certificate_bundle" "main" {
43+
name = "main"
44+
hostnames = var.domains
45+
key_type = "rsa"
46+
}
47+
48+
resource "upcloud_loadbalancer_frontend_tls_config" "main" {
49+
frontend = upcloud_loadbalancer_frontend.main.id
50+
name = "main"
51+
certificate_bundle = upcloud_loadbalancer_dynamic_certificate_bundle.main.id
52+
}

outputs.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
output "id" {
2+
value = upcloud_loadbalancer.main.id
3+
}
4+
5+
output "dns_name" {
6+
value = upcloud_loadbalancer.main.dns_name
7+
}
8+
9+
output "backend_name" {
10+
value = upcloud_loadbalancer_backend.main.name
11+
}
12+
13+
output "backend_id" {
14+
value = upcloud_loadbalancer_backend.main.id
15+
}
16+
17+
output "frontend_name" {
18+
value = upcloud_loadbalancer_frontend.main.name
19+
}
20+
21+
output "frontend_id" {
22+
value = upcloud_loadbalancer_frontend.main.id
23+
}

0 commit comments

Comments
 (0)