Browse Source

Initial Commit

master
Theodotos Andreou 2 years ago
commit
d8f31c1625
5 changed files with 225 additions and 0 deletions
  1. +7
    -0
      .gitignore
  2. +70
    -0
      README.md
  3. +92
    -0
      lpi.tf
  4. +21
    -0
      terraform.tfvars.example
  5. +35
    -0
      variables.tf

+ 7
- 0
.gitignore View File

@@ -0,0 +1,7 @@
terraform.tfvars
*.tfplan
.terraform
*.tfstate*
*.swp
*.bak
private.key

+ 70
- 0
README.md View File

@@ -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

+ 92
- 0
lpi.tf View File

@@ -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")}"
}
}
}

+ 21
- 0
terraform.tfvars.example View File

@@ -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=="

+ 35
- 0
variables.tf View File

@@ -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" {}

Loading…
Cancel
Save