From 4ba0aed89b1eb8bf89debb81364adebe0585fca9 Mon Sep 17 00:00:00 2001 From: Tim Birkett Date: Wed, 16 Jan 2019 01:36:40 +0000 Subject: [PATCH] Add Load Balancer example. --- examples/loadbalancer/README.md | 35 +++++++++++++++++++++++ examples/loadbalancer/main.tf | 44 +++++++++++++++++++++++++++++ examples/loadbalancer/output.tf | 9 ++++++ examples/loadbalancer/user-data.web | 5 ++++ 4 files changed, 93 insertions(+) create mode 100644 examples/loadbalancer/README.md create mode 100644 examples/loadbalancer/main.tf create mode 100644 examples/loadbalancer/output.tf create mode 100644 examples/loadbalancer/user-data.web diff --git a/examples/loadbalancer/README.md b/examples/loadbalancer/README.md new file mode 100644 index 0000000..22e0fc8 --- /dev/null +++ b/examples/loadbalancer/README.md @@ -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. + + +## 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 | + + diff --git a/examples/loadbalancer/main.tf b/examples/loadbalancer/main.tf new file mode 100644 index 0000000..87bec2a --- /dev/null +++ b/examples/loadbalancer/main.tf @@ -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}" +} diff --git a/examples/loadbalancer/output.tf b/examples/loadbalancer/output.tf new file mode 100644 index 0000000..3e27e6b --- /dev/null +++ b/examples/loadbalancer/output.tf @@ -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}" +} diff --git a/examples/loadbalancer/user-data.web b/examples/loadbalancer/user-data.web new file mode 100644 index 0000000..12972f7 --- /dev/null +++ b/examples/loadbalancer/user-data.web @@ -0,0 +1,5 @@ +#cloud-config + +runcmd: + - apt-get install -y nginx + - 'echo "

$(hostname)

" > /var/www/html/index.html'