Skip to content

Commit

Permalink
Add Load Balancer example.
Browse files Browse the repository at this point in the history
  • Loading branch information
pysysops committed Jan 16, 2019
1 parent b4f9843 commit 4ba0aed
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
35 changes: 35 additions & 0 deletions examples/loadbalancer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Loadbalancer example

An example of provisioning Droplets attached to a Load Balancer using tags.

## Usage

To run this example you need to execute:

```bash
$ terraform init
$ terraform plan
$ terraform apply
```

Now visit your Load Balancer IP in a browser and refresh. You should see the
requests are sent to each Droplet.

Note that this example may create resources which can cost money.
Run `terraform destroy` when you don't need these resources.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| do\_token | - | string | - | yes |

## Outputs

| Name | Description |
|------|-------------|
| loadbalancer\_ip | IP address of the Load Balancer |
| web\_ipv4\_address | List of IPv4 addresses of web Droplets |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
44 changes: 44 additions & 0 deletions examples/loadbalancer/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
variable "do_token" {}

provider "digitalocean" {
token = "${var.do_token}"
}

resource "digitalocean_tag" "ENV_example" {
name = "ENV:example"
}

resource "digitalocean_tag" "ROLE_web" {
name = "ROLE:web"
}

module "web" {
source = "../../"

droplet_count = 2

droplet_name = "example-web"
droplet_size = "nano"
tags = ["${digitalocean_tag.ENV_example.id}", "${digitalocean_tag.ROLE_web.id}"]
user_data = "${file("user-data.web")}"
}

resource "digitalocean_loadbalancer" "public" {
name = "web-balancer"
region = "ams3"

forwarding_rule {
entry_port = 80
entry_protocol = "http"

target_port = 80
target_protocol = "http"
}

healthcheck {
port = 22
protocol = "tcp"
}

droplet_tag = "${digitalocean_tag.ROLE_web.name}"
}
9 changes: 9 additions & 0 deletions examples/loadbalancer/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "loadbalancer_ip" {
description = "IP address of the Load Balancer"
value = "${digitalocean_loadbalancer.public.ip}"
}

output "web_ipv4_address" {
description = "List of IPv4 addresses of web Droplets"
value = "${module.web.ipv4_address}"
}
5 changes: 5 additions & 0 deletions examples/loadbalancer/user-data.web
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#cloud-config

runcmd:
- apt-get install -y nginx
- 'echo "<html><body><h2>$(hostname)</h2></body></html>" > /var/www/html/index.html'

0 comments on commit 4ba0aed

Please sign in to comment.