forked from alessfg/nginx-controller-automation-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
186 lines (172 loc) · 4.75 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
resource "null_resource" "build_nginx_ami" {
count = var.run_packer ? 1 : 0
provisioner "local-exec" {
command = "packer build -var='nginx_plus_certificate=${var.nginx_plus_certificate}' -var='nginx_plus_key=${var.nginx_plus_key}' -var='ami_name=${var.ami_name_nginx}' -var='owner=${var.owner}' packer/nginx/nginx.pkr.hcl"
}
}
resource "null_resource" "build_nginx_controller_ami" {
count = var.run_packer ? 1 : 0
provisioner "local-exec" {
command = "packer build -var='nginx_controller_tarball_location=${var.nginx_controller_tarball_location}' -var='ami_name=${var.ami_name_nginx_controller}' -var='owner=${var.owner}' packer/nginx-controller/nginx-controller.pkr.hcl"
}
}
resource "null_resource" "build_postgresql_ami" {
count = var.run_packer ? 1 : 0
provisioner "local-exec" {
command = "packer build -var='ami_name=${var.ami_name_postgresql}' -var='owner=${var.owner}' packer/postgresql/postgresql.pkr.hcl"
}
}
resource "null_resource" "build_smtp_ami" {
count = var.run_packer ? 1 : 0
provisioner "local-exec" {
command = "packer build -var='ami_name=${var.ami_name_smtp}' -var='owner=${var.owner}' packer/smtp/smtp.pkr.hcl"
}
}
data "aws_ami" "nginx" {
owners = [
"self"
]
filter {
name = "name"
values = [
var.ami_name_nginx,
]
}
depends_on = [
null_resource.build_nginx_ami,
]
}
data "aws_ami" "nginx_controller" {
owners = [
"self"
]
filter {
name = "name"
values = [
var.ami_name_nginx_controller,
]
}
depends_on = [
null_resource.build_nginx_controller_ami,
]
}
data "aws_ami" "postgresql" {
owners = [
"self"
]
filter {
name = "name"
values = [
var.ami_name_postgresql,
]
}
depends_on = [
null_resource.build_postgresql_ami,
]
}
data "aws_ami" "smtp" {
owners = [
"self"
]
filter {
name = "name"
values = [
var.ami_name_smtp,
]
}
depends_on = [
null_resource.build_smtp_ami,
]
}
module "network" {
# Module source
source = "./terraform/network"
# Module variables
owner = var.owner
}
module "nginx" {
# Module source
source = "./terraform/nginx"
# Module variables
ami_id = data.aws_ami.nginx.id
key_name = var.key_name
subnet_id = module.network.subnet_id
owner = var.owner
}
module "nginx_controller" {
# Module source
source = "./terraform/nginx-controller"
# Module variables
nginx_controller_fqdn = var.nginx_controller_fqdn
ami_id = data.aws_ami.nginx_controller.id
key_name = var.key_name
subnet_id = module.network.subnet_id
owner = var.owner
}
module "postgresql" {
# Module source
source = "./terraform/postgresql"
# Module variables
ami_id = data.aws_ami.postgresql.id
key_name = var.key_name
subnet_id = module.network.subnet_id
owner = var.owner
}
module "smtp" {
# Module source
source = "./terraform/smtp"
# Module variables
ami_id = data.aws_ami.smtp.id
key_name = var.key_name
subnet_id = module.network.subnet_id
owner = var.owner
}
resource "null_resource" "install_nginx_controller" {
count = var.run_ansible ? 1 : 0
provisioner "remote-exec" {
inline = [
"echo 'Instance up and ready'",
]
connection {
type = "ssh"
user = "ubuntu"
private_key = file(var.key_location)
host = var.nginx_controller_fqdn
}
}
provisioner "local-exec" {
command = <<EOT
ansible-galaxy install -r ansible/requirements.yml
ansible-playbook --private-key ${var.key_location} -i ${var.nginx_controller_fqdn}, -u ubuntu ansible/nginx-controller-install.yml \
--extra-vars 'nginx_controller_license_location=${var.nginx_controller_license_location} nginx_controller_tarball_location=${var.nginx_controller_tarball_location} nginx_controller_fqdn=${var.nginx_controller_fqdn} nginx_controller_db_host=${module.postgresql.private_ip} nginx_controller_smtp_host=${module.smtp.private_ip}'
EOT
}
depends_on = [
module.nginx_controller,
]
}
resource "null_resource" "install_nginx_controller_agent" {
count = var.run_ansible ? length(module.nginx.public_ip) : 0
provisioner "remote-exec" {
inline = [
"echo 'Instance up and ready'",
]
connection {
type = "ssh"
user = "ubuntu"
private_key = file(var.key_location)
host = module.nginx.public_ip[count.index]
}
}
provisioner "local-exec" {
command = <<EOT
ansible-galaxy install -r ansible/requirements.yml
ansible-playbook --private-key ${var.key_location} -i ${module.nginx.public_ip[0]}, -u ubuntu ansible/nginx-controller-agent.yml \
--extra-vars 'controller_fqdn=${var.nginx_controller_fqdn}'
EOT
}
depends_on = [
module.nginx,
null_resource.install_nginx_controller,
]
}