Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save micrometre/845caf58dd15de898a2d180a451b1eb6 to your computer and use it in GitHub Desktop.

Select an option

Save micrometre/845caf58dd15de898a2d180a451b1eb6 to your computer and use it in GitHub Desktop.

Revisions

  1. @onnimonni onnimonni created this gist Jun 28, 2017.
    109 changes: 109 additions & 0 deletions terraform_digitalocean_dokku.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    variable "digitalocean_token" {
    description = "This is the Digitalocean API-token which is used to setup the machines."
    }
    variable "digitalocean_region" {
    description = "For example: nyc1, nyc2, ams2, ams3, fra2"
    default = "fra1"
    }
    variable "digitalocean_dokku_size" {
    description = "Instance size: 512mb, 1gb, 2gb, 4gb ..."
    default = "2gb"
    }
    variable "digitalocean_volume_size" {
    description = "We will attach permanent volume for persistent data. You can provide size of that volume here"
    default = "100"
    }

    variable "digitalocean_dokku_domain_tld" {
    description = <<EOF
    Main TLD for this dokku installation like: example.com
    Subdomain of that domain are used for dokku and apps.
    NOTE: THIS DOMAIN NEEDS TO BE SETUPPED IN DIGITALOCEAN DOMAINS!
    EOF
    }

    variable "digitalocean_dokku_subdomain" {
    description = <<EOF
    Write any subdomain of the domain you provided for: var.digitalocean_dokku_domain
    This will be used for this dokku installation and subdomains of that domain will be used to apps running in dokku.
    For example writing: 'dokku-test.something' would result in dokku server => 'dokku-test.something.example.com'
    EOF
    }

    variable "initial_ssh_key" {
    description = "Public SSH Key which is installed into the server during initial setup"
    }

    # Configure the DigitalOcean Provider
    provider "digitalocean" {
    token = "${var.digitalocean_token}"
    }

    resource "digitalocean_ssh_key" "pihvi" {
    name = "Pihvi.io Deployment key"
    public_key = "${var.initial_ssh_key}"
    }

    # Add volume for persistent data
    resource "digitalocean_volume" "dokku_persistent_data" {
    region = "${var.digitalocean_region}"
    name = "persistent-${replace("${var.digitalocean_dokku_subdomain}.${var.digitalocean_dokku_domain_tld}",".","-")}"
    size = "${var.digitalocean_volume_size}"
    description = "Persistent data for ${var.digitalocean_dokku_subdomain}.${var.digitalocean_dokku_domain_tld}"
    }

    # Create a new Dokku Droplet
    resource "digitalocean_droplet" "dokku" {
    # Ubuntu is the prefered installation for dokku
    image = "ubuntu-16-04-x64"

    # This is also the hostname and FQDN of the machine
    name = "${var.digitalocean_dokku_subdomain}.${var.digitalocean_dokku_domain_tld}"
    region = "${var.digitalocean_region}"
    size = "${var.digitalocean_dokku_size}"
    ssh_keys = [ "${digitalocean_ssh_key.pihvi.id}" ]
    ipv6 = true

    # Attach persistent volume
    volume_ids = ["${digitalocean_volume.dokku_persistent_data.id}"]

    user_data = <<EOF
    #cloud-config
    manage-resolv-conf: true
    resolv_conf:
    nameservers:
    - '8.8.8.8'
    - '8.8.4.4'
    EOF
    }

    # Add floating ip for safe system updates
    resource "digitalocean_floating_ip" "dokku" {
    droplet_id = "${digitalocean_droplet.dokku.id}"
    region = "${digitalocean_droplet.dokku.region}"
    }

    # Enable IPv4 (with floating ip-address)
    resource "digitalocean_record" "dokku_ipv4" {
    domain = "${var.digitalocean_dokku_domain_tld}"
    type = "A"
    name = "${var.digitalocean_dokku_subdomain}"
    value = "${digitalocean_floating_ip.dokku.ip_address}"
    }

    # Enable IPv6 (non floating ip-address)
    # To enable flaoting IPv6, Please vote for: https://digitalocean.uservoice.com/forums/136585-digitalocean/suggestions/10513887-floating-ip-ipv6
    resource "digitalocean_record" "dokku_ipv6" {
    domain = "${var.digitalocean_dokku_domain_tld}"
    type = "AAAA"
    name = "${var.digitalocean_dokku_subdomain}"
    value = "${digitalocean_droplet.dokku.ipv6_address}"
    }

    # Enable CNAME for all subdomains
    resource "digitalocean_record" "dokku_subdomains" {
    domain = "${var.digitalocean_dokku_domain_tld}"
    type = "CNAME"
    name = "*.${var.digitalocean_dokku_subdomain}"
    value = "${var.digitalocean_dokku_subdomain}.${var.digitalocean_dokku_domain_tld}."
    }