forked from ndejong/terraform-digitalocean-droplet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
91 lines (77 loc) · 2.69 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# terraform-digitalocean-droplet
# ============================================================================
# Copyright (c) 2021 Verb Networks Pty Ltd <contact [at] verbnetworks.com>
# - All rights reserved.
#
# Apache License v2.0
# - http://www.apache.org/licenses/LICENSE-2.0
# Setup the required terraform and digitalocean provider
# ===
terraform {
required_version = ">= 0.13"
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
}
}
}
provider "digitalocean" {}
# Render the required userdata
# ===
data "template_file" "droplet-bootstrap-sh" {
template = file("${path.module}/data/droplet-bootstrap.sh")
vars = {
# volume0
volume0_dev = element(split(":", var.digitalocean_volume0),1)
volume0_mount = element(split(":", var.digitalocean_volume0),0)
volume0_fstype = element(split(":", var.digitalocean_volume0),3)
# volume1
volume1_dev = element(split(":", var.digitalocean_volume1),1)
volume1_mount = element(split(":", var.digitalocean_volume1),0)
volume1_fstype = element(split(":", var.digitalocean_volume1),3)
initial_user = var.initial_user
initial_user_sshkeys = join("\n", var.initial_user_sshkeys)
}
}
# create the cloudinit user-data string to apply to this droplet
# ===
data "template_cloudinit_config" "droplet-userdata" {
# NB: some kind of cloud-init issue prevents gzip+base64 from working with digitalocean, we (mostly) work around this
# by wrapping via " echo -n | base64 -d | gunzip | /bin/sh" style pipe chain as per below
# ===
gzip = false
base64_encode = false
part {
content_type = "text/x-shellscript"
filename = "10-terraform-bootstrap"
content = "#!/bin/sh\necho -n '${base64gzip(data.template_file.droplet-bootstrap-sh.rendered)}' | base64 -d | gunzip | /bin/sh"
}
part {
content = var.user_data
filename = "20-userdata-bootstrap"
}
}
locals {
volume_ids = [
element(split(":", var.digitalocean_volume0),2),
element(split(":", var.digitalocean_volume1),2)
]
}
# Establish the digitalocean_droplet
# ===
resource "digitalocean_droplet" "droplet" {
image = var.digitalocean_image
name = var.hostname
region = var.digitalocean_region
size = var.digitalocean_size
backups = var.digitalocean_backups
monitoring = var.digitalocean_monitoring
ipv6 = var.digitalocean_ipv6
vpc_uuid = var.digitalocean_vpc_uuid == "" ? null : var.digitalocean_vpc_uuid
private_networking = var.digitalocean_private_networking
ssh_keys = var.digitalocean_ssh_keys
resize_disk = var.digitalocean_resize_disk
tags = compact(var.digitalocean_tags)
user_data = data.template_cloudinit_config.droplet-userdata.rendered
volume_ids = compact(local.volume_ids)
}