commit d8f31c1625d01b129b187617450a5fc456dc9ed5 Author: Theodotos Andreou Date: Sat Oct 20 13:18:34 2018 +0300 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b5a08eb --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +terraform.tfvars +*.tfplan +.terraform +*.tfstate* +*.swp +*.bak +private.key diff --git a/README.md b/README.md new file mode 100644 index 0000000..c243cb9 --- /dev/null +++ b/README.md @@ -0,0 +1,70 @@ +# Terraform setup for DigitalOcean + +This is a teraform setup for my LPI Labs. The servers are deployed on DigitalOcean. The DNS is automatically updated with the server names. + +## Prerequisites + +* Install *Terraform* on your local host: + +``` +$ wget https://releases.hashicorp.com/terraform/0.11.7/terraform_0.11.7_linux_amd64.zip +$ unzip terraform_0.11.7_linux_amd64.zip +$ sudo cp terraform /usr/local/bin +``` + +## Instructions + +* Get the repo + +``` +$ git clone https://git.theo-andreou.org/Personal/lpi-deploy-digitalocean.git +$ cd terraform-scaleway +``` + +* Copy the *terraform.tfvars.example* to *terraform.tfvars* and setup your settings: + +``` +do_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +domain = "example.org" +count = 14 + +server_name = { + "debian" = "lpi-deb" + "centos" = "lpi-centos" +} + +images = { + "debian" = "debian-9-x64" + "centos" = "centos-7-x64" +} + +ssh_keys = [ + "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx", +] + +dns_server = "ns1.example.org" +dns_key_name = "control.ns1.example.org" +dns_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxx+xxxxxxxxxxxxxxxxxxxxx+xxx==" +``` + +* Initialize your *Terraform* environment: + +``` +$ terraform init +``` + +* Create a *Terraform* plan: + +``` +$ terraform plan -out lpi.plan +``` + +* Apply the plan: + +``` +$ terraform apply "lpi.plan" +``` + +## References +* https://www.terraform.io/docs/providers/scaleway/index.html +* https://scaleway.com diff --git a/lpi.tf b/lpi.tf new file mode 100644 index 0000000..5e675c9 --- /dev/null +++ b/lpi.tf @@ -0,0 +1,92 @@ +provider "digitalocean" { + token = "${var.do_token}" +} + +resource "digitalocean_volume" "centos_lpi" { + count = "${var.count}" + name = "${var.server_name["centos"]}-${count.index}.vol" + region = "${var.region}" + size = 20 + description = "Empty Volume" +} + +resource "digitalocean_droplet" "centos_lpi" { + count = "${var.count}" + name = "${var.server_name["centos"]}-${count.index}.${var.domain}" + image = "${var.images["centos"]}" + size = "${var.size}" + region = "${var.region}" + ssh_keys = "${var.ssh_keys}" + volume_ids = ["${element(digitalocean_volume.centos_lpi.*.id, count.index)}"] +} + +resource "digitalocean_volume" "debian_lpi" { + count = "${var.count}" + name = "${var.server_name["debian"]}-${count.index}.vol" + region = "${var.region}" + size = 20 + description = "Empty Volume" +} + +resource "digitalocean_droplet" "debian_lpi" { + count = "${var.count}" + name = "${var.server_name["debian"]}-${count.index}.${var.domain}" + image = "${var.images["debian"]}" + size = "${var.size}" + region = "${var.region}" + ssh_keys = "${var.ssh_keys}" + volume_ids = ["${element(digitalocean_volume.debian_lpi.*.id, count.index)}"] +} + +provider "dns" { + update { + server = "${var.dns_server}" + key_name = "${var.dns_key_name}" + key_algorithm = "hmac-sha512" + key_secret = "${var.dns_key}" + } +} + +resource "dns_a_record_set" "centos_lpi" { + count = "${var.count}" + zone = "${var.domain}." + name = "${var.server_name["centos"]}-${count.index}" + addresses = ["${element(digitalocean_droplet.centos_lpi.*.ipv4_address, count.index)}"] + ttl = 300 + + provisioner "remote-exec" { + inline = [ + "rndc sync -clean", + ] + + connection { + type = "ssh" + host = "${var.dns_server}" + user = "root" + port = "3347" + private_key = "${file("private.key")}" + } + } +} + +resource "dns_a_record_set" "debian_lpi" { + count = "${var.count}" + zone = "${var.domain}." + name = "${var.server_name["debian"]}-${count.index}" + addresses = ["${element(digitalocean_droplet.debian_lpi.*.ipv4_address, count.index)}"] + ttl = 300 + + provisioner "remote-exec" { + inline = [ + "rndc sync -clean", + ] + + connection { + type = "ssh" + host = "${var.dns_server}" + user = "root" + port = "3347" + private_key = "${file("private.key")}" + } + } +} diff --git a/terraform.tfvars.example b/terraform.tfvars.example new file mode 100644 index 0000000..bd4b3ee --- /dev/null +++ b/terraform.tfvars.example @@ -0,0 +1,21 @@ +do_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +domain = "example.org" +count = 14 + +server_name = { + "debian" = "lpi-deb" + "centos" = "lpi-centos" +} + +images = { + "debian" = "debian-9-x64" + "centos" = "centos-7-x64" +} + +ssh_keys = [ + "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx", +] + +dns_server = "ns1.example.org" +dns_key_name = "control.ns1.example.org" +dns_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxx+xxxxxxxxxxxxxxxxxxxxx+xxx==" diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..6d02ab2 --- /dev/null +++ b/variables.tf @@ -0,0 +1,35 @@ +variable "do_token" {} + +variable "count" { + default = 11 +} + +variable "region" { + default = "lon1" +} + +variable "size" { + default = "1gb" +} + +variable "server_name" { + type = "map" +} + +variable "domain" { + default = "example.com" +} + +variable "images" { + type = "map" +} + +variable "ssh_keys" { + type = "list" +} + +variable "dns_server" {} + +variable "dns_key_name" {} + +variable "dns_key" {}