forked from oracle-quickstart/oci-nginx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
144 lines (128 loc) · 5.45 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
## DATASOURCE
locals {
ssl_cert_filename = "${basename(coalesce(var.ssl_cert_file_path, "/non_exsit_cert_file"))}"
ssl_cert_key_filename = "${basename(coalesce(var.ssl_cert_key_file_path, "/non_exsit_key_file"))}"
ssl_cert_file_path = "${var.folder_path_for_ssl_cert_files}/${local.ssl_cert_filename}"
ssl_cert_key_file_path = "${var.folder_path_for_ssl_cert_files}/${local.ssl_cert_key_filename}"
}
# Init Script Files
data "template_file" "install_nginx" {
template = "${file("${path.module}/scripts/install.sh")}"
vars {
http_port = "${coalesce(var.server_http_port, "80")}"
https_port = "${coalesce(var.server_https_port, "443")}"
folder_path_for_ssl_cert_files = "${var.folder_path_for_ssl_cert_files}"
ssl_cert_file_path = "${local.ssl_cert_file_path}"
ssl_cert_key_file_path = "${local.ssl_cert_key_file_path}"
}
}
# Create the nginx server host(s)
module "nginx_server" {
source = "./modules/compute-instance"
compartment_id = "${var.compartment_id}"
instance_display_name = "${var.server_display_name}"
source_ocid = "${var.server_image_id}"
vcn_ocid = "${var.vcn_ocid}"
subnet_ocid = "${var.server_subnet_ids}"
ssh_authorized_keys = "${var.server_ssh_authorized_keys}"
assign_public_ip = "${var.server_assign_public_ip}"
instance_count = "${var.server_count}"
shape = "${var.server_shape}"
}
# Do the nginx server setup with Bastion Host
resource "null_resource" "setup_with_bastion" {
depends_on = ["module.nginx_server"]
count = "${var.server_count}"
triggers {
bastion_public_ip = "${join(",",list(var.bastion_host_public_ip))}"
server_private_ip = "${element(module.nginx_server.private_ip, count.index)}"
}
provisioner "file" {
connection = {
bastion_host = "${var.bastion_host_public_ip}"
bastion_host_key = "${chomp(var.bastion_ssh_authorized_keys)}"
bastion_port = 22
bastion_user = "${var.bastion_host_user}"
bastion_private_key = "${chomp(var.bastion_ssh_private_key)}"
host = "${element(module.nginx_server.private_ip, count.index)}"
agent = false
timeout = "10m"
user = "opc"
private_key = "${chomp(var.server_ssh_private_key)}"
}
source = "${var.ssl_cert_file_path}"
destination = "/home/opc/${local.ssl_cert_filename}"
on_failure = "continue"
}
provisioner "file" {
connection = {
bastion_host = "${var.bastion_host_public_ip}"
bastion_host_key = "${chomp(var.bastion_ssh_authorized_keys)}"
bastion_port = 22
bastion_user = "${var.bastion_host_user}"
bastion_private_key = "${chomp(var.bastion_ssh_private_key)}"
host = "${element(module.nginx_server.private_ip, count.index)}"
agent = false
timeout = "10m"
user = "opc"
private_key = "${chomp(var.server_ssh_private_key)}"
}
source = "${var.ssl_cert_key_file_path}"
destination = "/home/opc/${local.ssl_cert_key_filename}"
on_failure = "continue"
}
provisioner "remote-exec" {
connection = {
bastion_host = "${var.bastion_host_public_ip}"
bastion_host_key = "${chomp(var.bastion_ssh_authorized_keys)}"
bastion_port = 22
bastion_user = "${var.bastion_host_user}"
bastion_private_key = "${chomp(var.bastion_ssh_private_key)}"
host = "${element(module.nginx_server.private_ip, count.index)}"
agent = false
timeout = "10m"
user = "opc"
private_key = "${chomp(var.server_ssh_private_key)}"
}
inline = [
"sudo mkdir -p ${var.folder_path_for_ssl_cert_files}",
"sudo mv /home/opc/${local.ssl_cert_filename} ${local.ssl_cert_file_path}",
"sudo mv /home/opc/${local.ssl_cert_key_filename} ${local.ssl_cert_key_file_path}",
]
on_failure = "continue"
}
provisioner "file" {
connection = {
bastion_host = "${var.bastion_host_public_ip}"
bastion_host_key = "${chomp(var.bastion_ssh_authorized_keys)}"
bastion_port = 22
bastion_user = "${var.bastion_host_user}"
bastion_private_key = "${chomp(var.bastion_ssh_private_key)}"
host = "${element(module.nginx_server.private_ip, count.index)}"
agent = false
timeout = "10m"
user = "opc"
private_key = "${chomp(var.server_ssh_private_key)}"
}
content = "${data.template_file.install_nginx.rendered}"
destination = "/tmp/setup.sh"
}
provisioner "remote-exec" {
connection = {
bastion_host = "${var.bastion_host_public_ip}"
bastion_host_key = "${chomp(var.bastion_ssh_authorized_keys)}"
bastion_port = 22
bastion_user = "${var.bastion_host_user}"
bastion_private_key = "${chomp(var.bastion_ssh_private_key)}"
host = "${element(module.nginx_server.private_ip, count.index)}"
agent = false
timeout = "10m"
user = "opc"
private_key = "${chomp(var.server_ssh_private_key)}"
}
inline = [
"chmod +x /tmp/setup.sh",
"sudo /tmp/setup.sh",
]
}
}