Skip to content

Commit

Permalink
Remove private IPv6 related code, update DNS example.
Browse files Browse the repository at this point in the history
Fixes issue #1 - removes private IPv6 related code.

Fixes issue #2 - computed fields and counts in the DNS example.
  • Loading branch information
pysysops committed Jan 18, 2019
1 parent a534f12 commit 6539341
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 73 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ Some examples can be found in this repository:
**Note** that examples may create resources which can cost money.
Run `terraform destroy` when you don't need these resources.

## Droplet Sizes
A map of name to Droplet sizes exists to make specifying Droplet sizes simpler:

| Name | Droplet Size |
| --------- | -------------- |
| nano | s-1vcpu-1gb |
| micro | s-2vcpu-2gb |
| small | s-2vcpu-4gb |
| medium | s-4vcpu-8gb |
| large | s-6vcpu-16gb |
| x-large | s-8vcpu-32gb |
| xx-large | s-16vcpu-64gb |
| xxx-large | s-24vcpu-128gb |
| maximum | s-32vcpu-192gb |

See [DigitalOcean Pricing](https://www.digitalocean.com/pricing/) for costs.

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

Expand Down Expand Up @@ -59,7 +76,6 @@ Run `terraform destroy` when you don't need these resources.
| public\_domain | (Optional) String containing the public DNS domain to create a record for the Droplets in. | string | `` | no |
| region | The Digitalocean datacenter to create resources in. | string | `ams3` | no |
| resize\_disk | (Optional) Boolean controlling whether to increase the disk size when resizing a Droplet. It defaults to true. When set to false, only the Droplet's RAM and CPU will be resized. Increasing a Droplet's disk size is a permanent change. Increasing only RAM and CPU is reversible. | string | `true` | no |
| sizes | A map of pre-canned instance sizes. | map | `{ "large": "s-6vcpu-16gb", "maximum": "s-32vcpu-192gb", "medium": "s-4vcpu-8gb", "micro": "s-2vcpu-2gb", "nano": "s-1vcpu-1gb", "small": "s-2vcpu-4gb", "x-large": "s-8vcpu-32gb", "xx-large": "s-16vcpu-64gb", "xxx-large": "s-24vcpu-128gb" }` | no |
| ssh\_keys | (Optional) A list of SSH IDs or fingerprints to enable in the format [12345, 123456]. To retrieve this info, use a tool such as curl with the DigitalOcean API, to retrieve them. | list | `[]` | no |
| tags | (Optional) A list of the tags to label this Droplet. A tag resource must exist before it can be associated with a Droplet. | list | `[]` | no |
| user\_data | (Optional) A string of the desired User Data for the Droplet. | string | `exit 0` | no |
Expand All @@ -76,7 +92,6 @@ Run `terraform destroy` when you don't need these resources.
| ipv4\_address | List of public IPv4 addresses assigned to the Droplets |
| ipv4\_address\_private | List of private IPv4 addresses assigned to the Droplets, if applicable |
| ipv6\_address | List of public IPv6 addresses assigned to the Droplets, if applicable |
| ipv6\_address\_private | List of private IPv6 addresses assigned to the Droplets, if applicable |
| loadbalancer\_id | ID of the loadbalancer |
| loadbalancer\_ip | IP address of the loadbalancer |
| name | List of names of Droplets |
Expand Down
13 changes: 11 additions & 2 deletions examples/dns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ configuring DNS zones and records for droplets and the loadbalancer.

## Usage

>**Caveat** due to a long standing limitation in using computed values within
count variable assignment you need to target the public domain creation before
creating the other resources. This shouldn't be a problem in normal usage and is
because this example uses the random provider to generate a random domain name.

To run this example you need to execute:

```bash
$ terraform init
$ terraform plan -target=digitalocean_domain.public
$ terraform apply -target=digitalocean_domain.public
$ terraform plan
$ terraform apply
```

This is due to the domain already existing on DigitalOcean. You can swap these
lines to generate a random domain name:

Now visit your Load Balancer IP in a browser and refresh. After a few minutes
you should see the requests are sent to each Droplet in a round-robin fashion.

Expand Down Expand Up @@ -41,10 +51,9 @@ Run `terraform destroy` when you don't need these resources.
| Name | Description |
|------|-------------|
| loadbalancer\_ip | IP address of the Load Balancer. |
| private\_domain\_zone\_file | The private DNS domain zone file contents. |
| public\_domain\_ip\_address | The public DNS domain apex record IP address. |
| public\_domain\_name | The public DNS domain name. |
| public\_hostnames | The public domain name of the first Droplet. |
| web\_ipv4\_address | List of IPv4 addresses of web Droplets. |
| web\_ipv6\_address | List of IPv6 addresses of web Droplets. |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
15 changes: 6 additions & 9 deletions examples/dns/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ resource "digitalocean_tag" "ROLE_web" {

// DNS Zones
resource "digitalocean_domain" "public" {
name = "${format("public.%s.com", random_string.domain.result)}"
ip_address = "${module.web.loadbalancer_ip}"
name = "${format("public.%s.com", random_string.domain.result)}"
}

resource "digitalocean_domain" "private" {
name = "${format("private.%s.com", random_string.domain.result)}"
}

data "digitalocean_domain" "private" {
name = "${digitalocean_domain.private.name}"
resource "digitalocean_record" "public-apex" {
domain = "${digitalocean_domain.public.name}"
type = "A"
name = "@"
value = "${module.web.loadbalancer_ip}"
}

module "web" {
Expand All @@ -49,7 +47,6 @@ module "web" {

ipv6 = true
public_domain = "${digitalocean_domain.public.name}"
public_domain = "${digitalocean_domain.private.name}"

loadbalancer = true
}
15 changes: 5 additions & 10 deletions examples/dns/output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ output "web_ipv4_address" {
value = "${module.web.ipv4_address}"
}

output "web_ipv6_address" {
description = "List of IPv6 addresses of web Droplets."
value = "${module.web.ipv6_address}"
}

output "public_domain_name" {
description = "The public DNS domain name."
value = "${digitalocean_domain.public.name}"
}

output "public_domain_ip_address" {
description = "The public DNS domain apex record IP address."
value = "${digitalocean_domain.public.ip_address}"
}

output "private_domain_zone_file" {
description = "The private DNS domain zone file contents."
value = "${data.digitalocean_domain.private.zone_file}"
}

output "public_hostnames" {
description = "The public domain name of the first Droplet."
value = "${module.web.public_a}"
Expand Down
35 changes: 19 additions & 16 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
locals {
// Map of pre-named sizes to look up from
sizes = {
nano = "s-1vcpu-1gb"
micro = "s-2vcpu-2gb"
small = "s-2vcpu-4gb"
medium = "s-4vcpu-8gb"
large = "s-6vcpu-16gb"
x-large = "s-8vcpu-32gb"
xx-large = "s-16vcpu-64gb"
xxx-large = "s-24vcpu-128gb"
maximum = "s-32vcpu-192gb"
}
}

// Lookup image to get id
data "digitalocean_image" "official" {
count = "${var.custom_image > 0 ? 0 : 1}"
Expand All @@ -21,7 +36,7 @@ resource "digitalocean_droplet" "droplet" {
name = "${format("%s-%s", var.droplet_name, format(var.number_format, count.index+1))}"

region = "${var.region}"
size = "${coalesce(var.sizes[var.droplet_size], var.droplet_size)}"
size = "${coalesce(local.sizes[var.droplet_size], var.droplet_size)}"

// Optional
backups = "${var.backups}"
Expand Down Expand Up @@ -89,7 +104,7 @@ resource "digitalocean_loadbalancer" "loadbalancer" {

// Public DNS A Record
resource "digitalocean_record" "public_a" {
count = "${var.public_domain != "" ? var.droplet_count : 0}"
count = "${length(var.public_domain) > 0 ? var.droplet_count : 0}"

domain = "${var.public_domain}"
type = "A"
Expand All @@ -99,7 +114,7 @@ resource "digitalocean_record" "public_a" {

// Public DNS AAAA Record
resource "digitalocean_record" "public_aaaa" {
count = "${var.ipv6 > 0 && var.public_domain != "" ? var.droplet_count : 0}"
count = "${length(var.public_domain) > 0 ? var.droplet_count : 0}"

domain = "${var.public_domain}"
type = "AAAA"
Expand All @@ -109,22 +124,10 @@ resource "digitalocean_record" "public_aaaa" {

// Private DNS A Record
resource "digitalocean_record" "private_a" {
count = "${var.private_networking > 0 && var.private_domain != "" ? var.droplet_count : 0}"
count = "${var.private_networking > 0 && length(var.private_domain) > 0 ? var.droplet_count : 0}"

domain = "${var.private_domain}"
type = "A"
name = "${element(digitalocean_droplet.droplet.*.name, count.index)}"
value = "${element(digitalocean_droplet.droplet.*.ipv4_address_private, count.index)}"
}

// Private DNS AAAA Record - Not supported somewhere:
// https://github.com/terraform-providers/terraform-provider-digitalocean/issues/181
// resource "digitalocean_record" "private_aaaa" {
// count = "${var.private_networking > 0 && var.ipv6 > 0 && var.private_domain != "" ? var.droplet_count : 0}"
//
// domain = "${var.private_domain}"
// type = "AAAA"
// name = "${element(digitalocean_droplet.droplet.*.name, count.index)}"
// value = "${element(digitalocean_droplet.droplet.*.ipv6_address_private, count.index)}"
// }

26 changes: 9 additions & 17 deletions output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ locals {
droplet_ipv4_address = "${compact(concat(digitalocean_droplet.droplet.*.ipv4_address, list("")))}"
droplet_ipv4_address_private = "${compact(concat(digitalocean_droplet.droplet.*.ipv4_address_private, list("")))}"
droplet_ipv6_address = "${compact(concat(digitalocean_droplet.droplet.*.ipv6_address, list("")))}"
droplet_ipv6_address_private = "${compact(concat(digitalocean_droplet.droplet.*.ipv6_address_private, list("")))}"
droplet_region = "${compact(concat(digitalocean_droplet.droplet.*.region, list("")))}"
droplet_name = "${compact(concat(digitalocean_droplet.droplet.*.name, list("")))}"
droplet_size = "${compact(concat(digitalocean_droplet.droplet.*.size, list("")))}"
droplet_image = "${compact(concat(digitalocean_droplet.droplet.*.image, list("")))}"
droplet_tags = "${compact(concat(flatten(digitalocean_droplet.droplet.*.tags), list("")))}"
floating_ip_address = "${compact(concat(digitalocean_floating_ip.floating_ip.*.ip_address, list("")))}"
loadbalancer_id = "${compact(concat(digitalocean_loadbalancer.loadbalancer.*.id, list("")))}"
loadbalancer_ip = "${compact(concat(digitalocean_loadbalancer.loadbalancer.*.ip, list("")))}"
private_a = "${compact(concat(digitalocean_record.private_a.*.fqdn, list("")))}"

// private_aaaa = "${compact(concat(digitalocean_record.private_aaaa.*.fqdn, list("")))}"
// join is used to return a string rather than a list as only a 1 or 0 loadbalancers will ever exist.
// Using the * value is needed as there may or moy not be a loadbalancer resource to get the ip from which causes errors.
loadbalancer_id = "${join("", compact(concat(digitalocean_loadbalancer.loadbalancer.*.id, list(""))))}"

loadbalancer_ip = "${join("", compact(concat(digitalocean_loadbalancer.loadbalancer.*.ip, list(""))))}"

private_a = "${compact(concat(digitalocean_record.private_a.*.fqdn, list("")))}"
public_a = "${compact(concat(digitalocean_record.public_a.*.fqdn, list("")))}"
public_aaaa = "${compact(concat(digitalocean_record.public_aaaa.*.fqdn, list("")))}"
volume_id = "${compact(concat(digitalocean_volume.volume.*.id, list("")))}"
Expand Down Expand Up @@ -63,19 +65,14 @@ output "ipv6_address" {
value = ["${local.droplet_ipv6_address}"]
}

output "ipv6_address_private" {
description = "List of private IPv6 addresses assigned to the Droplets, if applicable"
value = ["${local.droplet_ipv6_address_private}"]
}

output "loadbalancer_id" {
description = "ID of the loadbalancer"
value = ["${local.loadbalancer_ip}"]
value = "${local.loadbalancer_ip}"
}

output "loadbalancer_ip" {
description = "IP address of the loadbalancer"
value = ["${local.loadbalancer_ip}"]
value = "${local.loadbalancer_ip}"
}

output "name" {
Expand All @@ -88,11 +85,6 @@ output "private_a" {
value = ["${local.private_a}"]
}

// output "private_aaaa" {
// description = "List of Droplet private DNS AAAA record FQDNs."
// value = ["${local.private_aaaa}"]
// }

output "public_a" {
description = "List of Droplet public DNS A record FQDNs."
value = ["${local.public_a}"]
Expand Down
17 changes: 0 additions & 17 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,6 @@ variable "resize_disk" {
default = true
}

variable "sizes" {
description = "A map of pre-canned instance sizes."
type = "map"

default = {
nano = "s-1vcpu-1gb"
micro = "s-2vcpu-2gb"
small = "s-2vcpu-4gb"
medium = "s-4vcpu-8gb"
large = "s-6vcpu-16gb"
x-large = "s-8vcpu-32gb"
xx-large = "s-16vcpu-64gb"
xxx-large = "s-24vcpu-128gb"
maximum = "s-32vcpu-192gb"
}
}

variable "ssh_keys" {
description = "(Optional) A list of SSH IDs or fingerprints to enable in the format [12345, 123456]. To retrieve this info, use a tool such as curl with the DigitalOcean API, to retrieve them."
type = "list"
Expand Down

0 comments on commit 6539341

Please sign in to comment.